git 如何远程触发Jenkins多分支管道项目构建?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39490150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remotely trigger Jenkins multibranch pipeline project build?
提问by Marmoy
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.
“远程触发构建”构建触发选项似乎不起作用,因为没有保存您设置的令牌。
采纳答案by Marmoy
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.
目前(Jenkins 2.22)“远程触发构建”构建触发选项在多分支管道作业配置中可见,但不起作用(如果您检查它并指定一个令牌,它会在保存后重置)。根据这个,这是故意的触发器不能确定,但一个错误,它表现为在所有的选项。
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.
在同一线程中,他们解释了如何为多分支管道项目中的每个单独项目(分支)触发构建。我需要的是一个动态设置,它适用于在设置触发器后创建的分支,而不是来自线程的建议端点(/job/project-name/job-name/build
应该是/job/job-name/project-name/build
,因为项目是从作业中的分支创建的),我发现要使用的端点是/job/job-name/build
. 为了使其工作,您必须使用 API 令牌创建一个用户(转到管理 Jenkins -> 管理用户 -> 齿轮图标 -> 显示 API 令牌),并在您的请求中使用这些作为用户名和密码。
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 都不熟悉时,明确表示并没有什么坏处。
回答by Abhijit Sarkar
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:
我无法获得已接受答案中所述的 API 令牌,因为即使我以管理员身份登录,也没有名为“管理用户”的链接。相反,我得到了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
API 令牌在您的个人配置页面中可用。在每个页面的右上角单击您的姓名,然后单击“配置”以查看您的 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:
注意事项:
- If the pipeline or branch name contains special characters, you need to encode it; for example,
/
becomes%252F
. - The
token
query parameter is optional. - This answer has been verified in Jenkins v2.6.
- 如果管道或分支名称包含特殊字符,则需要对其进行编码;例如,
/
变成%252F
。 - 该
token
查询参数是可选。 - 此答案已在 Jenkins v2.6 中得到验证。
回答by Devesh Chanchlani
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.
用适当的值替换大写字母。
回答by Lam Ly
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).
在我的配置(Jenkins 2.60.2)中,我无法远程启用触发器构建(例如,从脚本)选项,因为我只能“查看配置”。这阻止了我通过向端点 (/job/project-name/job/job-name/build) 发出 HTTP GET 来触发管道。
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.
您必须在请求中提供有效的 crumb 或禁用“配置全局安全性”中的“防止跨站点请求伪造攻击”选项。
Regards,
问候,