Jenkins pipeline 08 when 阶段运行控制
when 阶段运行控制
判断条件
• 根据环境变量判断
• 根据表达式判断
• 根据条件判断(not/allOf/anyOf)
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'build......'
}
}
stage('Deploy') {
when {
environment name: 'DEPLOY_TO', value: 'DEV'
}
steps {
echo 'Deploying.......'
}
}
}
}
### allOf 条件全部成立
when {
allOf {
environment name: 'CAN_DEPLOY', value: 'true'
environment name: 'DEPLOY_ENV', value: 'dev'
}
}
### anyOf 条件其中一个成立
when {
anyOf {
environment name: 'CAN_DEPLOY', value: 'true'
environment name: 'DEPLOY_ENV', value: 'dev'
}
}
when是去控制的,什么时候去执行,什么时候去不执行,比如通过input去拿到选项参数.
when就是根据变量或者表达式等等其他条件去判断阶段能不能运行.
这里使用环境变量去测试
environment{
ENV_TYPE = "DEV"
description = "版本号"
}
stage('deploy') {
when {
environment name: 'ENV_TYPE', value: 'DEV'
}
steps {
script{
echo "deploy dev"
}
}
}
这样就会先去判断变量是否成立,成立了之后才会去运行
成功
[Pipeline] echo (hide)
deploy dev
[Pipeline] }
[Pipeline] // script
失败
[Pipeline] { (deploy)
Stage "deploy" skipped due to when conditional
目录 返回
首页