Pass Jenkins Pipeline parameters from a Jenkins job?

喜夏-厌秋 提交于 2019-12-08 11:20:57

问题


I have a Jenkins Pipeline script being used to deploy lambdas. 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.

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.

Does anyone have a good solution that I could implement?


回答1:


I would recommend the GenericWebhook Plugin.

You can define a token and then use a POST request to trigger the job with any JSON you need. The plugin will take care of triggering the job and even unpacking variables from the JSON if you want it to.




回答2:


Might not be the best solution, but it's the one I use extensively. You can read in a JSON file like so..

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

{
  "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


来源:https://stackoverflow.com/questions/58031673/pass-jenkins-pipeline-parameters-from-a-jenkins-job

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