简体   繁体   English

如何远程触发Jenkins多分支管道项目构建?

[英]How to remotely trigger Jenkins multibranch pipeline project build?

Title mostly says it. 标题大多是这样说的。 How can you trigger a Jenkins multibranch pipeline project build from a remote git repository? 如何从远程git存储库触发Jenkins多分支管道项目构建?

The "Trigger builds remotely" build trigger option does not seem to work, since no tokens that you set are saved. “ Trigger远程构建”构建触发器选项似乎不起作用,因为没有保存您设置的令牌。

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). 目前(Jenkins 2.22),“触发触发器远程构建”构建触发选项在多分支管道作业配置中可见,但不起作用(如果您检查并指定了令牌,则无论如何保存后都会重置)。 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 . 我需要的是一种动态设置,该设置适用于设置触发器后创建的分支,因此,而不是线程中的建议端点( /job/project-name/job-name/build ,应该是/job/job-name/project-name/build ,因为项目是从作业中的分支创建的,所以我发现要使用的端点是/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. 为了使其正常工作,您必须创建一个具有API令牌的用户(进入Manage Jenkins-> Manage Users-> Gear icon-> Show API Token),并在请求中使用这些用户名和密码。

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. 该解决方案对于那些曾经使用过Jenkins REST API的人来说可能是显而易见的,但是当您不熟悉多分支管道项目和REST API时,明确表示就不会受到伤害。

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. 我无法获得接受的答案中所述的API令牌,因为即使以admin身份登录,也没有名为“ Manage Users”的链接。 Instead, I got the token as described in the Jenkins Wiki : 相反,我得到了Jenkins Wiki中所述的令牌:

The API token is available in your personal configuration page. 您的个人配置页面中提供了API令牌。 Click your name on the top right corner on every page, then click "Configure" to see your API token 在每个页面的右上角单击您的名称,然后单击“配置”以查看您的API令牌

Once you have the token, the following curl request will trigger a new build for a multibranch pipeline (Replace placeholders starting with $ ) 获得令牌后,以下curl请求将触发多分支管道的新构建(替换以$开头的占位符)

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 . 例如, /变为%252F
  2. The token query parameter is optional. token查询参数是可选的。
  3. This answer has been verified in Jenkins v2.6. 此答案已在Jenkins v2.6中得到验证。

Without disabling the Cross Site Request Forgery (CSRF) protection, the commands you can make use of are 在不禁用跨站点请求伪造(CSRF)保护的情况下,可以使用的命令是

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. 用适当的值替换大写字母。

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 (eg, from scripts) option since I can only "View Configuration". 在我的配置(Jenkins 2.60.2)中,由于我只能“查看配置”,因此无法启用远程(例如,从脚本)构建触发器的选项。 This prevented me from trigger the pipeline by issue HTTP GET to end point (/job/project-name/job/job-name/build). 这阻止了我通过发出HTTP GET到端点(/ job / project-name / job / job-name / build)来触发管道。

However, I found out that I can always trigger by issue HTTP POST to the end point. 但是,我发现我总是可以通过发出HTTP POST到终点来触发。

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, 问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM