詹金斯检查env.BRANCH_NAME是否在“帖子”部分中包含某些关键字

时间:2019-03-21 11:21:04

标签: jenkins jenkins-pipeline

我需要跳过某些包含“ HotFix”作为单词的分支。 Jenkins文件中是否可以包含以下内容?

post {
    success {
        withCredentials(some_details) {
            script {
                try {
                    if (!env.BRANCH_NAME.contains('HotFix')) {

                    }
                    else {

                    }
                }
                catch (err) {
                    echo    err
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

Jenkins声明性管道支持when directive,可以根据预定义条件跳过某些阶段。考虑以下示例:

pipeline {
    agent any

    stages {
        stage("A") {
            steps {
                // ....
            }
        }

        stage("B") {
            when {
                expression {
                    !env.BRANCH_NAME.contains("HotFix")
                }
            }
            steps {
                // ....
            }
        }
    }
}

在这种情况下,仅当当前分支名称不包含B时,我们才想执行阶段HotFix

相关问题