Search code examples
gitmavenjenkinsjenkins-pipelinemaven-release-plugin

How to get latest git commit author name or message in jenkins multibranch scripted pipeline and use under branch condition


We use github organisation scan job in jenkins to auto scan all the related repos in the org based on certain name filters and auto triggers builds for specified branch regex. The pipeline works great and saves a ton of time having to manually configure jobs for each repo. All of these repos utilize maven build to built the snapshot and release artifacts. Since we are trying to automate everything including the release process, what we have now done is to have a single externalized jenkinsfile (scripted) that these repos utilize and that file has all conditions for different branches like dev, uat, release and master. The approach is code development begins in dev with a snapshot version and continues to deploy that until the release day is near and that point of time merge dev to release through PR, etc and that kicks off an automated release build under for the release branch for that repo job in jenkins. Release branch build basically uses maven release plugin to first edit the pom in the remote repository by removing -SNAPSHOT for the artifact version, then doing a deployment to artifactory and then again committing back to the github repository with the increment version in pom.xml in the same release branch. I have added an extra step in the jenkinsfile that pulls that latest release, merges to dev and then push back to remote dev so that dev has the correct increment SNAPSHOT version in pom.xml. All of this works fine and does the job. The issue is that when the release build kicks off and submits back two commits to the remote repo release branch (truncating -SNAPSHOT first and later adding incremented SNAPSHOT version in pom.xml), that in turns triggers another build which you have to manually stop else the loop will go endlessly. Our single jenkinsfile has all branch checks like :-

if (env.BRANCH_NAME == 'release')

Now is there a way to add an and condition in that if statement that does something like:- if latest checked out commit not has author "xyz" or its message !~ "[maven-release-plugin]*" That would mean if those auto commits are made by maven release plugin then the moment the next build is triggered, it wouldn't do anything since the if condition for the branch and the above extra condition would invalidate it. The question is how to achieve the above solution or what could be some similar alternatives here as.


Solution

  • I was able to achieve this using the below approach :-

    def author = sh(returnStdout: true, script: "git log -1 --pretty=format:'%an'").trim()
         echo "$author"
            
            if (env.BRANCH_NAME == 'release' && author != 'xyz')