如何在声明性 Jenkins 管道的各个阶段之间传递变量?

如何在声明性管道中的各阶段之间传递变量?

在一个脚本化的管道中,我收集的程序是写入一个临时文件,然后将该文件读入一个变量。

How do I do this in a declarative pipeline?

例如,我想触发一个不同作业的构建,基于一个 shell 操作创建的变量。

stage("stage 1") {
steps {
sh "do_something > var.txt"
// I want to get var.txt into VAR
}
}
stage("stage 2") {
steps {
build job: "job2", parameters[string(name: "var", value: "${VAR})]
}
}
156332 次浏览

如果您想要使用一个文件(因为脚本是生成您所需要的值的东西) ,您可以使用 readFile,如下所示。如果没有,使用 shscript选项,如下所示:

// Define a groovy local variable, myVar.
// A global variable without the def, like myVar = 'initial_value',
// was required for me in older versions of jenkins. Your mileage
// may vary. Defining the variable here maybe adds a bit of clarity,
// showing that it is intended to be used across multiple stages.
def myVar = 'initial_value'


pipeline {
agent { label 'docker' }
stages {
stage('one') {
steps {
echo "1.1. ${myVar}" // prints '1.1. initial_value'
sh 'echo hotness > myfile.txt'
script {
// OPTION 1: set variable by reading from file.
// FYI, trim removes leading and trailing whitespace from the string
myVar = readFile('myfile.txt').trim()
}
echo "1.2. ${myVar}" // prints '1.2. hotness'
}
}
stage('two') {
steps {
echo "2.1 ${myVar}" // prints '2.1. hotness'
sh "echo 2.2. sh ${myVar}, Sergio" // prints '2.2. sh hotness, Sergio'
}
}
// this stage is skipped due to the when expression, so nothing is printed
stage('three') {
when {
expression { myVar != 'hotness' }
}
steps {
echo "three: ${myVar}"
}
}
}
}

简单来说:

  pipeline {
parameters {
string(name: 'custom_var', defaultValue: '')
}


stage("make param global") {
steps {
tmp_param =  sh (script: 'most amazing shell command', returnStdout: true).trim()
env.custom_var = tmp_param
}
}
stage("test if param was saved") {
steps {
echo "${env.custom_var}"
}
}
}

我也遇到过类似的问题,因为我需要一个特定的管道来提供变量,还有许多其他的管道使用它来获取这个变量。

我建立了一个 my-set-env-variables管道

script
{
env.my_dev_version = "0.0.4-SNAPSHOT"
env.my_qa_version  = "0.0.4-SNAPSHOT"
env.my_pp_version  = "0.0.2"
env.my_prd_version = "0.0.2"
echo " My versions  [DEV:${env.my_dev_version}] [QA:${env.my_qa_version}] [PP:${env.my_pp_version}] [PRD:${env.my_prd_version}]"
}

我可以在另一个管道 My-set-env-variable-test中重用这些变量

script
{
env.dev_version = "NOT DEFINED DEV"
env.qa_version  = "NOT DEFINED QA"
env.pp_version  = "NOT DEFINED PP"
env.prd_version = "NOT DEFINED PRD"
}


stage('inject variables') {


echo "PRE DEV version = ${env.dev_version}"
script
{
def variables = build job: 'my-set-env-variables'
def vars = variables.getBuildVariables()
//println "found variables" + vars
env.dev_version = vars.my_dev_version
env.qa_version  = vars.my_qa_version
env.pp_version  = vars.my_pp_version
env.prd_version = vars.my_prd_version
}
}


stage('next job') {
echo "NEXT JOB DEV version = ${env.dev_version}"
echo "NEXT JOB QA version = ${env.qa_version}"
echo "NEXT JOB PP version = ${env.pp_version}"
echo "NEXT JOB PRD version = ${env.prd_version}"


}




I have enhanced the existing solution by correcting syntax .Also used hidden parameter plugin so that it does not show up as an extra parameter in Jenkins UI. Works well :)

properties([parameters([[$class: 'WHideParameterDefinition', defaultValue: 'yoyo', name: 'hidden_var']])])
pipeline {
agent any


stages{
stage("make param global") {
steps {
script{
env.hidden_var = "Hello"
}
}
}
stage("test if param was saved") {
steps {
echo"About to check result"
echo "${env.hidden_var}"
}
}
}
}

不需要(隐藏插件)参数定义或临时文件访问。跨阶段共享变量可以通过在 Jenkins 文件中使用全局 Groovy 变量来实现,如下所示:

#!/usr/bin/env groovy
def MYVAR


def outputOf(cmd) { return sh(returnStdout:true,script:cmd).trim(); }


pipeline {
agent any
stage("stage 1") {
steps {
MYVAR = outputOf('echo do_something')
sh "echo MYVAR has been set to: '${MYVAR}'"
}
}
stage("stage 2") {
steps {
sh '''echo "...in multiline quotes: "''' + MYVAR + '''" ... '''
build job: "job2", parameters[string(name: "var", value: MYVAR)]
}
}
}