Limit Jenkins Git polling to one branch

牧云@^-^@ 提交于 2021-02-10 15:47:32

问题


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']
    ]
  )

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

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"

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. 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

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.

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


回答1:


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>

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.



来源:https://stackoverflow.com/questions/64640148/limit-jenkins-git-polling-to-one-branch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!