[英]Jenkins pipeline : using variable created in shell 1, in shell 2
In my pipeline I've 2 stages, and they both call a different shell script (the way they're called is some custom code as we have different pipelines interacting with each other):在我的管道中,我有 2 个阶段,它们都调用不同的 shell 脚本(调用它们的方式是一些自定义代码,因为我们有不同的管道相互交互):
stage('first') {
steps {
script {
stageErrors.add(launchPortalStep.launchStepWithVerification('shell',"chmod +x ./script1.sh && ./script1.sh"))
}
}
}
stage('second') {
steps {
script {
stageErrors.add(launchPortalStep.launchStepWithVerification('shell',"chmod +x ./script2.sh && ./script2.sh"))
}
}
}
The thing is, in script2 I need to use a variable that is set in script1, the only ways I've think of that could work would mean pretty much rewriting everything and that's not a possibility.问题是,在 script2 中,我需要使用在 script1 中设置的变量,我认为唯一可行的方法意味着几乎要重写所有内容,而这是不可能的。
Script1 must return either 0 or 1, so it's not possible to return the value and stock in an environment variable that would be then passed as a parameter in script2. Script1 必须返回 0 或 1,因此不可能返回环境变量中的值和库存,然后将其作为参数传递给 script2。 Scripts are also way too long to have them entirely in the pipeline using sh commands (that would be adding at least 700 lines).
脚本也太长了,无法使用 sh 命令将它们完全放在管道中(这将添加至少 700 行)。
What can I do?我能做些什么?
You should use a properties file (it can be even a temp file) to pass the needed variables between the stages.您应该使用属性文件(甚至可以是临时文件)在阶段之间传递所需的变量。
You should write the needed variables into a properties file (in VAR=VAL
format) in the shell
script which is called in First
step ( sh 'echo "MY_ENV_VAR=test_var" > test.prop'
).您应该将所需的变量写入
shell
脚本中的属性文件( VAR=VAL
格式),该脚本在First
中调用( sh 'echo "MY_ENV_VAR=test_var" > test.prop'
)。
You should read the properties file in the Second
step with the readProperties
function which is part of the Pipeline Utility Steps
plugin def props = readProperties file: 'test.pro
).您应该在
Second
中使用readProperties
function 读取属性文件,这是Pipeline Utility Steps
插件def props = readProperties file: 'test.pro
。 Then set the needed variables as environment variables ( env.MY_ENV_VAR = props.MY_ENV_VAR
) so you will be able to use the variables in your second shell script as environment variables ( sh 'echo MY_ENV_VAR = ${MY_ENV_VAR}'
).然后将所需的变量设置为环境变量(
env.MY_ENV_VAR = props.MY_ENV_VAR
),这样您就可以将第二个 shell 脚本中的变量用作环境变量( sh 'echo MY_ENV_VAR = ${MY_ENV_VAR}'
)。
Complete example code:完整的示例代码:
pipeline {
agent { label 'MISC' }
stages{
stage('First'){
steps {
sh 'echo "MY_ENV_VAR=test_var" > test.prop'
}
}
stage('Second'){
steps {
script {
// readProperties is a step in Pipeline Utility Steps plugin
def props = readProperties file: 'test.prop'
// Assuming the key name is MY_ENV_VAR in properties file.
env.MY_ENV_VAR = props.MY_ENV_VAR
sh 'echo MY_ENV_VAR = ${MY_ENV_VAR}'
}
}
}
}
}
Output: Output:
[Pipeline] stage
[Pipeline] { (First)
[Pipeline] sh
+ echo MY_ENV_VAR=test_var
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Second)
[Pipeline] script
[Pipeline] {
[Pipeline] readProperties
[Pipeline] sh
+ echo MY_ENV_VAR = test_var
MY_ENV_VAR = test_var
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.