如果 tag 出现在提交消息中,则跳过 jenkins 阶段

Skip jenkins stage if tag appears in the commit message

提问人:chocho.boss 提问时间:1/19/2023 更新时间:1/19/2023 访问量:409

问:

如果提交消息中有类似 #no_build 或 #no_unittest 的内容,我正在尝试跳过 Jenkins 管道中的某些阶段。

如果提交消息中存在 #no_unittest,我不想跳过 Unittest 阶段。

我看到有一个使用脚本的解决方案,但是否可以以声明性方式进行?

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'npm build'
            }
        }
        stage('Unittest') {
            when{
                branch 'master'
            }
            steps {
                sh 'python unittest.py'
            }
        }
    }
}

P.S. 标签应该区分大小写,“-”和“_”也应该被同等对待。

我尝试放置更改日志:#no_build但这并没有产生预期的效果。 我不确定如何解决“-”和“_”相等的问题。

git jenkins jenkins-pipeline git-commit 提交消息

评论


答:

1赞 ycr 1/19/2023 #1

您可以将 与条件一起使用。示例如下。changeLogwhen

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'npm build'
            }
        }
        stage('Unittest') {
            when{
                when { not { changelog '^.*#no_unittest.*$'} }
            }
            steps {
                sh 'python unittest.py'
            }
        }
    }
}

评论

1赞 chocho.boss 1/24/2023
非常感谢对我有帮助!此外,我设法更改了正则表达式,以满足我对“-”和“_”相同处理并使其不区分大小写的需要。这是我的正则表达式:^(?i).*#no[-_]build.*$
0赞 chocho.boss 1/30/2023
嘿@ycr!我有一个关于 .我正在尝试将变量中的正则表达式设置为 并将其用作 .但是 Jenkins 抛出了一个错误,说出来并指向 .你知道为什么会这样吗?据我所知,正则表达式是有效的。changelog '^.*#no_unittest.*$'def regex = /^.*#no_unittest.*$/changelog regex${regex} is not a valid regular expression. Illegal repetition near index 0changelog regex
0赞 ycr 1/30/2023
@K.Г.你为什么不创建一个包含所有细节的新 SO 问题呢?