有没有办法将Jenkinsfile中的整个post {}构建部分移动到全局管道库?

问题描述 投票:2回答:1

我对Jenkins管道比较陌生,但已经实现了一些,我已经意识到我需要在生气之前开始使用jenkins共享库。

已经弄清楚如何在库中定义一些重复的步骤并用Jenkinsfile中的杂乱来调用它们,但不确定是否可以对整个post build部分做同样的事情(我以为我已经读过如何使用define the entire pipeline in the libsimilar),因为这是每个管道代码的静态结束:

@Library('jenkins-shared-library')_
pipeline {
    agent none
    stages {
        stage ('System Info') { agent any
            steps { printSysInfo() }
        }

        stage ('Init'){ agent {label 'WinZipSE'}
            steps { init('SCMroot') }
        }
        stage('Build') { agent any 
            steps { doMagic() }
        }
    }

    // This entire 'post {}' section needs to go to a shared lib
    // and be called just with a simple methed call, e.g.
    // doPostBuild()
    post {
        always {
            node ('master') {
                googlechatnotification (
                message: '[$BUILD_STATUS] Build $JOB_NAME $BUILD_NUMBER has finished',
                url: 'id:credential_id_for_Ubuntu')

                step (
                    [$class: 'Mailer',
                    recipients: '[email protected] [email protected]',
                    notifyEveryUnstableBuild: true,
                    sendToIndividuals: true]
                )
            }
        }
        success {
            node ('master') {
                echo 'This will run only if successful'
            }
        }
        failure {
            node ('master') {
                echo 'This will run only if failed'
            }
        }
        // and so on
    }
}

我只是不知道如何在语法上实现这一点。当然,我可以将整个post构建部分定义为lib / var,如:doPotBuild.groovy

def call () {
  post {...}
}

但是我最终会如何在我定义的post {}构建块部分(AKA阶段)之外的Jenkins文件中调用它。

我可以在一些stage('post build){doPostBuild()}中调用它,但它不会起到真正的post {}部分应该如何工作的方式,例如如果之前的某个阶段出现故障,它将无法执行。

有什么想法,主要是工作的例子吗?

jenkins-pipeline post-build-event jenkins-shared-libraries
1个回答
0
投票

我并不完全是因为我不使用声明性管道,所以我不确定顶层结构有多严格。但我会回到脚本块。

@Library('jenkins-shared-library')_
pipeline {
    agent none
    stages {
        stage ('System Info') { agent any
            steps { printSysInfo() }
        }

        stage ('Init'){ agent {label 'WinZipSE'}
            steps { init('SCMroot') }
        }
        stage('Build') { agent any 
            steps { doMagic() }
        }
    }

    // This entire 'post {}' section needs to go to a shared lib
    // and be called just with a simple methed call, e.g.
    // doPostBuild()
    script {
        doPostBuild()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.