git 如何使用 Gitlab Webhook 触发 Jenkins 多分支流水线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40979405/
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 trigger Jenkins Multi-Branch Pipeline with Gitlab Webhook
提问by Michael Lihs
I currently set up a Jenkins Multi-Branch Pipeline job that is based on a Git repository hosted on our Gitlab server. Jenkins can read the branches in the repository and creates a job for every branch in the repository. But I can't figure out how to trigger the jobs with webhooks in Gitlab.
我目前设置了一个基于 Gitlab 服务器上托管的 Git 存储库的 Jenkins 多分支流水线作业。Jenkins 可以读取存储库中的分支并为存储库中的每个分支创建一个作业。但我不知道如何在 Gitlab 中使用 webhooks 触发作业。
My questions are:
我的问题是:
How can I trigger the creation of a new branch jobin Jenkins from our Gitlab server? I can't see a webhook for a new branch being pushed.
How do I trigger the actual build jobfor a single branch? I can only add a webhook for push events but then I would have to add the branch name which I don't know how to do.
How can I make sure that Gitlab always triggers the "creation of the branch job" before a push to a branch triggers the build job itself.
如何从我们的 Gitlab 服务器触发在 Jenkins 中创建新的分支作业?我看不到正在推送的新分支的 webhook。
如何触发单个分支的实际构建作业?我只能为推送事件添加一个 webhook,但随后我将不得不添加我不知道该怎么做的分支名称。
在推送到分支触发构建作业本身之前,如何确保 Gitlab 始终触发“创建分支作业”。
What I tried so far is triggering the multi-branch job, but this has no effect and following this postdoes not work at all.
到目前为止,我尝试过的是触发多分支作业,但这没有任何效果,并且遵循这篇文章根本不起作用。
采纳答案by Antoine
You need to install Gitlab Pluginon Jenkins.
您需要在 Jenkins 上安装Gitlab 插件。
This will add a /project
endpoint on Jenkins. (See it in Jenkins => Administrate Jenkins => Configure System => Gitlab )
这将/project
在 Jenkins 上添加一个端点。(参见 Jenkins => 管理 Jenkins => 配置系统 => Gitlab )
Now add a webhook on your Gitlab project => Settings => Integrations. (or in older Gitlab version : Gitlab project => Wheel icon => Integrations, it seems you need to be owner of project in this case)
现在在您的 Gitlab 项目 => 设置 => 集成中添加一个 webhook。(或在旧版 Gitlab 中:Gitlab 项目 => 轮子图标 => 集成,在这种情况下,您似乎需要成为项目的所有者)
In Url : set http://*yourjenkins.com*/**project**(/*foldername*)?/*yourprojectname*
and that's all, and click on "Add Webhook".
在 Url : set 中http://*yourjenkins.com*/**project**(/*foldername*)?/*yourprojectname*
,仅此而已,然后单击“添加 Webhook”。
When you click on test on your webhook, it should trigger your jenkins pipeline build. (You should have a 200 http response)
当您单击 webhook 上的 test 时,它应该会触发您的 jenkins 管道构建。(你应该有一个 200 http 响应)
It works without authentication in Gitlab plugin, configuration with authentication are welcome.
它在 Gitlab 插件中无需身份验证即可工作,欢迎配置身份验证。
回答by tbradt
This is a longer response to the question comment above about merge requests, and due to length I'm posting it as an answer rather than a comment.
这是对上述关于合并请求的问题评论的较长回复,由于篇幅原因,我将其发布为答案而不是评论。
Multibranch pipeline does not support Gitlab merge request hooks. More accurately, the gitlab plugin does not support this. If you add a merge request webhook and test it you will get the response..."Merge Request Hook is not supported for this project". Going to the documentation you find...
多分支管道不支持 Gitlab 合并请求挂钩。更准确地说,gitlab 插件不支持这一点。如果您添加合并请求 webhook 并对其进行测试,您将得到响应...“此项目不支持合并请求钩子”。转到您找到的文档...
"Pipeline Multibranch jobs
"管道多分支作业
Note: There is no way to pass external data from GitLab to a Pipeline Multibranch job, so the GitLab environment variables are not populated for this job type. GitLab will just trigger branch indexing for the Jenkins project, and Jenkins will build branches accordingly without needing e.g. the git branch env var. Due to this, the plugin just listens for GitLab Push Hooks for multibranch pipeline jobs; merge Request hooks are ignored." https://github.com/jenkinsci/gitlab-plugin#pipeline-multibranch-jobs
注意:无法将外部数据从 GitLab 传递到 Pipeline Multibranch 作业,因此不会为该作业类型填充 GitLab 环境变量。GitLab 只会触发 Jenkins 项目的分支索引,Jenkins 将相应地构建分支,而不需要例如 git branch env var。因此,该插件只为多分支管道作业监听 GitLab Push Hooks;合并请求挂钩被忽略。” https://github.com/jenkinsci/gitlab-plugin#pipeline-multibranch-jobs
Very unfortunate. You can at least create a classic job of type "Pipeline", and use the "Pipeline script from SCM" option which will rely on a Jenkinsfile in the repo. This will support scripted or declarative, or a blend of the 2. This means you can point this at a Jenkinsfile you've already created in Blue Ocean (only to find that you can't do a merge request for this because a pipeline created in Blue Ocean is of course always multibranch). You can then configure this classic pipeline job to trigger on merge requests using the job configuration. You will need to use the plugin steps in the script to updateGitlabCommitStatus and acceptGitLabMR as required. This does at least result in the ability to view the pipeline runs in the more modern Blue Ocean UI. And the pipeline link in your Gitlab MR will also direct you to the same UI.
很不幸。您至少可以创建一个“管道”类型的经典作业,并使用“来自 SCM 的管道脚本”选项,该选项将依赖于 repo 中的 Jenkinsfile。这将支持脚本式或声明式,或两者的混合。这意味着您可以将其指向您已经在 Blue Ocean 中创建的 Jenkinsfile(只会发现您无法为此执行合并请求,因为创建了一个管道在蓝海当然总是多分支)。然后,您可以配置此经典管道作业以使用作业配置触发合并请求。您需要使用脚本中的插件步骤来根据需要更新GitlabCommitStatus 和acceptGitLabMR。这至少会导致在更现代的 Blue Ocean UI 中查看管道运行的能力。