如何在声明式Jenkins管道中的阶段之间传递变量?

3okqufwl  于 2022-11-02  发布在  Jenkins
关注(0)|答案(5)|浏览(395)

如何在声明性管道中的阶段之间传递变量?
在脚本化管道中,我收集的过程是写入临时文件,然后将文件读入变量。
如何在声明性管道中执行此操作?
例如,我想根据shell操作创建的变量触发不同作业的构建。

stage("stage 1") {
    steps {
        sh "do_something > var.txt"
        // I want to get var.txt into VAR
    }
}
stage("stage 2") {
    steps {
        build job: "job2", parameters[string(name: "var", value: "${VAR})]
    }
}
chy5wohz

chy5wohz1#

如果你想使用一个文件(因为脚本是生成你所需要的值的东西),你可以使用readFile,如下所示。如果不想使用,使用shscript选项,如下所示:

// Define a groovy local variable, myVar.
// A global variable without the def, like myVar = 'initial_value',
// was required for me in older versions of jenkins. Your mileage
// may vary. Defining the variable here maybe adds a bit of clarity,
// showing that it is intended to be used across multiple stages.
def myVar = 'initial_value'

pipeline {
  agent { label 'docker' }
  stages {
    stage('one') {
      steps {
        echo "1.1. ${myVar}" // prints '1.1. initial_value'
        sh 'echo hotness > myfile.txt'
        script {
          // OPTION 1: set variable by reading from file.
          // FYI, trim removes leading and trailing whitespace from the string
          myVar = readFile('myfile.txt').trim()
        }
        echo "1.2. ${myVar}" // prints '1.2. hotness'
      }
    }
    stage('two') {
      steps {
        echo "2.1 ${myVar}" // prints '2.1. hotness'
        sh "echo 2.2. sh ${myVar}, Sergio" // prints '2.2. sh hotness, Sergio'
      }
    }
    // this stage is skipped due to the when expression, so nothing is printed
    stage('three') {
      when {
        expression { myVar != 'hotness' }
      }
      steps {
        echo "three: ${myVar}"
      }
    }
  }
}
2exbekwf

2exbekwf2#

简单地说:

pipeline {
        parameters {
            string(name: 'custom_var', defaultValue: '')
        }

        stage("make param global") {
             steps {
               tmp_param =  sh (script: 'most amazing shell command', returnStdout: true).trim()
               env.custom_var = tmp_param
              }
        }
        stage("test if param was saved") {
            steps {
              echo "${env.custom_var}"
            }
        }
  }
hs1rzwqc

hs1rzwqc3#

我遇到了一个类似的问题,因为我希望一个特定的管道提供变量,而许多其他管道使用它来获得这些变量。
我创建了一个my-set-env-variables管道

script
{
    env.my_dev_version = "0.0.4-SNAPSHOT"
    env.my_qa_version  = "0.0.4-SNAPSHOT"
    env.my_pp_version  = "0.0.2"
    env.my_prd_version = "0.0.2"
    echo " My versions  [DEV:${env.my_dev_version}] [QA:${env.my_qa_version}] [PP:${env.my_pp_version}] [PRD:${env.my_prd_version}]"
}

我可以在另一个管道中重用这些变量my-set-env-variables-test

script 
{
    env.dev_version = "NOT DEFINED DEV"
    env.qa_version  = "NOT DEFINED QA"
    env.pp_version  = "NOT DEFINED PP"
    env.prd_version = "NOT DEFINED PRD"
}

stage('inject variables') {

    echo "PRE DEV version = ${env.dev_version}"
    script 
    {
       def variables = build job: 'my-set-env-variables'
       def vars = variables.getBuildVariables()
      //println "found variables" + vars
      env.dev_version = vars.my_dev_version
      env.qa_version  = vars.my_qa_version
      env.pp_version  = vars.my_pp_version
      env.prd_version = vars.my_prd_version
    }
}

stage('next job') {
    echo "NEXT JOB DEV version = ${env.dev_version}"
    echo "NEXT JOB QA version = ${env.qa_version}"
    echo "NEXT JOB PP version = ${env.pp_version}"
    echo "NEXT JOB PRD version = ${env.prd_version}"

}
amrnrhlw

amrnrhlw4#

我已经通过纠正语法增强了现有的解决方案。还使用了隐藏参数插件,这样它就不会在Jenkins UI中显示为额外的参数。效果很好:)

properties([parameters([[$class: 'WHideParameterDefinition', defaultValue: 'yoyo', name: 'hidden_var']])]) 
  pipeline {
        agent any

stages{
        stage("make param global") {
             steps {
                 script{
               env.hidden_var = "Hello"
                 }
              }
        }
        stage("test if param was saved") {
            steps {
              echo"About to check result"
              echo "${env.hidden_var}"
            }
        }
    }
  }
1rhkuytd

1rhkuytd5#

不需要(隐藏的插件)参数定义或临时文件访问。通过在Jenkins文件中使用全局Groovy变量可以完成跨阶段共享变量,如下所示:


# !/usr/bin/env groovy

def MYVAR

def outputOf(cmd) { return sh(returnStdout:true,script:cmd).trim(); }

pipeline {
    agent any    
    stage("stage 1") {
        steps {
            MYVAR = outputOf('echo do_something')
            sh "echo MYVAR has been set to: '${MYVAR}'"
        }
    }
    stage("stage 2") {
        steps {
            sh '''echo "...in multiline quotes: "''' + MYVAR + '''" ... '''
            build job: "job2", parameters[string(name: "var", value: MYVAR)]
        }
    }
}

相关问题