Kev*_*chs 4 jenkins jenkins-pipeline
我想创建一个 Jenkins (v2.126) 声明性语法管道,它具有使用 when() 子句检查环境变量值的阶段。具体来说,我想设置一个 Jenkins 作业参数(因此“使用参数构建”,而不是管道参数)并确定是否执行了一个阶段。
我有这样的阶段代码:
stage('plan') {
when {
environment name: ExecuteAction, value: 'plan'
}
steps {
sh 'cd $dir && $tf plan'
}
}
Run Code Online (Sandbox Code Playgroud)
参数名称是 ExecuteAction。但是,当 ExecuteAction 通过作业“选择”参数设置为:计划时,此阶段不会运行。通过添加此调试阶段,我可以看到通过环境变量输入了适当的值:
stage('debug') {
steps {
sh 'echo "ExecuteAction = $ExecuteAction"'
sh 'env'
}
}
Run Code Online (Sandbox Code Playgroud)
我得到这样的控制台输出:
[Pipeline] stage
[Pipeline] { (debug)
[Pipeline] sh
[workspace] Running shell script
+ echo 'ExecuteAction = plan'
ExecuteAction = plan
[Pipeline] sh
[workspace] Running shell script
+ env
...
ExecuteAction=plan
...
Run Code Online (Sandbox Code Playgroud)
我正在使用Jenkins 书籍管道语法中的 when 声明性语法,大约在页面中间,在 when 部分下,内置条件。
Jenkins 在 Gnu/Linux 上运行。
任何想法我可能做错了什么?
我相信您需要使用params而不是environment。请尝试以下操作:
when {
expression { params.ExecuteAction == 'plan' }
}
Run Code Online (Sandbox Code Playgroud)
呸!您需要在 when 子句中引用环境变量的名称。
stage('plan') {
when {
environment name: 'ExecuteAction', value: 'plan'
}
steps {
sh 'cd $dir && $tf plan'
}
}
Run Code Online (Sandbox Code Playgroud)