简体   繁体   English

Jenkins 管道:使用在 shell 1 中创建的变量,在 shell 2 中

[英]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.

相关问题 如何在 Jenkins 管道脚本中使用 shell 脚本变量作为 groovy 变量 - How to use shell script variable as groovy variable in Jenkins pipeline script Jenkins Pipeline Access阶段在power shell执行中执行变量 - Jenkins Pipeline Access stage steps variable in power shell execution Shell脚本中的Jenkins Pipeline Environment Variable创建新行 - Jenkins Pipeline Environment Variable in Shell script creates a new line 如何访问 jenkins 管道中 shell 脚本中的 groovy 变量? - How to access a groovy variable in shell script in jenkins pipeline? 在 jenkins 声明性管道 cloudformation 中使用带有 shell 命令的 If 语句 - Using If statement with shell commands in jenkins Declarative pipeline cloudformation 使用管道作为代码在 jenkins master 上找不到返回的 shell 脚本 - shell script returning not found on jenkins master using pipeline as code 在 Jenkinsfile 的 shell 命令中为 Jenkins 管道使用参数 - Using parameters within a shell command in Jenkinsfile for Jenkins pipeline 在 YML 管道中使用 shell 脚本更改 Jenkins 中的目录 - Change directory in Jenkins using shell script in YML pipeline 在 Jenkins 管道 shell 中运行嵌套命令 - Running nested commands in Jenkins pipeline shell 在 groovy jenkins 管道中转义 shell 字符 - Escaping shell character in groovy jenkins pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM