gpt4 book ai didi

Jenkins 管道条件环境变量

转载 作者:行者123 更新时间:2023-12-03 18:35:01 26 4
gpt4 key购买 nike

我在声明性管道的 environmental 指令部分中有一组静态环境变量。这些值可用于管道中的每个阶段。
我希望根据任意条件更改值。
有没有办法做到这一点?

pipeline {
agent any
environment {
if ${params.condition} {
var1 = '123'
var2 = abc
} else {
var1 = '456'
var2 = def
}
}
stages {
stage('One') {
steps {
script {
...
echo env.var1
echo env.var2
...
}
}
}
}
stag('Two'){
steps {
script {
...
echo env.var1
echo env.var2
...
}
}
}

最佳答案

我建议您创建一个阶段“环境”并根据您想要的条件声明您的变量,如下所示:-

pipeline {
agent any
environment {
// Declare variables which will remain same throughout the build
}
stages {
stage('Environment') {
agent { node { label 'master' } }
steps {
script {
//Write condition for the variables which need to change
if ${params.condition} {
env.var1 = '123'
env.var2 = abc
} else {
env.var1 = '456'
env.var2 = def
}
sh "printenv"
}
}
}
stage('One') {
steps {
script {
...
echo env.var1
echo env.var2
...
}
}
}
stage('Two'){
steps {
script {
...
echo env.var1
echo env.var2
...
}
}
}
}
}

关于Jenkins 管道条件环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56071436/

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