在两个独立阶段使用的 Jenkinsfile 变量
Jenkinsfile variable used in two separate stages
我有一个使用两个独立节点(一个用于构建,一个用于测试)的管道作业,我想在我的 Jenkinsfile
中的两个代码块之间共享一个变量。我认为这是可能的,但我对 groovy 和 Jenkinsfile
概念还很陌生。这是到目前为止的相关代码:
node('build') {
stage('Checkout') {
checkout scm
}
stage('Build') {
bat(script: 'build')
def rev = readFile('result')
}
}
node('test') {
stage('Test') {
def SDK_VERSION = "5.0.0001.${rev}"
bat "test.cmd ${env.BUILD_URL} ${SDK_VERSION}"
archiveArtifacts artifacts: 'artifacts/**/*.xml'
junit 'artifacts/**/*.xml'
}
}
我想在构建阶段分配 "rev" 变量,然后在测试阶段将其连接到 SDK_VERSION 变量。我的错误是:
groovy.lang.MissingPropertyException: No such property: rev for class: groovy.lang.Binding
只需在 node
块之前定义变量:
def rev = ''
node('build') {
stage('Checkout') {
checkout scm
}
stage('Build') {
bat(script: 'build')
rev = readFile('result')
}
}
在声明性管道中,@mkobit 的回答无效。但是,您可以显式切换到脚本模式并使用其作用域,例如像这样:
...
steps {
script {
def foo = sh script: "computeFoo", returnStdout: true
node('name') {
script {
someStep()
}
}
}
}
...
我有一个使用两个独立节点(一个用于构建,一个用于测试)的管道作业,我想在我的 Jenkinsfile
中的两个代码块之间共享一个变量。我认为这是可能的,但我对 groovy 和 Jenkinsfile
概念还很陌生。这是到目前为止的相关代码:
node('build') {
stage('Checkout') {
checkout scm
}
stage('Build') {
bat(script: 'build')
def rev = readFile('result')
}
}
node('test') {
stage('Test') {
def SDK_VERSION = "5.0.0001.${rev}"
bat "test.cmd ${env.BUILD_URL} ${SDK_VERSION}"
archiveArtifacts artifacts: 'artifacts/**/*.xml'
junit 'artifacts/**/*.xml'
}
}
我想在构建阶段分配 "rev" 变量,然后在测试阶段将其连接到 SDK_VERSION 变量。我的错误是:
groovy.lang.MissingPropertyException: No such property: rev for class: groovy.lang.Binding
只需在 node
块之前定义变量:
def rev = ''
node('build') {
stage('Checkout') {
checkout scm
}
stage('Build') {
bat(script: 'build')
rev = readFile('result')
}
}
在声明性管道中,@mkobit 的回答无效。但是,您可以显式切换到脚本模式并使用其作用域,例如像这样:
...
steps {
script {
def foo = sh script: "computeFoo", returnStdout: true
node('name') {
script {
someStep()
}
}
}
}
...