在同一共享库的其他管道中“包含”共享库管道

时间:2019-09-30 07:13:09

标签: jenkins jenkins-pipeline jenkins-2 jenkins-shared-libraries

我有多个微服务,它们使用shared library中名为 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中的所有阶段,还将添加一些自己的阶段。目前,我们只是将这些阶段复制粘贴到这些其他管道中,

无效调用(地图管道属性= [:]){                 。                 。                 

    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块,另一个在 管道可能有自己的帖子块

0 个答案:

没有答案