仅在签入该分支时尝试触发分支的构建

时间:2013-09-16 21:57:59

标签: git jenkins

我们有10-15个git分支,但我们只想在其中2个(“master”和“beta1”)上触发Jenkins作业。我正在使用Git Plugin用于Jenkins,并在“要构建的分支”部分中指定“master”和“beta1”。我还在“repo(可选)的Local子目录”字段中指定$ {GIT_BRANCH}。

我想在主分支上出现签到时,仅在主分支 上触发Jenkins作业

我想在beta1分支上出现checkin时,仅在beta1分支 上触发Jenkins作业

我希望这两个分支都由同一个Jenkins作业控制,以便构建号在两个分支之间是唯一的(如果它们在两个不同的作业中,构建号可以是相同的)。

目前,它似乎正在触发beta1分支机构的工作,以便从主分支机构办理登机手续。从日志中(注意:下面日志中第一行中报告的更改来自主分支):

Started by remote host (IP) with note: Triggered by push of revision e4391d0049ff: "blah" to (URL) by (USER)
Building in workspace /var/lib/jenkins/jobs/Orca/workspace
Checkout:workspace / /var/lib/jenkins/jobs/Orca/workspace - hudson.remoting.LocalChannel@3ae1a582
Using strategy: Default
Last Built Revision: Revision 7e2aae6c752a16516d9f6ac48944492a4e3596d4 (origin/master)
Wiping out workspace first.
Cloning the remote Git repository
Cloning repository (SSH)
git --version
git version 1.7.4.1
Fetching upstream changes from origin
Cleaning workspace
Resetting working tree
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/beta1
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/(BLAH)
Seen branch in repository origin/master
Seen branch in repository origin/(BLAH)
Seen 22 remote branches
Multiple candidate revisions
Scheduling another build to catch up with Orca
Commencing build of Revision eaad42c836a87672c546d61f310cc31bf03ecb97 (origin/beta1)
Checking out Revision eaad42c836a87672c546d61f310cc31bf03ecb97 (origin/beta1)

3 个答案:

答案 0 :(得分:3)

如果你这样做:

  

在“要构建的分支”部分中指定“master”和“beta1”

并且您正在基于轮询Git触发构建,然后Jenkins将在其中任何一个分支中发现更改时触发该作业。这是预期的功能,据我所知,我们不支持您正在寻找的配置。

我可以建议两种可能的解决方案:

  1. 使用两个单独的Jenkins作业。这就是我们如何做到这一点,如果我们需要保持构建数字离散,那么我们只需将构建数字分开一个大数字。例如,您可以在其中一个作业上将下一个内部版本号设置为300,000。

  2. 创建两个作业,一个跟踪每个分支但实际上没有构建任何内容的作业。使用Parameterized Trigger Plugin使用Git分支参数触发相同的下游作业。

答案 1 :(得分:0)

另一种方法是使用git的post commit hooks。这也可以让你更快地开始构建而不是轮询。


对Jenkins进行以下更改:

  1. 确保您已安装Parameterized Build plugin
  2. 配置要参数化的作业。考虑命名此参数“BRANCH_TO_BULD”。考虑默认值“master”
  3. 将源代码管理设置为“无”
  4. 禁用轮询
  5. 在第一个构建步骤中,执行以下shell命令:
  6. cd /path/to/your/workspace/SpecialJob 
    git reset --hard 
    git clean -f -d 
    git fetch origin 
    git checkout $BRANCH_TO_BUILD  
    



    对Git进行以下更改:

    1. 首先导航到来源 git存储库根目录中的hooks目录
    2. 将post-receive.sample重命名为post-receive
    3. 编辑文件的内容以包含以下内容(更改MyJenkinsBox& MySpecialJob):
    4. echo  
      # if you want to send some fancy text to the committer's console output.
      echo Post Receive Hook  
      echo  
      while read oldrev newrev refname  
      do  
          branch=$(git rev-parse --symbolic --abbrev-ref $refname)  
          # only build for master  
          if [ "master" == "$branch" || "beta1" == "$branch" ]; then  
              # more valuable feedback to the committer's console output  
              echo Building MySpecialJob job on Jenkins. Branch = $branch  
              echo  
              curl -s -X POST http://MyJenkinsBox:8080/job/MySpecialJob/buildWithParameters?BRANCH_TO_BUILD=$branch  
              echo  
          fi
      done
      

答案 2 :(得分:0)

一个很老的帖子,但是如果仍然有人在寻找这个问题,这里是解决问题的方法。

https://issues.jenkins-ci.org/browse/JENKINS-22794

Jenkins网站上最近关于如何在EC2上解决该问题的评论很少,但我还没有亲自在AWS上进行过测试,因此有一些地方需要探讨一下它是否适用于您各自的场景。