Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins input pipeline step filled via POST with CSRF - howto?

I have Jenkins pipeline with an Input step, and I would like to submit this input(single string argument) via a script. So far I am trying with curl, ideally I'll be sending it via Python requests library. This should be an easy POST request, however with CSRF it becomes tricky. I've obtained Jenkins-Crumb (using curl in this case, from the same machine and same bash session), but still can't send the content...

I'm sending Jenkins-Crumb:XXX header, just like it is explained at https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API

my request looks like this:

curl -vvv -X POST --user '${USER}:${API_KEY}' -H "Jenkins-Crumb:${JENKINS_CRUMB}" -d 'json="{"parameter":{"name":"${PARAM_NAME}","value":"asd"},"Jenkins-Crumb":"${JENKINS_CRUMB}"}"' 'http://${JENKINS_URL}/job/${JOB_NAME}/${BUILD_NR}/input/'

The URL I'm POSTing at is the same, as the one linked in build log (Console output).

like image 606
J. Doe Avatar asked Aug 08 '16 11:08

J. Doe


2 Answers

I managed to resolve this issue.

Hoping that someone else will benefit from my answer, let me explain how I achieved OPs target.

All that Jenkis Pipeline input step needs is a properly formatted JSON and OK button caption sent to the right URL.

So, the proper syntax is:

curl -X POST -H "Jenkins-Crumb:${JENKINS_CRUMB}" -d json='{"parameter": {"name": "${PARAMETER_NAME}", "value": "${PARAMETER_VALUE}"}}' -d proceed='${SUBMIT_CAPTION}' 'http://j${JENKINS_URL}/job/${JOB_NAME}/${BUILD_ID}/input/${INPUT_ID}/submit'

regards

like image 122
J. Doe Avatar answered Oct 14 '22 04:10

J. Doe


There is an easier way, simply call the proceedEmpty URL for the jobs:

curl -X POST -H "Jenkins-Crumb:${JENKINS_CRUMB}" http://${JENKINS_URL}/job/${JOB_NAME}/${BUILD_ID}/input/${INPUT_ID}/proceedEmpty

There is no need to pass in body data.

To abort, use:

curl -X POST -H "Jenkins-Crumb:${JENKINS_CRUMB}" http://${JENKINS_URL}/job/${JOB_NAME}/${BUILD_ID}/input/${INPUT_ID}/abort
like image 37
andriosr Avatar answered Oct 14 '22 04:10

andriosr