1

我有几个微服务使用来自名为jenkins-shared-pipelines的共享库中的相同管道。微服务的Jenkinsfile如下:

@Library(['jenkins-shared-pipelines']) _

gradleProjectPrPipeline([buildAgent: 'oc-docker-jdk11', disableIntegrationTestStage: true])

在 jenkins-shared-pipelines/vars 中,gradleProjectPrPipeline 有以下几个阶段:

/**
 * gradleProjectPrPipeline is a generic pipeline
 * @param pipelineProperties map used to pass parameters
 * @return
 */
void call(Map pipelineProperties = [:]) {
            .
            .
            .

    pipeline {
        agent {
            node {
                label "${pipelineProperties.buildAgent}"
            }
        }

        options {
            skipDefaultCheckout true
            timeout(time: 1, unit: 'HOURS')
            buildDiscarder(logRotator(
                    numToKeepStr: '5',
                    daysToKeepStr: '7',
                    artifactNumToKeepStr: '1',
                    artifactDaysToKeepStr: '7'
            ))
        }

        stages {
            stage('Clone') {                
                steps {
                    //clone step
                }
            }
            stage('Compile') {                
                steps {
                    script {
                       /*Some custom logic*/
                    }
                    runGradleTask([task: 'assemble',
                                   rawArgs: defaultGradleArgs + " -Pcurrent_version=${releaseTag}"
                    ])
                }
            }
            stage('Tests') {
                parallel {
                    stage('Unit tests') {                        
                        steps {
                            //Unit tests
                        }
                    }
                    stage('Integration tests') {                        
                        steps {
                            //Integration tests
                        }
                    }
                }
            }
            stage('Sonar scan') {                
                steps {
                    //Sonar scanning
                }
            }

        }

        post {

            unsuccessful {
                script {
                    bitbucketHandler.notifyBuildFail([
                        displayName: pipelineName,
                        displayMessage: "Build ${env.BUILD_ID} failed at ${env.BUILD_TIMESTAMP}."
                    ])
                }
            }
            success {
                script {
                    bitbucketHandler.notifyBuildSuccess([
                        displayName: pipelineName,
                        displayMessage: "Build ${env.BUILD_ID} completed at ${env.BUILD_TIMESTAMP}."
                    ])
                }
            }
        }
    }
}

现在,除了上述管道之外,jenkins-shared-pipelines(在同一个 vars 目录下)中还会有几个管道,例如:awsPipeline、azurePipeline 等,它们也将包含部署阶段。这些额外的管道将需要上述 gradleProjectBranchWrapper 中的所有阶段,并且还将添加一些自己的阶段。目前,我们只需将这些阶段复制粘贴到这些额外的管道中,

无效调用(映射 pipelineProperties = [:]){ 。. .

    pipeline {
        agent {
            node {
                label "${pipelineProperties.buildAgent}"
            }
        }

        options {
            skipDefaultCheckout true
            timeout(time: 1, unit: 'HOURS')
            buildDiscarder(logRotator(
                    numToKeepStr: '5',
                    daysToKeepStr: '7',
                    artifactNumToKeepStr: '1',
                    artifactDaysToKeepStr: '7'
            ))
        }

        stages {
            stage('Clone') {                
                steps {
                    //clone step
                }
            }
            stage('Compile') {                
                steps {
                    script {
                       /*Some custom logic*/
                    }
                    runGradleTask([task: 'assemble',
                                   rawArgs: defaultGradleArgs + " -Pcurrent_version=${releaseTag}"
                    ])
                }
            }
            stage('Tests') {
                parallel {
                    stage('Unit tests') {                        
                        steps {
                            //Unit tests
                        }
                    }
                    stage('Integration tests') {                        
                        steps {
                            //Integration tests
                        }
                    }
                }
            }
            stage('Sonar scan') {                
                steps {
                    //Sonar scanning
                }
            }  
           stage('AWS'){

           }          
        }

        post {

            unsuccessful {
                script {
                    bitbucketHandler.notifyBuildFail([
                        displayName: pipelineName,
                        displayMessage: "Build ${env.BUILD_ID} failed at ${env.BUILD_TIMESTAMP}."
                    ])
                }
            }
            success {
                script {
                    bitbucketHandler.notifyBuildSuccess([
                        displayName: pipelineName,
                        displayMessage: "Build ${env.BUILD_ID} completed at ${env.BUILD_TIMESTAMP}."
                    ])
                }
            }
        }
    }
}

然后,我们从微服务调用这些新管道,例如:

@Library(['jenkins-shared-pipelines']) _

awsPipeline([buildAgent: 'oc-docker-jdk11', disableIntegrationTestStage: true])

很明显,由于克隆到 sonarScan 阶段很常见,因此存在代码冗余,但没有“基本管道”或其他方式将这些通用阶段包含在所有管道中。我想知道是否有办法“包含” gradleProjectPrPipeline(可以用作“基础管道”)管道,如 awsPipeline、azurePipeline 等。 笔记:

  • awsPipeline 等将使用工作区(克隆 stag 签出代码和后续阶段操作的地方)。换句话说,来自 gradleProjectBranchWrapper 的变量和结果应该可供 awsPipeline 等访问。
  • gradleProjectBranchWrapper 中有一个 post 块,其他管道可能有自己的 post 块
4

0 回答 0