Joh*_*ohn 62 jenkins jenkins-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})]
}
}
Run Code Online (Sandbox Code Playgroud)
bur*_*ttk 71
如果你想使用一个文件(因为脚本是生成你需要的值的东西):
// Define a groovy global variable, myVar.
// A local, def myVar = 'initial_value', didn't work for me.
// 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.
myVar = 'initial_value'
pipeline {
agent { label 'docker' }
stages {
stage('one') {
steps {
echo "${myVar}" // prints '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()
// OPTION 2: set variable by grabbing output from script
myVar = sh(script: 'echo hotness', returnStdout: true).trim()
}
echo "${myVar}" // prints 'hotness'
}
}
stage('two') {
steps {
echo "${myVar}" // prints 'hotness'
}
}
// this stage is skipped due to the when expression, so nothing is printed
stage('three') {
when {
expression { myVar != 'hotness' }
}
steps {
echo "three: ${myVar}"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 12
只是:
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}"
}
}
}
Run Code Online (Sandbox Code Playgroud)
不需要(隐藏插件)参数定义或临时文件访问。跨阶段共享变量可以通过在 Jenkinsfile 中使用全局 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)]
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
我有一个类似的问题,因为我想要一个特定的管道来提供变量,而许多其他管道使用它来获取这个变量。
我创建了一个 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}]"
}
Run Code Online (Sandbox Code Playgroud)
我可以在另一个管道中重用这些变量 my-set-env-variables-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}"
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
55279 次 |
最近记录: |