简体   繁体   中英

Declarative Jenkins Pipeline: How can I iterate the same stage for different repositories?

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'
            }
        }                                   
    }
}

}

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'
          }
        }
      }                                   
    }
  }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM