在进入阶段之前设置变量的声明式管道脚本
Declrative Pipeline script with setting variables well before entering stages
我对声明式管道脚本有疑问,我正在尝试根据阶段开始前传递的参数动态设置变量,比如在环境块或节点块中
只有一个参数,此时我想用if条件动态构造其他变量,尝试了两个块(环境,节点)但没有运气,因为这需要全局我需要在进入阶段之前初始化它
pipeline {
environment {
stream_name = "${stream_name}"
user_id = "${user_id}"
currentBuild_displayName = "${currentBuild_displayName}"
GBE_ViewTag = "${DevWorkspace_name}"
script {
if ( ${Stream_name} == 'Allura_Main_Infra_PreInt') {
loadrule = "Infra-uInfra/Infra.loadrule"
}
}
}
agent {
node {
label 'CP'
customWorkspace 'D:\RTC'
}
}
您好,您可以在管道级别或每个阶段级别使用环境{}块。在环境块中,您可以设置变量检查以下示例:
pipeline {
agent {label 'master'}
environment{
env1 = 'value0' // these are environment variables for all stages
}
stages{
stage('stage 1') {
environment{
env1 = 'value1' // these are environment variables for 'stage 1'
}
steps{
echo "$env1"
}
}
stage('stage 2') {
environment{
env1 = 'value2' // these are environment variables for 'stage 2'
}
steps{
echo "$env1"
}
}
stage('stage 3') {
steps{
echo "$env1"
}
}
}
}
它也有效,如果我将所有逻辑条件移到管道之外并且变量在所有阶段都是全局可用的
def user_id = currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserId()
def full_name = currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserName()
DevWorkspace_name = "${Developer_workspace}"
if ( DevWorkspace_name ==~ /(?s).*Allura_Main_Infra_PreInt.*/) {
loadrule = "Infra-uInfra/Infra.loadrule"
subsystem = "Infra"
stream_name = "Allura_Main_Infra_PreInt"
}
pipeline {
.....
}
我对声明式管道脚本有疑问,我正在尝试根据阶段开始前传递的参数动态设置变量,比如在环境块或节点块中
只有一个参数,此时我想用if条件动态构造其他变量,尝试了两个块(环境,节点)但没有运气,因为这需要全局我需要在进入阶段之前初始化它
pipeline {
environment {
stream_name = "${stream_name}"
user_id = "${user_id}"
currentBuild_displayName = "${currentBuild_displayName}"
GBE_ViewTag = "${DevWorkspace_name}"
script {
if ( ${Stream_name} == 'Allura_Main_Infra_PreInt') {
loadrule = "Infra-uInfra/Infra.loadrule"
}
}
}
agent {
node {
label 'CP'
customWorkspace 'D:\RTC'
}
}
您好,您可以在管道级别或每个阶段级别使用环境{}块。在环境块中,您可以设置变量检查以下示例:
pipeline {
agent {label 'master'}
environment{
env1 = 'value0' // these are environment variables for all stages
}
stages{
stage('stage 1') {
environment{
env1 = 'value1' // these are environment variables for 'stage 1'
}
steps{
echo "$env1"
}
}
stage('stage 2') {
environment{
env1 = 'value2' // these are environment variables for 'stage 2'
}
steps{
echo "$env1"
}
}
stage('stage 3') {
steps{
echo "$env1"
}
}
}
}
它也有效,如果我将所有逻辑条件移到管道之外并且变量在所有阶段都是全局可用的
def user_id = currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserId()
def full_name = currentBuild.rawBuild.getCause(Cause.UserIdCause).getUserName()
DevWorkspace_name = "${Developer_workspace}"
if ( DevWorkspace_name ==~ /(?s).*Allura_Main_Infra_PreInt.*/) {
loadrule = "Infra-uInfra/Infra.loadrule"
subsystem = "Infra"
stream_name = "Allura_Main_Infra_PreInt"
}
pipeline {
.....
}