如何在Jenkins管道中用writeFile
写出环境变量?
这似乎是一项非常简单的任务,但我找不到任何关于如何使其工作的文档。
我尝试了$VAR
,${VAR}
和${env.VAR}
,没有任何作用......?
答案 0 :(得分:3)
在声明性管道中(使用writeFile的脚本块),它将如下所示:
pipeline {
agent any
environment {
SENTENCE = 'Hello World\n'
}
stages {
stage('Write') {
steps {
script {
writeFile file: 'script.txt', text: env.SENTENCE
}
}
}
stage('Verify') {
steps {
sh 'cat script.txt'
}
}
}
}
输出:
...
[Pipeline] { (Verify)
[Pipeline] sh
[test] Running shell script
+ cat script.txt
Hello World
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
如果你想避免groovy,这也会有效:
writeFile file: 'script.txt', text: "${SENTENCE}"
要将env var与文本结合起来,您可以这样做:
...
environment {
SENTENCE = 'Hello World'
}
...
writeFile file: 'script.txt', text: env.SENTENCE + ' is my newest sentence!\n'