2016-07-01 15 views
28

Sto creando un nuovo lavoro in Jenkins utilizzando l'API REST. Ho provato sotto linee cmd arricciatura ma sono gettando erroreAPI REST Jenkins Crea lavoro

curl -i -X POST --user "admin:<API token>" --data-binary "@C:\mylocalconfig.xml" -H "Content-Type: text/xml" http://localhost:8080/createItem?name=NewJob 

curl -X POST -u <username>:<pass> -H "Content-Type:application/xml" -d "@C:\mylocalconfig.xml" "http://localhost:8080/createItem?name=AA_TEST_JOB3" 

errore:


HTTP/1.1 403 No valid crumb was included in the request 
Date: Fri, 01 Jul 2016 05:25:59 GMT 
X-Content-Type-Options: nosniff 
Content-Type: text/html; charset=ISO-8859-1 
Cache-Control: must-revalidate,no-cache,no-store 
Content-Length: 360 
Server: Jetty(9.2.z-SNAPSHOT) 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<title>Error 403 No valid crumb was included in the request</title> 
</head> 
<body><h2>HTTP ERROR 403</h2> 
<p>Problem accessing /createItem. Reason: 
<pre> No valid crumb was included in the request</pre></p><hr><i><small>Power 
ed by Jetty://</small></i><hr/> 
</body> 
</html> 

+0

[Eventuali duplicati] (http://stackoverflow.com/questions/23497819/trigger-parameterized-build-with-curl-and-crumb) – rdupz

risposta

50

Jenkins predefinita ha CSRF Protection abilitato che impedisce one-click attacks. Per richiamare la richiesta, è necessario ottenere la briciola da /crumbIssuer/api/xml utilizzando le credenziali e includerla nella richiesta. Per esempio:

CRUMB=$(curl -s 'http://USER:[email protected]:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)') 

quindi è possibile creare un posto di lavoro (includendo briciole sul suo colpo di testa):

curl -X POST -H "$CRUMB" "http://USER:[email protected]:8080/createItem?name=NewJob" 

Se in precedenza non funziona, controllare la mollica (echo $CRUMB) o eseguire curl con -u USER:TOKEN.

Per una spiegazione più dettagliata, vedere: Running jenkins jobs via command line.

+0

Quando faccio questo, ottengo 'Errore No Content Tipo intestazione set ' – battey

+0

A partire dal 2017, questo è l'esempio completo: [Esecuzione di lavori jenkins tramite la riga di comando] (http://www.inanzzz.com/index.php/post/jnrg/running-jenkins-build-via- riga di comando) – BentCoder