How do I specify multiple node values in a buildWithParameters call to Jenkins?

Viewed 166

I have a webhook service which kicks off a buildWithParameters Jenkins job, and I want to be able to specify which buildservers are being used.

This is easy enough in the job configuration - I've added a Node parameter which lets me specify which nodes are valid, and when starting the job manually in the Jenkins web UI, I can select which nodes I want:

Jenkins Node parameter

I'm able to kick off the job via curl using the buildWithParameters Jenkins feature:

curl -vvv 'https://webhook:examplepassword@jenkins.example.com/job/build-sideboard-plugin/buildWithParameters?token=exampletoken&GIT_REPO=example/repo&YUM_REPO=example&BUILDSERVER=sideboard.build.dev.xr'

However, I can't figure out how to specify multiple parameters. I expected that I'd simply be able to add a second &BUILDSERVER=xxx value and have that work, but running this:

curl -vvv 'https://webhook:examplepassword@jenkins.example.com/job/build-sideboard-plugin/buildWithParameters?token=exampletoken&GIT_REPO=example/repo&YUM_REPO=example&BUILDSERVER=sideboard.build.dev.xr&BUILDSERVER=sideboard.rocky8.build.dev.xr'

Returns a 500 error. I also tried providing a single value with a comma separating the two values, i.e.

curl -vvv 'https://webhook:examplepassword@jenkins.example.com/job/build-sideboard-plugin/buildWithParameters?token=exampletoken&GIT_REPO=example/repo&YUM_REPO=example&BUILDSERVER=sideboard.build.dev.xr,sideboard.rocky8.build.dev.xr'

but Jenkins interpreted that as a single Node value which didn't match any node since there's no node named sideboard.build.dev.xr,sideboard.rocky8.build.dev.xr. I got the same result when submitting the two values separated by a space.

Is there any way to get Jenkins to do this while still using the buildWithParameter functionality? I'd hate to have to redo the structure of our build triggering or switch to Jenkins Pipeline. Even making 2 different curl commands would be somewhat of a pain given how our webhooks are structured, so I'd love to be able to provide both parameters just like I can in the Jenkins web UI.

1 Answers

I don't think it is possible using the query parameters like you have tried, due to the fact the the plugin actually triggers two different builds.

What you can do is pass the parameters with the submit command as JSON data, which will simulate the trigger of the build with multiple servers selected.
The general syntax will be something like:

curl -u USER:PASSWORD --show-error \
     --data 'json={"parameter":[{"name":"PARAMNAME","value":["node1","node2"]}]}' \
     http://localhost:8080/job/remote/build?token=TOKEN

or in your case:

curl -u webhook:examplepassword --show-error \
     --data 'json={"parameter":[{"name":"BUILDSERVER","value":["sideboard.build.dev.xr","sideboard.rocky8.build.dev.xr"]}]}' \
     https://jenkins.example.com/job/build-sideboard-plugin/build?token=exampletoken

You can of course pass all other needed parameters alongside the BUILDSERVERin the JSON data:

curl -u webhook:examplepassword --show-error \
     --data 'json={"parameter":[{"name":"BUILDSERVER","value":["sideboard.build.dev.xr","sideboard.rocky8.build.dev.xr"]},{"name":"YUM_REPO","value":"example"},{"name":"GIT_REPO","value":"=example/repo"}]}' \
     https://jenkins.example.com/job/build-sideboard-plugin/build?token=exampletoken

In addition it is probably better to use the --data-urlencode instead of the --data flag for the curl commands to avoid encoding issues in case the values of your parameters have special characters.

More info on submitting jobs via Remote Access API is available Here.

Related