Search code examples
jenkinsjenkins-pipelinejenkins-groovy

Jenkins: How to store and read a variable from shell command


In my Jenkins pipeline I am trying to create a zip file from my project folder, based on the version number. For this I am extracting the version number from a toml file and storing in a variable, but while printing that variable (in Zip stage), its showing "${version}".
I believe either I am not storing the value properly or not printing properly.
My groovy script is:

pipeline {
    agent any
    parameters {
        string(description: 'Enter the git branch to be built', name: 'branch', defaultValue: 'master', trim: true)
        choice(choices: 'Build\nPromote', description: 'Select the branch to build', name: 'runtype')
    }
    
    stages {
        stage('Checkout') {
            steps {
                script {
                    if ( params.branch == '') {
                        echo "Building commit with Tag: master"
                        checkout([$class: 'GitSCM', branch: '${params.branch}', extensions: [], userRemoteConfigs: [[credentialsId: 'Aiman_Sarosh_cred', url: 'https://mygit.server/myproject.git']]])
                        echo "Built at path: ${WORKSPACE}"
                        sh 'ls -l ${WORKSPACE}'
                    }
                    else {
                        echo "Building commit with Tag: ${params.branch}"
                        checkout([$class: 'GitSCM', branch: '${params.branch}', extensions: [], userRemoteConfigs: [[credentialsId: 'Aiman_Sarosh_cred', url: 'https://mygit.server/myproject.git']]])
                        echo "Built at path: ${WORKSPACE}"
                        sh 'ls -l ${WORKSPACE}'
                    }    
                }
            }
        }
        stage('Zip') {
            steps {
                script {
                    version=sh 'grep -m 1 version_number ${WORKSPACE}/ComponentInfo.toml | tr -s \' \' | tr -d \'"\' | tr -d "\'" | cut -d\' \' -f3'
                    echo 'Version: ${version}'
                }
            }
        }
    }
}

And the output is

17:05:05  [Pipeline] stage
17:05:05  [Pipeline] { (Zip)
17:05:05  [Pipeline] script
17:05:05  [Pipeline] {
17:05:06  [Pipeline] sh
17:05:07  + grep -m 1 version_number /apps/external/5/jenkins-node-home/workspace/APPL004316/APIN004621/myproject/ComponentInfo.toml
17:05:07  + tr -s ' '
17:05:07  + tr -d '"'
17:05:07  + tr -d ''\'''
17:05:07  + cut '-d ' -f3
17:05:07  0.1.29
17:05:07  [Pipeline] echo
17:05:07  Version: ${version}
17:05:07  [Pipeline] }
17:05:07  [Pipeline] // script
17:05:07  [Pipeline] }
17:05:07  [Pipeline] // stage
17:05:07  [Pipeline] }
17:05:07  [Pipeline] // node
17:05:07  [Pipeline] End of Pipeline
17:05:07  Finished: SUCCESS

Solution

  • sh step doesn’t return command output by default, you need to call it with returnStdout: true parameter. And since you will be using more than one parameter you need to name them all:

    def version = sh script: 'your command’, returnStdout: true