简体   繁体   English

从 Jenkins 作业传递 Jenkins 管道参数?

[英]Pass Jenkins Pipeline parameters from a Jenkins job?

I have a Jenkins Pipeline script being used to deploy lambdas.我有一个 Jenkins 管道脚本用于部署 lambda。 Right now the user passes the parameters to the job to kick it off.现在,用户将参数传递给作业以启动它。 I want to automate the job a bit more and create a process where the job triggers and the parameters are passed via a JSON file to kick off the job.我想进一步自动化这项工作并创建一个流程,在该流程中,工作触发和参数通过 JSON 文件传递以启动工作。

I'm not clear on how to proceed I've seen that maybe JsonSlurper could be used, but not sure if that is the ideal solution for the process.我不清楚如何继续我已经看到也许可以使用 JsonSlurper,但不确定这是否是该过程的理想解决方案。

Does anyone have a good solution that I could implement?有没有人有一个我可以实施的好解决方案?

I would recommend the GenericWebhook Plugin.我会推荐 GenericWebhook 插件。

You can define a token and then use a POST request to trigger the job with any JSON you need.您可以定义一个令牌,然后使用 POST 请求通过您需要的任何 JSON 触发作业。 The plugin will take care of triggering the job and even unpacking variables from the JSON if you want it to.如果您愿意,该插件将负责触发作业,甚至从 JSON 中解压缩变量。

Might not be the best solution, but it's the one I use extensively.可能不是最好的解决方案,但它是我广泛使用的解决方案。 You can read in a JSON file like so..您可以像这样读取 JSON 文件。

node() {
    stage("Read JSON") {
        // This has to be done within a node() construct
        myObj = readJSON file: '/opt/app/jenkins/userContent/test.json'
    }
}

Here's a simple JSON file to test with..这是一个简单的 JSON 文件进行测试。

{
  "Hosts": [
    { 
      "Hostname": "host1.foobar.com",
      "Purpose": "Web Server"
    },
    { 
      "Hostname": "host2.foobar.com",
      "Purpose": "DB Server"
    },
    { 
      "Hostname": "host3.foobar.com",
      "Purpose": "App Server"
    }
  ]
}

You can reference it like so..你可以像这样引用它..

for (int hostnum=0; hostnum < myObj.Hosts.size(); hostnum++) {
   println "Hostname: " + myObj.Hosts[hostnum].Hostname
   println "Purpose: " + myObj.Hosts[hostnum].Purpose
}

From the build log..从构建日志..

Hostname: host1.foobar.com
[Pipeline] echo
Purpose: Web Server
[Pipeline] echo
Hostname: host2.foobar.com
[Pipeline] echo
Purpose: DB Server
[Pipeline] echo
Hostname: host3.foobar.com
[Pipeline] echo
Purpose: App Server

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将“git 参数”从管道詹金斯作业 A 传递到作业 B - Pass "git parameters" from pipeline jenkins job A to job B 如何在詹金斯中将参数从管道作业传递到Maven作业 - How to pass parameters from a pipeline job to a maven job in Jenkins 如何将变量从管道传递到 Jenkins 中的作业? - How to pass variable from pipeline to job in Jenkins? 将变量从 jenkins 管道作业传递到其他管道作业 - Pass variable from jenkins pipeline job to other pipeline job 如何在 git push 上将自定义参数传递给 jenkins 作业(多分支管道)? - How to pass custom parameters to jenkins job (multibranch pipeline) on git push? Jenkins:运行作业后参数从管道作业中消失 - Jenkins: Parameters disappear from pipeline job after running the job 从詹金斯的自由式作业下游管道作业传递值 - Pass values from a freestyle job downstream pipeline job in jenkins Jenkins - 如何将环境变量从自由式作业传递到管道作业 - Jenkins - How to pass environmental variable from freestyle job to pipeline job 将环境变量从父作业传递到子作业 Jenkins 管道 - Pass environment variables from parent job to child job Jenkins pipeline 在Jenkins管道的中间作业中接受用户的参数 - Accepting parameters from user in a middle job of a Jenkins pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM