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.