Jenkins pipeline, bitbucket hook and maven release plugin infinite loop

纵然是瞬间 提交于 2020-02-02 10:46:11

问题


I haven't been able to find any info about this, so i hope you guys can help me on this one

I've a maven project hosted in bitbucket that has a BitBucket WebHook pointing to someurl/bitbucket-hook/ , this hooks triggers the build of my project that is defined by a pipeline that has this structure:

node {
   stage 'Checkout'
   git url: 'https:...'

   def mvnHome = tool 'M3'

   #Various stages here
   ...
   stage 'Release'
   sh "${mvnHome}/bin/mvn -B clean install release:prepare release:perform release:clean"
}

the problem is that maven release plugin pushes changes to BitBucket, and this triggers again the jenkins script, making an infinite loop of builds, is there a way to prevent this?

I've tried setting a quiet period in Jenkins with no success


回答1:


From my perspective you should have specific jobs for build and release, and the release job should be triggered manually. Anyway, if there is some reason to have them in the job you can check for the message of the last commit:

node {
  git 'https...'
  sh 'git log -1 > GIT_LOG'
  git_log = readFile 'GIT_LOG'
  if (git_log.contains('[maven-release-plugin]')) {
    currentBuild.result = 'ABORTED'
    return
  }
  ... // continue with release or whatever

}



回答2:


A New Way to Do Continuous Delivery with Maven and Jenkins Pipeline article approach solves the infinite loop:

Use the Maven release plugin to prepare a release with pushChanges=false (we are not going to push the release commits back to master) and preparationGoals=initialize (we don't care if the tag is bad as we will only push tags that are good)

sh "${mvnHome}/bin/mvn -DreleaseVersion=${version} -DdevelopmentVersion=${pom.version} -DpushChanges=false -DlocalCheckout=true -DpreparationGoals=initialize release:prepare release:perform -B"



回答3:


Another solution can be to change the git hook (post-receive) and add a conditional curl similar to this script:

#!/bin/bash
git_log=$(git log --branches -1)
if ! [[ $git_log =~ .*maven-release-plugin.* ]] ;
then
curl http://buildserver:8080/git/notifyCommit?url=ssh://git@server/projects/name.git;
fi


来源:https://stackoverflow.com/questions/38270738/jenkins-pipeline-bitbucket-hook-and-maven-release-plugin-infinite-loop

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