I am writing a Jenkins declarative pipeline script with a powershell script inside it. When I try to get the value of an environment variable inside powershell, I get its "original" value, as defined on the environment block, and not the value set in a previuous stage. The withEnv block doesn't work either. Example:
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
'''
}
}
}
}
}
}
}
}