In the new Jenkins Pipeline, under Build Triggers, select the checkbox Trigger builds remotely (e.g., from scripts). Then give Jenkins a token that will be required when triggering the build.
Not authorized
errors
A problem with triggering the builds remotely is, if you've set up Jenkins right and disabled anonymous user access, you will get Not authorized
errors when you try to trigger the build from a script (as @keocra pointed out). You now have two options:
- Pass a username and password along when you trigger the build. This means that your script will need to include the username and password, which means everyone who can read your script will have the username and password, which is almost as bad as anonymous access.
- Use the Build Token Root Plugin. This plugin allows you to use the Trigger builds remotely feature without requiring the username and password. All you need is the token you generated before.
Triggering the build
To trigger the build remotely, run
curl JENKINS_URL/buildByToken/build?job=JobFoo&token=MyToken
Where JENKINS_URL
is the URL to your Jenkins instance, JobFoo
is the name of your job, and MyToken
is the token you entered under Trigger bulids remotely.
Of course, you don't need to use curl
; you can also use wget
or any other program that can make HTTP requests.