本文介绍了如何在不使用buildWithParameters的情况下调用Jenkins作业时在curl中传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Jenkins管道作业没有参数化,但是从管道脚本调用该作业时,我可以提供在当前作业中使用的参数.

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.

我想使用curl命令从外部传递这些参数.我尝试了以下选项,但尚未成功.

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'

该调用失败,因为我的当前作业DS_JOB1没有参数化.

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.

举个例子,我提供了2个简单的jenkinsfiles,在jenkins管道实现方式中效果很好.

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()
            }
        }
    }
}

从上述作业中调用的作业DS_JOB1的詹金文件是:

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>
        }
    }
}

推荐答案

在DS_JOB1 Jenkinsfile中添加了以下代码.

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')
        ])
    ])

添加此命令后,我可以使用以下curl命令运行该作业.

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'

如果有任何线索而无需更改"DS_JOB1",请分享.

If anyone having any clue without making changes to "DS_JOB1", please share.

这篇关于如何在不使用buildWithParameters的情况下调用Jenkins作业时在curl中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 23:45