Home > database >  How to use result of command to decide if a Jenkins stage should execute or not?
How to use result of command to decide if a Jenkins stage should execute or not?

Time:06-13

I have a very simple declarative Jenkins file which uses cURL to retrieve config files from an API and then uses the diff command to see if they are different from the same config files in the repository. If the retrieves config files are different, I would like to replace the old files and commit the new ones.

I can't seem to figure out how to store a value (for example $CONFIG_CHANGED = YES) and use it in the next stage/step. Ideally I would like to skip a couple of stages if the config is not changed, but I don't know how to re-use variables across the pipeline. I have googled quite a bit but it seems that environment variables are immutable and can't be changed in the pipeline. Maybe there is a really simple approach to this that I am not seeing? I would appreciate some pointers in the right direction.

CodePudding user response:

The first part will explain how to extract the value set in the shell script. Then I'll explain how to conditionally run a stage.

Here are a few ways you can extract the value from shell execution.

  1. Executing a script and reading the standard out. Note that what ever you write to STDOUT will be appended and returned.
                res = sh (script: '''
                # DO what ever you want here
                CONFIG_SET="YES"
                echo "1234"
                echo $CONFIG_SET''', returnStdout: true).trim()
                echo "$res"
  1. Returning the exit status. Here instead of returning the STDOUT you can return the exit code. YOu can create a shell script in such a way it check the parameter and return the correct exit status.
                res2 = sh (script: '''
                # DO what ever you want here
                CONFIG_SET="NO"
                if [ $CONFIG_SET == "YES" ]
                then
                   exit 0
                else
                    echo "1111"    
                    exit 1
                fi
                ''', returnStatus: true) == 0
                echo "$res2"
  1. Writing to a file and reading the file.
                sh (script: '''
                # DO what ever you want here
                CONFIG_SET="NO"
                echo $CONFIG_SET > output
                ''')
                res3 = readFile('output').trim()
                echo "$res3"

After extracting the value you can define a new stage and add a conditional check with when{}. Following is the full Pipeline.

def res2 = false

pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                script {
                res = sh (script: '''
                # DO what ever you want here
                CONFIG_SET="YES"
                echo "1234"
                echo $CONFIG_SET''', returnStdout: true).trim()
                echo "$res"
                
                res2 = sh (script: '''
                # DO what ever you want here
                CONFIG_SET="YES"
                if [ $CONFIG_SET == "YES" ]
                then
                   exit 0
                else
                    echo "1111"    
                    exit 1
                fi
                ''', returnStatus: true) == 0
                echo "$res2"
                
                
                sh (script: '''
                # DO what ever you want here
                CONFIG_SET="NO"
                echo $CONFIG_SET > output
                ''')
                res3 = readFile('output').trim()
                echo "$res3"
                
                def type = res.class
            }
        }
    }
        stage('IFYES') {
           when { expression { return res2} }
            steps {
                echo "Executing"
        }
    }
}
}
  • Related