How to trigger a build remotely?

Jenkins gives a nice way to trigger builds remotely through its ReST API.

The first problem that comes is to find a way to use this API without using your own account. Indeed, it is a bad idea to share your credentials with anyone having access to this script. To solve this issue, you can use the Jenkins Build token root plugin. You can simply install it through the Jenkins plugin manager.

Then, you can configure your job to accept remote build triggers as follows:

remote-build-trigger

Don’t forget to provide a TOKEN.

Then, from a remote script, you can simply invoke:

wget --no-check-certificate 'JENKINS_SERVER/buildByToken/build?job=NAME&token=SECRET'

where:

  • JENKINS_SERVER is the URL used to reach your jenkins server,
  • NAME is the job name,
  • and SECRET is the token you specified in the job configuration.

2 comments

  1. Sorry to come back to this after so long, but I’m working on Jenkins in a secure environment, and I need to solve the opposite problem: How can I initiate slave builds that must be under my personal login on the slave? It seems like this should be easier, as you allude in your comment, but I can’t find any guides to that.

    1. You can define a Jenkins node by providing your personal login and an SSH key to use. Once done, your jobs will be able to run jobs on this slave by using your account. One other way would be to define a JNLP node and initiate the connection from your slave. See https://support.cloudbees.com/hc/en-us/articles/227834227-How-to-create-a-new-node- for more information

Leave a Reply