How to remotely trigger Jenkins multibranch pipeline project build?
Asked Answered
M

4

21

Title mostly says it. How can you trigger a Jenkins multibranch pipeline project build from a remote git repository?

The "Trigger builds remotely" build trigger option does not seem to work, since no tokens that you set are saved.

Mainstay answered 14/9, 2016 at 12:11 Comment(0)
M
19

At the moment (Jenkins 2.22) the "Trigger builds remotely" build trigger option is visible in the multibranch pipeline job configuration, but does not work (if you check it and specify a token, it gets reset after saving anyway). According to this, it is intentional that the trigger cannot be set, but a bug that it appears as an option at all.

In the same thread they explain how to trigger builds for each individual project (branch) in a multibranch pipeline project. What I needed was a dynamic setup that would work for branches created after setting up the trigger, so rather than the suggested endpoint from the thread (/job/project-name/job-name/build, which should have been /job/job-name/project-name/build , since projects are created from branches in a job), I found that the endpoint to use is /job/job-name/build. In order for that to work you have to create a user with an API token (go to Manage Jenkins -> Manage Users -> Gear icon -> Show API Token), and use those as username and password in your request.

The solution might be obvious for those used to working with Jenkins REST API, but when you are new to both multibranch pipeline projects and the REST API, it doesn't hurt to be explicit.

Mainstay answered 14/9, 2016 at 12:11 Comment(2)
Thanks for adding this, this has helped me a lot this morning :-) i do get an invalid HTTP response 302 though in the github webhook sectionHelluva
For build parameters, this worked for me. In caps what should be replaced: http:// SERVER_URL/job/MULTIBRANCH_NAME/job/BRANCH_NAME/buildWithParameters?token=USER_TOKEN&PARAM1=VALUE1Kanter
C
7

I couldn't get the API token as described in the accepted answer because there is no such link called "Manage Users" even though I log in as admin. Instead, I got the token as described in the Jenkins Wiki:

The API token is available in your personal configuration page. Click your name on the top right corner on every page, then click "Configure" to see your API token

Once you have the token, the following curl request will trigger a new build for a multibranch pipeline (Replace placeholders starting with $)

curl -X POST -u "$jenkins_username:$api_token" "http://$jenkins_url/job/$my-pipeline/job/$branch_name/build?token=BUILD_TOKEN"

Notes:

  1. If the pipeline or branch name contains special characters, you need to encode it; for example, / becomes %252F.
  2. The token query parameter is optional.
  3. This answer has been verified in Jenkins v2.6.
Chufa answered 2/3, 2018 at 0:37 Comment(4)
This worked for me. But please remember that if you are not the only user on your Jenkins server you should create a user specifically for this task. Don't use your personal token for a build which somebody else might one day need to maintain.Staphylo
@andrewlorien of course, you could also have a “service account”Chufa
how can i parametrize it ?Inclinable
@CarlosAndres post a new question, don’t piggyback on this oneChufa
U
5

Without disabling the Cross Site Request Forgery (CSRF) protection, the commands you can make use of are

crumb=$(curl -s 'http://USERNAME:API_TOKEN@JENKINS_URL/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
curl -X POST -H "$crumb" "http://USERNAME:API_TOKEN@JENKINS_URL/job/JOB_NAME/build"

Replace the capital letters with the appropriate values.

Underbrush answered 22/12, 2017 at 9:11 Comment(0)
H
0

I just recently got over this hurdle and would like to share my note.

In my configuration (Jenkins 2.60.2), there is no way for me to enable the Trigger builds remotely (e.g., from scripts) option since I can only "View Configuration". This prevented me from trigger the pipeline by issue HTTP GET to end point (/job/project-name/job/job-name/build).

However, I found out that I can always trigger by issue HTTP POST to the end point.

You will have to provide a valid crumb in the request or disable the "Prevent Cross Site Request Forgery exploits" option in Configure Global Security.

Regards,

Hydrocephalus answered 13/10, 2017 at 16:22 Comment(1)
@MattFriedman The author suggests disabling CSRF so that crumb is not needed. which is not the correct way.Impractical

© 2022 - 2024 — McMap. All rights reserved.