Jenkins:Git推送将仅针对该分支触发Jenkins构建 [英] Jenkins: Git push will trigger a Jenkins build for only that branch

查看:222
本文介绍了Jenkins:Git推送将仅针对该分支触发Jenkins构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在许多Git分支上并行工作.

We are working in parallel on a number of Git branches.

当推送特定的Git分支时,我们如何启动将触发该特定分支构建的Jenkins项目/作业?

When pushing a specific Git branch, how can we start a Jenkins item/job that will trigger the build of that specific branch?

作为一个例子:我们推送一个Git分支"feature-abc" ==>,这应该触发使用/拉该分支"feature-abc"的Jenkins构建作业.

As an example: we push a Git branch "feature-abc" ==> this should trigger a Jenkins build job using/pulling that branch "feature-abc".

对于每个新功能,我们都有一个分支,因此分支的数量不是固定的.

For each new feature we have a branch, so the number of branches is not fixed.

因此,我认为这与通过手动操作进行的回答不同.

So, I think it is different than this answer with a manual action.

推荐答案

通过GUI构建Jenkins作业的更好的选择是将其作为Jenkins管道.

A much better alternative for building Jenkins jobs via the GUI is making them as Jenkins pipelines.

(下面)简单的演示配置的结果是,每次将更新推送到Git存储库时,都会启动该(更改的)分支的Jenkinsfile.在Jenkins文件中,您可以输出分支名称.

The result of the (below) simple demo configuration is that each time you push updates to the Git repo, a Jenkinsfile for that (changed) branch is started. In the Jenkinsfile you can output the branch name.

如何?您可以在几分钟内轻松地自己创建这样的配置.在这种情况下,我的本地PC上运行的是Jenkins服务器.因此,默认情况下,Git项目中的Webhook无法访问此Jenkins服务器. (注意:您可以使用Webhook中继来模拟真实的Webhook).当Git服务器可以访问Jenkins服务器时,则绝对应使用Webhook.

How? You can easily create such a configuration yourself in a few minutes. In this case I have a Jenkins server running on my local PC. So, this Jenkins server is by default not accessable for a webhook in the Git project. (Notice: you can use Webhook relay to simulate real webhooks). When the Git server can access the Jenkins server, then you should definitely use a webhook.

  • 创建一个项目并将其放入Git(lab)存储库中.
  • 将此简单的Jenkinsfile(管道)添加到您的项目中.

.

node {
    stage('Preparation') {
        checkout scm
    }
    stage('Who am i?') {
        echo "This job was triggered by a Git push to branch: ${env.BRANCH_NAME}"
    }
}

  • Jenkins>新项目>多分支管道.如果Git服务器无法访问您的Jenkins服务器,则可以使用轮询.否则,您可以通过Git服务器设置一个Webhook.
  • Jenkins现在将在Git项目中扫描具有"Jenkinsfile"的分支.对于每个匹配的分支(具有Jenkinsfile),它将自动创建Jenkins Pipeline作业.作业具有分支的名称.最初只有1个分支,因此将创建1个名称为"master"的作业.新创建的jenkins作业将为"master"分支执行"Jenkinsfile".在控制台中,您将看到带有分支名称:master的输出.
  • 创建一个分支,并对Jenkinsfile进行更改.将更改推送到分支.
  • 最多等待1分钟(由于轮询),多分支作业将扫描是否有带有Jenkinsfile的新分支.在这种情况下,将使用分支的名称创建一个新的jenkins作业.所有作业均在多分支作业下方可见.作业将被执行并打印出分支名称.
  • 如果仅对现有分支进行更改并将更改推送到Git项目,则将在1分钟内扫描分支. Jenkins将看到分支已更改,并开始Jenkins管道作业.
    • Jenkins > New Item > Multibranch pipeline. If your Jenkins server is not accessable for the Git server you can use polling. Otherwise you can setup a webhook via the Git server.
    • Jenkins will now scan the Git project for branches having a 'Jenkinsfile'. For each matching branch (having a Jenkinsfile) it will automatically create a Jenkins Pipeline job. The jobs have the name of the branch. At first there will be only 1 branch, so there will be 1 job with the name 'master' created. The newly created jenkins job will execute the 'Jenkinsfile' for the 'master' branch. In the console you will see the output with the name of the branch: master.
    • Create a branch and make a change to the Jenkinsfile. Push the changes to the branch.
    • After waiting max 1 minute (due to the polling) the multibranch job will scan whether there are new branches with a Jenkinsfile. In this case a new jenkins job is created with the name of the branch. All jobs are visible below the Multibranch job. The job will be executed and printout the branch name.
    • If you only make a change to an existing branch AND you push the change to the Git project, then within 1 minute the branches are scanned. Jenkins will see that the branch is changed and start the Jenkins pipeline job.
    • 这篇关于Jenkins:Git推送将仅针对该分支触发Jenkins构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆