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

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

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

如果要使用文件(由于脚本是生成所需值的东西),则可以

readFile
如下所示使用。如果没有,请使用如下所示
sh
script
选项:

// 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}"      }    }  }}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5564794.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-14
下一篇2022-12-14

发表评论

登录后才能评论

评论列表(0条)

    保存