简体   繁体   中英

Pass Jenkins Pipeline parameters from a Jenkins job?

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM