bash 在 Jenkins 的构建步骤之间传递数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22366808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing data between build steps in Jenkins
提问by jcol
I'd like to do something along the lines of:
我想做一些事情:
This is overly simple and just demonstrates what I'd like to do. Basically, I want to be able to store and access variables within a single job scope between multiple build steps. Also, I can get around by storing the data to a file and reading it later, but I'd like something easier and less 'hacky'
这过于简单,只是展示了我想做的事情。基本上,我希望能够在多个构建步骤之间的单个作业范围内存储和访问变量。此外,我可以通过将数据存储到文件并稍后读取来解决问题,但我想要一些更简单且不那么“hacky”的东西
Build Step #1 - Execute shell
构建步骤 #1 - 执行 shell
$START=timestamp
Build Step #2 - Run another job
构建步骤 #2 - 运行另一个作业
Build Step #3 - Execute Shell
构建步骤 #3 - 执行 Shell
$END=timestamp
TIME_LAPSED=$END-$START
(post lapsed time somewhere)
回答by Destroyica
One thing remains between shells: the workspace.
Simple and stupid solution: use file(s)!
壳之间还剩下一件事:工作区。
简单而愚蠢的解决方案:使用文件!
Huge additional advantage: it works when you split your job in several jobs and use the Clone Workspace plugin
巨大的额外优势:当您将工作拆分为多个工作并使用克隆工作区插件时,它会起作用
Build Step #1 - Execute shell
构建步骤 #1 - 执行 shell
START=timestamp
...
echo $START > env_start.txt
...
...
Build Step #3 - Execute Shell
构建步骤 #3 - 执行 Shell
START=`cat env_start.txt`
END=timestamp
TIME_LAPSED=$END-$START
回答by Dominik
If you are using the declarative pipeline syntax defining a variable in the environment
section and using a script
step to set its value may be useful.
如果您使用声明性管道语法在该environment
部分定义变量并使用script
步骤设置其值可能会很有用。
I'm doing something like this with a declarative pipeline and it works for passing a variable (both inside one stage and between stages):
我正在用一个声明性管道做这样的事情,它适用于传递一个变量(在一个阶段内和阶段之间):
pipeline {
agent any
environment {
variable = ''
}
stages {
stage('Some stage') {
steps {
script {
if (some condition){
variable = 'some value'
} else { variable = 'else value' }
}
sh '${somepath}/bin/script ' +
"-parameter=${variable}"
}
}
}
stage('Dummy print') {
steps {
sh "echo ${variable}"
}
}
[...]
回答by beka
We use inject environment variables plugin extensively and it works great. The solution is:
我们广泛使用注入环境变量插件,效果很好。解决办法是:
- Set your variable myenv=value1
- print to file in workspace: echo "myenv=$myenv" > tmp.myenv
- Inject after every change: Use envinject to read environment from file tmp.myenv -> myenv is now known as part of the job environment.
- 设置变量 myenv=value1
- 打印到工作区中的文件:echo "myenv=$myenv" > tmp.myenv
- 每次更改后注入:使用 envinject 从文件 tmp.myenv 读取环境 -> myenv 现在被称为作业环境的一部分。
回答by Alan Hu
@Gurubaran's solution works for me, you need to install "Environment Injector" plugin in jenkins, then
@Gurubaran 的解决方案对我有用,你需要在 jenkins 中安装“ Environment Injector”插件,然后
Step1: use shell/powershell/windows batch/etc. to create generate a properties(key=value) file. for example: the file path is $WORKSPACE/env.properties.
Step1:使用shell/powershell/windows batch/etc。创建生成属性(键=值)文件。例如:文件路径是 $WORKSPACE/env.properties。
Step2: Add an "Inject environment variables" component and set "Properties File Path" to $WORKSPACE/env.properties
Step2:添加“Inject environment variables”组件并将“Properties File Path”设置为$WORKSPACE/env.properties
After Step2: You can use these environment variables in the following steps.
Step2 之后:您可以在以下步骤中使用这些环境变量。
Example:
例子:
回答by Drew Pierce
One way to work with Jenkins variables is to use jenkins-cli.jar
in the build step, it takes some work but this will add FOO=1
in the parameters list, since it's running in a build step it knows which build to set the parameter for.
使用 Jenkins 变量的一种方法是jenkins-cli.jar
在构建步骤中使用,它需要一些工作,但这将添加FOO=1
到参数列表中,因为它在构建步骤中运行,它知道要为哪个构建设置参数。
java -jar ${JENKINS_HOME}/war/WEB-INF/jenkins-cli.jar -s ${JENKINS_URL} set-build-parameter FOO 1
回答by GeroldBroser reinstates Monica
None of:
没有:
- String Parameterof a Parameterized Build
- pre-build Inject environment variables to the build process
- in-build step Inject environment variables
- 字符串参数一的参数构建
- 预构建 将环境变量注入构建过程
- 在构建步骤中注入环境变量
works (as of v1.656). You are able to read each of them, of course, but new values that are assigned to them are not available in subsequent build steps.
有效(从 v1.656 开始)。当然,您可以读取它们中的每一个,但分配给它们的新值在后续构建步骤中不可用。
Hence, JediMasterCoder's answer and handling via file like Destroyica's are the only options so far.
因此, JediMasterCoder 的回答和通过文件处理(如 Destroyica 的文件)是迄今为止唯一的选择。
回答by Gurubaran
Jenkins allows you to inject environment variables to the build process. Maybe all you have to do is Inject the Start time and End Time as environment variables and access them across your build steps.
Jenkins 允许您将环境变量注入构建过程。也许您所要做的就是将开始时间和结束时间作为环境变量注入,并在您的构建步骤中访问它们。
回答by Eldad Assis
On top of what @Gurubaran suggested (which is what I'd do if had no other option), I'd just opt for joining the build steps to a single one, which will greatly simplify this need.
You will need to care for the error handling logic and exit conditions, but your environment will be solid!
除了@Gurubaran 的建议(如果没有其他选择,我会这样做),我只是选择将构建步骤加入一个步骤,这将大大简化这种需求。
您将需要关注错误处理逻辑和退出条件,但您的环境将是可靠的!
I hope this helps.
我希望这有帮助。