将Jenkins Git轮询限制为一个分支 [英] Limit Jenkins Git polling to one branch

查看:92
本文介绍了将Jenkins Git轮询限制为一个分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置当前Jenkins Pipeline作业以构建从Git签出的分支.要进行结帐,我们使用SCM插件:

Out current Jenkins Pipeline job is setup to build a branch checked out from Git. To do the checkout we use the SCM plugin:

  triggers {
    pollSCM scmpoll_spec: ''
  }

  checkout(
    poll: true,
    scm: [$class: 'GitSCM',
      branches: [[name: 'refs/heads/develop']],
      userRemoteConfigs: [
        [url: 'https://git-server/repo.git',
          name: 'origin',
          refspec: '+refs/heads/develop:refs/remotes/origin/develop',
          credentialsId: 'XXX']
      ],
      extensions: [
        [$class: 'WipeWorkspace'],
        [$class: 'CloneOption', honorRefspec: true, noTags: true, reference: '', shallow: false],
        [$class: 'LocalBranch', localBranch: 'develop']
      ],
      browser: [$class: 'GitList', repoUrl: 'https://git-server/gitlist/repo.git']
    ]
  )

在构建过程中,调用了 npm版本补丁,该补丁更新了 package.json 文件,在本地提交并创建了一个标签.然后,我们将其推回服务器端git存储库.为了阻止Jenkins开始另一个构建,我们使用options进行推送,并且post-receive挂钩将忽略这些推送:

During the build there is a call to npm version patch which updates the package.json file, commits and creates a tag locally. We then push this back to the server side git repository. In order to stop Jenkins from starting another build, we push with options and the post-receive hook ignores these pushes:

git push origin develop --follow-tags --push-option=nobuild

当用户按下时,服务器上的后接收挂钩仅POST到Jenkins,因为他们不会使用以下选项:

The post-receive hook on the server only POSTs to Jenkins when a user pushes as they wont use the options:

"https://jenkins-server/git/notifyCommit?url=https://git-server/repo.git"

这一切都很好,但是,问题是,当开发人员提交到 feature 分支时,将开始 develop 分支的构建.我猜这是问题的原因:

This is all working wonderfully, however, problem is that when a developer commits to feature branch, a build for the develop branch is started. I'm guessing the following is the cause of the problem:

  1. 提交/推送到 develop 分支
  2. 在构建 develop 分支期间创建的
  3. 标记
  4. 在功能分支上提交/推送
  5. Jenkins在开发 branch 上看到新标签并开始构建
  1. commit/push to develop branch
  2. tag created during building develop branch
  3. commit/push on feature branch
  4. Jenkins sees new tag on develop branch and starts a build

因此,我正在寻找一种方法来强制Jenkins仅考虑对特定分支的提交/推送以触发特定构建.或者在考虑开始构建时强制Jenkins忽略作为标签一部分的更改.

So I'm looking for a way to either force Jenkins to only consider commits/pushes to a specific branch to trigger a specific build. Or to force Jenkins to ignore changes as part of a tag when considering starting a build.

注意,我发现了另一个SO帖子:

Note, I found another SO post: Jenkins git commit for specific branch triggers build jobs for other branches too

推荐答案

尽管看似不在文档中,但再次寻找答案时,我遇到了这张Jenkins票证:

Though seemingly not in the documentation, looking for answers again I came across this Jenkins ticket: JENKINS-29574 - notifyCommit branch parameter is ignored. It seems you can add an extra parameter to the URL: &branches=<branch-name>

作为Git post-receive 钩子脚本的一部分,我可以找到分支名称并将其用作POST的一部分:

As part of the Git post-receive hook script I can find the branch name and use it as part of the POST:

https://jenkins-server/git/notifyCommit?url=https://git-server/repo.git&branches=develop

这现在仅使用该特定分支开始构建.

This now only kicks off the builds using that specific branch.

这篇关于将Jenkins Git轮询限制为一个分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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