Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Release Plugin use in Jenkins Pipeline

Tags:

I'm using Jenkins Pipeline to automatically build and deploy my Java apps. I also use maven-release-plugin to perform Maven deploy to Artifactory.

The problem is my Jenkinsfile (or Jenkins Pipeline Configuration) :

  1. We commit a version 0.1.00-SNAPSHOT on release branch
  2. Jenkins Pipeline get the code, and perform maven release
  3. Maven Release changes the version to 0.1.00
  4. Maven Release tags GIT branch, commit and deploy the artifact
  5. Maven Release changes the version to 0.2.00-SNAPSHOT and commit
  6. Jenkins Pipeline detect a change in GIT, so triggers a new build

You understood that the last step creates an infinite loop, even if there is no useful commit.

Here is the interesting part of my Jenkinsfile :

sshagent([git_credential]) {     sh "${maven_bin} --settings ${maven_settings} -DreleaseVersion=${release_version} -DdevelopmentVersion=${development_version} release:prepare release:perform -B" } 

How can I break the loop (avoid Jenkins to trigger new build when Maven commits on GIT)?

Thanks

like image 335
frinux Avatar asked Aug 26 '16 08:08

frinux


People also ask

What is Maven release plugin used for?

This plugin is used to release a project with Maven, saving a lot of repetitive, manual work. Releasing a project is made in two steps: prepare and perform. Note: Maven 3 users are encouraged to use at least Maven-3.0.

How do you release in Jenkins?

To run a release, click the Release icon from the job home page. This will bring you to the release details page where you will be prompted to fill in any parameters that you have defined (or the default RELEASE_VERSION and DEVELOPMENT_VERSION if there were no parameters defined).

How does Jenkins release Maven?

On the job configuration page, enable the "Maven release build" under the Build Environment heading and add whatever release goals and options your require.


2 Answers

IMHO with the advent of git and pull requests, I don't think using maven-release-plugin or maven-version-plugin with a Jenkins pipeline is a good idea.

Using Multibranch Pipeline with the versioning technique mentioned here is more in line with continuous delivery: https://axelfontaine.com/blog/dead-burried.html

Using the versioning technique above, the pom.xml now looks like this:

<project>     ...     <version>${revision}</version>      <properties>         <!-- Sane default when no revision property is passed in from the commandline -->         <revision>0-SNAPSHOT</revision>     </properties>      <scm>         <connection>scm:git:your-git-repo-url</connection>     </scm>      <distributionManagement>         <repository>             <id>artifact-repository</id>             <url>your-artifact-repo-url</url>         </repository>     </distributionManagement>      <build>         <plugins>             <plugin>                 <artifactId>maven-scm-plugin</artifactId>                 <version>1.9.5</version>                 <configuration>                    <tag>${project.artifactId}-${project.version}</tag>                 </configuration>             </plugin>         </plugins>     </build>     ... </project> 

You can now produce releases on your Jenkins server very easily by configuring a Multibranch Pipeline with a Jenkinsfile to build on all branches and deploy only from master branch:

pipeline {   agent any   environment {     REVISION = "0.0.${env.BUILD_ID}"   }   triggers {     pollSCM('')   }   options {     disableConcurrentBuilds()     buildDiscarder(logRotator(numToKeepStr: '30'))   }   tools {     maven '3.5.2'     jdk 'jdk8'   }   stages {     stage ('Initialize') {       steps {         sh '''           echo "PATH = ${PATH}"           echo "M2_HOME = ${M2_HOME}"         '''       }     }     stage ('Build') {       steps {         sh 'mvn clean package'       }     }     stage ('Deploy') {       when {         branch 'master'       }       steps {         script {           currentBuild.displayName = "${REVISION}"         }         sh 'mvn deploy scm:tag -Drevision=${REVISION}'       }     }   } }  

See https://jenkins.io/blog/2017/02/07/declarative-maven-project/#set-up on how to configure a Multibranch Pipeline.

With this technique you develop only on non-master branches. Then create a pull request to merge your changes back to master branch. This should then deploy your artifact automatically to your artifact repository.


Addendum

When publishing to a Maven repository using the above method, the pom.xml will not have the proper version. To get Maven to publish the proper version, use the flatten-maven-plugin: http://www.mojohaus.org/flatten-maven-plugin/usage.html.

Also, check out: https://maven.apache.org/maven-ci-friendly.html

like image 168
Dennis Hoer Avatar answered Nov 07 '22 13:11

Dennis Hoer


Thanks to @Daniel Omoto comment, I found out that Jenkins provides option for GIT polling. One is exactly what I needed (and the provided example is for maven-release-plugin!):

GIT poll screenshot

like image 38
frinux Avatar answered Nov 07 '22 13:11

frinux