![](/img/trans.png)
[英]how do i get a variable out of powershell in jenkins declarative pipeline?
[英]How to get updated environment variable value inside powershell script on Jenkins' declarative pipeline
我正在編寫一個 Jenkins 聲明性管道腳本,其中包含一個 powershell 腳本。 當我嘗試在 powershell 中獲取環境變量的值時,我得到了它的“原始”值,如environment
塊中定義的那樣,而不是在前一階段設置的值。 withEnv
塊也不起作用。 例子:
pipeline {
agent any
environment { TEST_ENV_VAR = "0" }
stages {
stage('stage1') {
failFast true
parallel {
stage('stage1.1') {
steps {
script {
TEST_ENV_VAR = "1"
}
}
}
}
}
stage('stage2') {
failFast true
parallel {
stage('stage2.1') {
steps {
echo "$TEST_ENV_VAR" // prints "1"
withEnv(["inv_var = $TEST_ENV_VAR"]) {
withCredentials([usernamePassword(credentialsId: "$CredentialsID", passwordVariable: 'password', usernameVariable: 'srvUser')]) {
echo "$TEST_ENV_VAR" // prints "1"
echo "$env.inv_var" // prints "null"
powershell label: 'pshell', returnStatus: true, script: '''
echo "$env:TEST_ENV_VAR" # prints "0"
echo "$env:inv_var" # prints nothing
'''
}
}
}
}
}
}
}
}
這是一個非常簡單的答案,非常遺憾的是 groovy 沒有提供更多幫助。 在 withEnv 中,變量和等號之間不能有空格。 這是刪除憑據的代碼(因為我沒有它們),唯一的其他更改是刪除兩個空格
pipeline {
agent any
environment { TEST_ENV_VAR = "0" }
stages {
stage('stage1') {
failFast true
parallel {
stage('stage1.1') {
steps {
script {
TEST_ENV_VAR = "1"
}
}
}
}
}
stage('stage2') {
failFast true
parallel {
stage('stage2.1') {
steps {
echo "$TEST_ENV_VAR" // prints "1"
withEnv(["inv_var=$TEST_ENV_VAR"]) {
echo "$TEST_ENV_VAR" // prints "1"
echo "$env.inv_var" // now prints "1"
powershell label: 'pshell', returnStatus: true, script: '''
echo "$env:TEST_ENV_VAR" # prints "0"
echo "$env:inv_var" # now prints "1"
'''
}
}
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.