1

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
                                '''
                            }
                        }
                    }
                }
            }
        }
    }
}
2
  • 1
    Have you already leveraged the documents on variable scope, or already know about that? learn.microsoft.com/en-us/powershell/module/… --- see also --- Allow variables and functions to be defined within pipeline to be used in any stage --- issues.jenkins-ci.org/browse/JENKINS-41335 --- jenkins.io/doc/pipeline/tour/environment Commented Apr 1, 2019 at 20:04
  • I still do not understand why I get the original value of "TEST_ENV_VAR". It seems it's set as a Powershell environment variable at the beginning of the pipeline, and thus is not affected by the subsequent stages. More strange is the behaviour of "inv_var". It is listed as an environment variable if I call "Get-Item -Path Env:" with the correct value ("1"), but I cannot access it neither through "$env:inv_var" nor through "Get-ChildItem -Path Env:inv_var". The latter throws an error indicating the path doesn't exist. Commented Apr 2, 2019 at 14:24

1 Answer 1

1

It's a very simple answer, and very sad that groovy doesn't help more. Inside withEnv, you must not have a space between the variable and the equals sign. Here's your code with the credentials removed (because i don't have them), and the only other change is removing two spaces

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"
                                '''

                        }
                    }
                }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.