问题
My Jenkins pipeline job is not parameterized, but while calling the job from a pipeline script I can provide parameters that are getting used inside my current job.
I would like to pass those parameters from outside using a curl command. I tried the following options but am yet to be successful.
curl -i -X POST 'https://<USERNAME>:<API_TOKEN>@JENKINS_URL/job/DS_JOB1/build?token=remotejob' --data-urlencode json='{"parameter": [{"PLATFORM":"Value1", "PROJECT": "Project_Type"}]}'
This doesn't work as I am unable get the parameters in the called job.
curl -i -X POST 'https://<USERNAME>:<API_TOKEN>@JENKINS_URL/job/DS_JOB1/buildWithParameters?token=remotejob&TestProject=NewCurlTesting'
This call crashes because my current job DS_JOB1 is not parameterized.
I am wondering if anyone has already used such jobs and fixed the issue.
To give example, I am providing 2 simple jenkinsfiles, which works fine in jenkins pipeline way of implementation.
node('LABEL_NAME') {
timestamps {
try {
stage("Calling Downstream Job") {
job_downstream = build(job: "DS_JOB1",
parameters: [[$class: 'StringParameterValue', name: 'PLATFORM', value: "pf-1"],
[$class: 'StringParameterValue', name: 'PROJECT', value: "Dummy1"]],
propagate: false,
wait: true)
if(job_downstream?.result.toString() == 'FAILURE') {
currentBuild.result = job_downstream?.result.toString()
println("Downstream job for PLATFORM: ${PLATFORM}")
}
}
}
catch (err) {
println(err)
currentBuild.result = 'FAILURE'
}
finally {
stage('Post build actions') {
// Mailer notification
step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: '<GroupMail_ID>', sendToIndividuals: false])
deleteDir()
}
}
}
}
Jenkinsfile for the job DS_JOB1 that is called from above job is:
node('LABEL_NAME') {
stage('Trigger Testlauncher') {
if("${PLATFORM}" == "pf-1")
{
<Take some action>
}
else if("${PROJECT}" == "Dummy1")
{
< Take some action>
}
else
{
<Take something>
}
}
}
回答1:
My Jenkins pipeline job is not parameterized, but while calling the job from a pipeline script I can provide parameters that are getting used inside my current job.
To confirm I understood it correctly, your pipeline script is able to 'successfully' invoke DS_JOB1 passing parameters even though the DS_JOB1 job is not parameterized ?
What version of Jenkins are you using ? I quickly tested on Jenkins version 2.146 and it doesn't work on my end as it says it doesn't recognize the property PLATFORM in the downstream job.
Are you able to parameterize your DS_JOB1 job and then you should be able to invoke using curl
curl -X POST JENKINS_URL/job/JOB_NAME/build \
--user USER:TOKEN \
--data-urlencode json='{"parameter": [{"PLATFORM":"Value1", "PROJECT": "Project_Type"}]}'
回答2:
Added following peice of code inside DS_JOB1 Jenkinsfile.
properties(
[
parameters(
[
string(defaultValue: 'PF-1', description: 'Project repo name to checkout for static analysis', name: 'PLATFORM'),
string(defaultValue: 'Dummy1', description: 'Project branch to be used', name: 'PROJECT')
])
])
After adding this I could run the job with following curl command.
curl -i -X POST 'https://<user>:<Token>@JENKINS_URL/job/DS_JOB1/buildWithParameters?token=remotejob&PLATFORM=NewCurlTesting&PROJECT=Test1'
If anyone having any clue without making changes to "DS_JOB1", please share.
来源:https://stackoverflow.com/questions/52771348/how-to-pass-parameter-in-curl-while-calling-jenkins-job-without-using-buildwithp