-->

passing parameter to jenkins job through curl POST

2020-03-20 13:36发布

问题:

I try to launch

curl -X POST -u "user:pass" -H Jenkins-Crumb:thecrumbnumber http://myjenkinsserver/jenkins/job/testjob/buildWithParameters?=PARAMETER=somenumber

which works by triggering a parametrized build, but the problem is no value is passed to the build (whats weird even when the token is set in the job, I can trigger the job without it). In jenkins, that job has a configured string "PARAMETER" with or without some default value, but never the parameter from the curl launch is passed. What I may be missing?

回答1:

I'm using:

curl -X POST -u "user" "http://myjenkins/path/to/my/job/buildWithParameters?GERRIT_REFNAME=feature/retry&goal=package"

here and it's working like a charm.

Watch out the "=" in front of the "PARAMETER" in the URL you pasted.



回答2:

I used below command to pass Multiple parameter.

curl -X POST "https://myjenkins.com/job/jobname/buildWithParameters?token=developer&name=abc&userid=CFDH123&mail=abc@gmail.com"

Note: parameter names are case sensitive.



回答3:

Please try something like:

curl -X POST http://<jenkins URL>/jenkins/job/TESTS/job/<Your job's Name>/buildWithParameters \
  --user <Jenkins account>:<account's TokenID>  \
  --data token=<job's token (if required)> \
  --data parameter=some_value
  ...
  --data MessageTextParameter=My%20Text


回答4:

When I tried

curl -X POST http://icm.git:faedc992afcd1ea4db3da6809435117a@10.221.31.36:8080/job/1_TODO_Get_Submit_Request_With_Dependency_Test/buildWithParameters?PARAMETER=1494390553639

OR

curl -X POST -u "icm.git:lge123" -H Jenkins-Crumb:faedc992afcd1ea4db3da6809435117a http://10.221.31.36:8080/job/1_TODO_Get_Submit_Request_With_Dependency_Test/buildWithParameters?PARAMETER=1494390553639

Both triggered Jenkins Job > 1_TODO_Get_Submit_Request_With_Dependency_Test but the parameter in Jenkins log showed default value for parameter REQUESTID = -1.

What is problem with Jenkins or CURL url ?

+ echo '4.2.25(1)-release'
4.2.25(1)-release
+ echo

+ echo 26
26
+ echo '#26'
#26
+ echo 1_TODO_Get_Submit_Request_With_Dependency_Test
1_TODO_Get_Submit_Request_With_Dependency_Test
+ echo 1_TODO_Get_Submit_Request_With_Dependency_Test
1_TODO_Get_Submit_Request_With_Dependency_Test
+ echo REQUESTID = -1
REQUESTID = -1


回答5:

I'm also trying to post but the following curl command, but it doesn't post.

curl -X POST -v https://JENKINS_SERVER/job/JOBNAME/buildWithParameters --user foo:8382 --data-urlencode json='{"parameter": [{"name": "registration_action","value": "DNS_only"},{"name": "entries","value": "add, hostname01.local.example.com, 192.168.1.1/24\nremove, hostname01.local.example.com, 192.168.1.1/24/\n" }]}'

It creates the job but doesn't pass in the variables. Not sure, what I'm doing incorrectly.



回答6:

Looks like there is a typo in Jenkins Confluence page while calling Jenkins url passing parameters as JSON payload.. https://wiki.jenkins.io/display/JENKINS/Remote+access+API Tried with url suffix as build instead of buildWithParameters and it is working..

curl -X POST -u "user:token/password" "http://myjenkins/path/to/my/job/build --data-urlencode json='{"parameter": [{"name":"GERRIT_REFNAME", "value":"feature/retry"},{"name":"goal", "value":"package"}]}'