I can't use environment variables in pipeline using when step

Hi,

In the following pipeline I need to perform the when depending on the result of a command within a stage.

pipeline {

    agent { label 'docker' }
    
    stages {
        
        stage("set env EXIST variable"){
            steps{
                container('docker') {
                    script{
                        EXIST = sh(script: "echo 'YES'", , returnStdout: true)
                    }
                }
            }
        } // END stage set env EXIST variable

        stage("control IMAGE"){
            steps{
                sh """
                #!/usr/bin/env bash
                echo "Value stage de EXIST is ${EXIST}"
                """
            }
        } // END stage control IMAGE

        stage("When"){
            when {
                anyOf {
                    **expression { EXIST == "YES" }**
                }
            }
            steps{
                sh """
                echo "IN WHEN with value ${EXIST}"
                """
            }
        } // END stage When
    } // END stages
} // END pipeline`Texto preformateado`

Always have the same result

Stage “When” skipped due to when conditional

thanks

Hello @cruyra and welcome to this community. :wave:

The issue you’re experiencing could be linked to the scope of the EXIST variable. In Jenkins Pipeline, each stage has its own scope, and variables defined in one stage are not accessible in another stage.

To make the EXIST variable accessible across stages, you can define it at the pipeline level using the environment directive. Here’s how you maybe could modify your pipeline:

pipeline {
    agent { label 'docker' }

    environment {
        EXIST = ""
    }

    stages {
        stage("set env EXIST variable"){
            steps{
                container('docker') {
                    script{
                        env.EXIST = sh(script: "echo 'YES'", , returnStdout: true).trim()
                    }
                }
            }
        } // END stage set env EXIST variable

        stage("control IMAGE"){
            steps{
                sh """
                #!/usr/bin/env bash
                echo "Value stage of EXIST is ${env.EXIST}"
                """
            }
        } // END stage control IMAGE

        stage("When"){
            when {
                expression { env.EXIST == "YES" }
            }
            steps{
                sh """
                echo "IN WHEN with value ${env.EXIST}"
                """
            }
        } // END stage When
    } // END stages
} // END pipeline

In this modified pipeline, EXIST is defined as an environment variable at the pipeline level, and it’s accessed as env.EXIST in the stages. The trim() function is used to remove any leading or trailing whitespace from the output of the sh step.

Thank you very much @poddingue, and happy new year.
I have tried the pipeline and it doesn’t work as expected.
The environment variable loses its value in the second stage and does not fulfil the expression in the When stage.

@Library('ci-library') _


pipeline {
    agent { label 'docker' }

    environment {
        EXIST = ""
    }

    stages {
        stage("set env EXIST variable"){
            steps{
                container('docker') {
                    script{
                        env.EXIST = sh(script: "echo 'YES'", , returnStdout: true).trim()
                    }
                }
            }
        } // END stage set env EXIST variable

        stage("control IMAGE"){
            steps{
                sh """
                #!/usr/bin/env bash
                echo "Value stage of EXIST is ${env.EXIST}"
                """
            }
        } // END stage control IMAGE

        stage("When"){
            when {
                expression { env.EXIST == "YES" }
            }
            steps{
                sh """
                echo "IN WHEN with value ${env.EXIST}"
                """
            }
        } // END stage When
    } // END stages
} // END pipeline

Result

Agent docker-agent-smm01 is provisioned from template docker-agent
Running on docker-agent-smm01 in /home/jenkins/agent/workspace/prueba_lectura de ECR
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (set env EXIST variable)
[Pipeline] container
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ echo YES
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (control IMAGE)
[Pipeline] sh
+ echo Value stage of EXIST is null
Value stage of EXIST is null
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (When)
Stage "When" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

if I use this pipeline
I have the variables but it doesn’t fit in the When
I don’t know if the conditional expression is correct.

@Library('ci-library') _


pipeline {

    agent { label 'docker' }
    
    stages {
        
        stage("set env EXISTE variable"){
            steps{
                container('docker') {
                    script{
                        EXISTE = sh(script: "echo 'YES'", , returnStdout: true)
                    }
                }
            }
        } // END stage set env EXISTE variable

        stage("control IMAGE"){
            steps{
                sh """
                #!/usr/bin/env bash
                echo "El valor en el stage de EXISTE es ${EXISTE}"
                """
            }
        } // END stage control IMAGE

        stage("When"){
//            when {
//                anyOf {
//                    expression { EXISTE == "YES" }
//                }
//            }
            steps{
                sh """
                echo "ENTRA EL WHEN con el valor ${EXISTE}"
                """
            }
        } // END stage When
    } // END stages
} // END pipeline

Result

Agent docker-agent-1ns0g is provisioned from template docker-agent
Running on docker-agent-1ns0g in /home/jenkins/agent/workspace/prueba_lectura de ECR
[Pipeline] {
[Pipeline] stage
[Pipeline] { (set env EXISTE variable)
[Pipeline] container
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ echo YES
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (control IMAGE)
[Pipeline] sh
+ echo El valor en el stage de EXISTE es YES

El valor en el stage de EXISTE es YES

[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (When)
[Pipeline] sh
+ echo ENTRA EL WHEN con el valor YES

ENTRA EL WHEN con el valor YES

[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS