0

I have several maven projects which are all treated in the same way to make a release.

Is it possible to reuse the same stage and iterate it just with a different repository name to clone?

stage('Maven_microservices') {
steps {

    checkout([$class: 'GitSCM', 
        branches: [[name: "*/${env.BRANCH}"]], 
        doGenerateSubmoduleConfigurations: false, 
        extensions: [[$class: 'CleanBeforeCheckout'], 
        [$class: 'RelativeTargetDirectory', relativeTargetDir: 'maven_microservice_1']], 
        submoduleCfg: [], 
        userRemoteConfigs: [[credentialsId: 'autouser',
        url: 'ssh://mygerrit:29418/maven/maven_microservice_1']]])

    container('maven') {                 
        configFileProvider([configFile(fileId: 'maven_settings', variable: 'MAVEN_SETTINGS')]) {
            dir('maven_microservice_1') {
                sh 'mvn -s $MAVEN_SETTINGS versions:update-parent'
                sh 'mvn -s $MAVEN_SETTINGS versions:resolve-ranges'
                sh 'mvn -s $MAVEN_SETTINGS versions:use-releases'
                sh 'mvn -s $MAVEN_SETTINGS --batch-mode release:prepare'
            }
        }                                   
    }
}

}

echedey lorenzo
  • 377
  • 1
  • 5
  • 19

2 Answers2

1

Not 100% sure if this answer is what you need, but do you mean something like this?

['maven_microservice_1', 'maven_microservice_2'].each { projectName ->

  stage("${projectName}") {
    steps {

      checkout([$class: 'GitSCM', 
        branches: [[name: "*/${env.BRANCH}"]], 
        doGenerateSubmoduleConfigurations: false, 
        extensions: [[$class: 'CleanBeforeCheckout'], 
        [$class: 'RelativeTargetDirectory', relativeTargetDir: "${projectName}"]], 
        submoduleCfg: [], 
        userRemoteConfigs: [[credentialsId: 'autouser',
        url: "ssh://mygerrit:29418/maven/${projectName}"]]])

      container('maven') {                 
        configFileProvider([configFile(fileId: 'maven_settings', variable: 'MAVEN_SETTINGS')]) {
          dir("${projectName}") {
            sh 'mvn -s $MAVEN_SETTINGS versions:update-parent'
            sh 'mvn -s $MAVEN_SETTINGS versions:resolve-ranges'
            sh 'mvn -s $MAVEN_SETTINGS versions:use-releases'
            sh 'mvn -s $MAVEN_SETTINGS --batch-mode release:prepare'
          }
        }
      }                                   
    }
  }
}
nogenius
  • 574
  • 1
  • 6
  • 18
  • It looks very good, I'm going to try and will let you know. Thanks so much nogenius :) – echedey lorenzo May 10 '19 at 12:37
  • It fails with the below error, does this syntax work in a declarative pipeline? Thanks! org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 60: expecting ')', found ',' @ line 60, column 34. ('maven_microservice_1', 'maven_microservice_2', 'maven_microservice_3', 'maven_microservice_4').each { projectName -> – echedey lorenzo May 10 '19 at 13:21
  • Sorry, I think it is supposed to be a square bracket [...]. I corrected it above – nogenius Sep 03 '19 at 11:22
  • Thanks, going to try again. – echedey lorenzo Sep 03 '19 at 14:41
1

Use of Jenkins Declarative Shared library can resolve this. Refer Extending with Shared Libraries

Create GenericMavenRelease.groovy in the library - vars folder and embed this code

def call(body){
    //evaluate the body block, and collect configuration into the object
    def config = [:]
    def builder
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()


    pipeline{
        agent any
        stages{
            stage('Maven_Microservices'){
                steps{
                        checkout([$class: 'GitSCM', 
                        branches: [[name: "*/${env.BRANCH}"]], 
                        doGenerateSubmoduleConfigurations: false, 
                        extensions: [[$class: 'CleanBeforeCheckout'], 
                        [$class: 'RelativeTargetDirectory', relativeTargetDir: "${projectName}"]], 
                        submoduleCfg: [], 
                        userRemoteConfigs: [[credentialsId: 'autouser',
                        url: "ssh://mygerrit:29418/maven/${projectName}"]]])

                        container('maven') {                 
                        configFileProvider([configFile(fileId: 'maven_settings', variable: 'MAVEN_SETTINGS')]) {
                          dir("${projectName}") {
                            sh 'mvn -s $MAVEN_SETTINGS versions:update-parent'
                            sh 'mvn -s $MAVEN_SETTINGS versions:resolve-ranges'
                            sh 'mvn -s $MAVEN_SETTINGS versions:use-releases'
                            sh 'mvn -s $MAVEN_SETTINGS --batch-mode release:prepare'
                          }
                        }
                }
            }
            }
    }

}

Create Jenkinsfile as below in each repository

@Library('maven-library@1.0.0') _
GenericMavenRelease {
}

This way you follow DRY - Do Not Repeat Yourself and build the code with multiple repository with single pipeline code

Girish
  • 171
  • 2
  • 9