Jenkins DSL-始终在内部进行状态检查

时间:2018-12-06 09:05:57

标签: jenkins jenkins-pipeline

我们有一个Jenkinsfile的DSL脚本。在其中配置了如下所示的备用消息。

....
....
pipeline {

    agent none

    stages {
        stage('Initialization') {
                     agent {
                         label 'master'
                     }
            when {
                beforeAgent true
                expression {
                                isMaster = (env.BRANCH_NAME == masterBranchName)
                                isPatch = (env.BRANCH_NAME ==~ /${patchBranchRegex}/)
                                isPR = !isMaster && !isPatch
                                echo "isMasterBranch : ${isMaster} , isPatchBranch : ${isPatch}, isPRBranch : ${isPR}"
                            return true
                           }
            }
            steps {
                    script {
                        message_default = "\nJob URL: <${env.RUN_DISPLAY_URL}|${serviceName}-pipeline>"
                        message_default = "${message_default} \nBranch : ${env.BRANCH_NAME}"
                        message_default = "${message_default} \nBuild# : ${env.BUILD_NUMBER}"

                        sh 'git rev-parse --short HEAD > commitID.txt'
                        gitCommitID = readFile('commitID.txt').trim()
                        sh 'git show -s HEAD | grep -Po \'(?<=(Author: )).*(?=<)\' > committer.txt'
                        gitCommitter = readFile('committer.txt').trim()
                        message_default = "${message_default} \nCommitID# : ${gitCommitID}"
                        message_default = "${message_default} \nCommitter : ${gitCommitter}"
                        message_final = message_default
                        }
                }
        }

        stage('Build Pull Req branch') {
        ....
        ....
        }
        ....
        ....
    post {
        always {
                slackSend channel: "${slackChannel}",
                          color: COLOR_MAP[currentBuild.currentResult],
                          message: "@here ${BUILD_TYPE_MAP[buildType]} ${message_final} \nStatus : ${currentBuild.currentResult} ${EMOJI_MAP[currentBuild.currentResult]}"
        }
    }
}

在这种情况下-我想添加一个条件,以检查构建是否已中止,如果是,则执行一些脚本,然后按用户名将中止的内容添加到最终消息中(添加为松弛状态)

post {
    always {

            when {
                ${currentBuild.currentResult} is ABORTED
                }
            do this {
                     script {
                        sh 'curl -s "${env.BUILD_URL}/consoleFull" | grep -Ei "Aborted by|Rejected by" | head -1 | awk -F"by " \'{print $2}\' > AbortedBy.txt'
                        abortedBy = readFile('AbortedBy.txt').trim()
                        message_final = "${message_final} \nAborted by : ${abortedBy}"
                    }
                }

            slackSend channel: "${slackChannel}",
                      color: COLOR_MAP[currentBuild.currentResult],
                      message: "@here ${BUILD_TYPE_MAP[buildType]} ${message_final} \nStatus : ${currentBuild.currentResult} ${EMOJI_MAP[currentBuild.currentResult]}"
    }
}


有可能吗?

注意:

  • 我也尝试添加post -> aborted {}-但是在那种情况下,它只能运行 aborted{}在某人中止作业后无法运行always{}

0 个答案:

没有答案