Har*_*ran 7 groovy continuous-deployment jenkins jenkins-groovy jenkins-pipeline
我试图跳过stage基于常规变量,该变量值将在另一个阶段计算。
在下面的示例中,Validate阶段是基于环境变量有条件地跳过的VALIDATION_REQUIRED,我将在构建/触发作业时传递该环境变量。---这按预期工作。
Build而即使isValidationSuccess变量设置为 ,阶段也始终运行false。我尝试更改when条件表达式,例如{ return "${isValidationSuccess}" == true ; } or{ return "${isValidationSuccess}" == 'true' ; }但没有成功。打印变量时它显示为“false”
def isValidationSuccess = true
pipeline {
agent any
stages(checkout) {
// GIT checkout here
}
stage("Validate") {
when {
environment name: 'VALIDATION_REQUIRED', value: 'true'
}
steps {
if(some_condition){
isValidationSuccess = false;
}
}
}
stage("Build") {
when {
expression { return "${isValidationSuccess}"; }
}
steps {
sh "echo isValidationSuccess:${isValidationSuccess}"
}
}
}
Run Code Online (Sandbox Code Playgroud)
when评估条件。when?when选项看起来很干净。此外,stage view当跳过该特定阶段时,显示效果也很好。script {
if(isValidationSuccess){
// Do the build
}else {
try {
currentBuild.result = 'ABORTED'
} catch(Exception err) {
currentBuild.result = 'FAILURE'
}
error('Build not happened')
}
}
Run Code Online (Sandbox Code Playgroud)
参考文献: https://jenkins.io/blog/2017/01/19/converting-conditional-to-pipeline/
stage("Build") {
when {
expression { isValidationSuccess == true }
}
steps {
// do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
when验证布尔值,因此应评估为 true 或 false。
| 归档时间: |
|
| 查看次数: |
9568 次 |
| 最近记录: |