如何设置和引用Jenkins文件中的变量 [英] How to set and reference a variable in a Jenkinsfile
问题描述
我有一个声明式管道脚本,用于我的多分支项目,我希望读取一个文本文件并将结果存储为一个字符串变量,以供流程后面的步骤访问。使用片段生成器我试图做这样的事情:
filename = readFile'output.txt'
code>
其中 filename
是我的字符串。
Jenkins控制台输出中出现错误:
org.codehaus.groovy.control .MultipleCompilationErrorsException:启动失败:
WorkflowScript:30:预计在第30行第5列执行一个步骤。
filename = readFile'output.txt'
我是否需要使用 withEnv
步骤来设置 readFile
到一个Jenkins环境变量?如果是这样,怎么样?
谢谢
你只能在 steps
指令中使用管道步骤。我知道的一种解决方法是使用脚本
步骤,并将任意管道脚本封装在其中并将结果保存在环境变量中,以便稍后使用。 >
所以在你的情况下:
pipeline {
agent agent any
阶段{
阶段(foo){
步骤{
脚本{
env.FILENAME = readFile'output.txt'
}
echo$ {env.FILENAME}
}
}
}
}
I have a declarative pipeline script for my multibranch project in which I would like to read a text file and store the result as a string variable to be accessed by a later step in the pipeline. Using the snippet generator I tried to do something like this:
filename = readFile 'output.txt'
For which filename
would be my string.
I get an error in the Jenkins console output:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 30: Expected a step @ line 30, column 5.
filename = readFile 'output.txt'
Do I need to use a withEnv
step to set the output of readFile
to a Jenkins environment variable? If so, how?
Thanks
The error is due to that you're only allowed to use pipeline steps inside the steps
directive. One workaround that I know is to use the script
step and wrap arbitrary pipeline script inside of it and save the result in the environment variable so that it can be used later.
So in your case:
pipeline {
agent any
stages {
stage("foo") {
steps {
script {
env.FILENAME = readFile 'output.txt'
}
echo "${env.FILENAME}"
}
}
}
}
这篇关于如何设置和引用Jenkins文件中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!