gpt4 book ai didi

jenkins - Jenkins 文件中单个阶段内的多个 when 子句

转载 作者:行者123 更新时间:2023-12-04 13:11:53 24 4
gpt4 key购买 nike

我想在一个阶段中放置多个 when 子句。这是基本示例

def foo = 0
pipeline {
agent any

stages {
stage('Hello') {
when {
expression {foo == 0}
}
steps {
echo 'foo is 0'
}
when {
expression {foo == 1}
}
steps {
echo 'foo is 1'
}
}
}
}
当我尝试这个时,我收到一个错误
 Multiple occurrences of the when section 
其实我真正的问题是不同的,但解决这个问题可能会解决我真正的问题。提前致谢

最佳答案

看起来你会更好地使用指令作为 intended :
多个 when设计不允许,如果您寻找一个简单的 if else 逻辑,它就在那里:

  • 直率if声明:

  • stage {
    steps {
    script {
    if (foo == 0){
    echo 'foo is 0'
    }
    else if (foo == 1){
    echo 'foo is 1'
    }
    }
    }
    }

    如果您想在 when 中使用嵌套条件:
    2. 给你:
    stage {
    when {
    anyOf {
    environment name: 'VAR_1', value: '1'
    branch 'production'
    }
    }
    steps {
    <your steps>
    }

    关于jenkins - Jenkins 文件中单个阶段内的多个 when 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64265345/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com