API (curl)Command to Approve a promoted build Job in Jenkins

爷,独闯天下 提交于 2019-12-23 17:32:07

问题


Is there any way can an Approver approve a specific build using curl command?

I am using Promoted Builds Plugin for manual approval for builds.

when i am trying below curl command it is giving "Error 400 Nothing is submitted". I searched everywhere but couldn't get proper answer. Please help

curl http://admin:XXXXXXXXXXXX@JENKINS_URL/job/JOB_NAME/129/promotion/promotionProcess/PromoteForTesting/promotionCondition/hudson.plugins.promoted_builds.conditions.ManualCondition/approve?token=abcde1234

When i tried to pass JSON data, It is throwing "Error 403 No valid crumb was included in the request"

the command is curl http://admin:XXXXXXXXXXXX@JENKINS_URL/job/JOB_NAME/129/promotion/promotionProcess/PromoteForTesting/promotionCondition/hudson.plugins.promoted_builds.conditions.ManualCondition/approve?token=abcde1234 --data-urlencode json='{&Submit=Approve}'

I followed this API support for manual approval of promoted builds in Jenkins? post as a reference but no luck


回答1:


Yeah finally got a solution after much plug n play.. thought to share as it could help others. First of all the Json Values i am passing are not correct and it doesn't have all the parameters the promotion expecting. Second as i have enabled CSRF protection the HTTP request should have a valid crumb. So What i did was i manually approved some dummy job and got the JSON of the successful promoted job. It contain all the passed parameters. So it helped me to get all the parameters that should be passed from CURL command. So finally the working URL is below. You can pass/remove any additional parameters to the JSON DATA as per your requirement.

Please Note: the crumb field name is changed from what much answers provided in many stackoverflow answers. Now the Filed name is not ".crumb" but "Jenkins-Crumb"

curl -v -H "Jenkins-Crumb:XXXXXXXXXXX" -X POST http://USER:TOKEN@JENKINS_URL/job/JOB_NAME/JOB_NUMBER/promotion/promotionProcess/PromoteForTesting/promotionCondition/hudson.plugins.promoted_builds.conditions.ManualCondition/approve --data-urlencode json='{"parameters":[{"_class":"hudson.model.StringParameterValue","name":"PARAMETER","value":""},{"_class":"hudson.model.StringParameterValue","name":"Job","value":"Deploy(ZettaDevelopment)"},{"_class":"hudson.model.StringParameterValue","name":"BuildSelection","value":"PARAMETER"},{"_class":"hudson.model.StringParameterValue","name":"PARAMETER","value":"PARAMETER"}]}'

The Parameter Values passed are found using http://USER:TOKEN@JENKINS_URL/job/JOB_NAME/JOB_NUMBER/promotion/promotionProcess/PromoteForTesting/promotionBuild/Build_NUMBER/api/json

Here Build_NUMBER is the build i manually approved. From the output you can easily get the required parameters to be passed.

Hope This can help as at least it worked for me :-)



来源:https://stackoverflow.com/questions/39920650/api-curlcommand-to-approve-a-promoted-build-job-in-jenkins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!