0

In Jenkins, I tried to set a environment variable with a empty value, and in stages I will update the value of the variable. but the shell script failed to get the latest value and the old value is echo.

pipeline {
        agent {
            label 'test'
        } 
        tools {
            jdk 'Java 1.8'
        }
       environment {
             DASHBOARD_PATH = '/pkg/vddfg/oradfg/apache/htdocs/'
             COMMIT_ID = ' '
             GROUP_ID = 'Dashboard3'
             ARTIFACT_ID = 'MS_CSD_DB3'
             MS_VERSION = ' '
             JAVA_HOME = tool('Java 1.8')
             PATH="${JAVA_HOME}/bin:${env.PATH}"
        }   
        stages {
            stage('SCM CheckOut'){
                steps {
                    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], gitTool: 'default (git from PATH)', submoduleCfg: [], userRemoteConfigs: [[credentialsId: '9754f1e7-7095-4f68-bf56-6e92a292a241', url: 'git@demucvtr13:csd/csd-dashboard3.git']]])
                    //def gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
                    //sh 'echo ${gitCommit}'
                    script {
                    COMMIT_ID =  withEnv(['PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin:/pkg/vddfg/home/oradfg/csw/bin']){ sh(script: "git rev-parse HEAD", returnStdout: true).trim() }
                    echo  COMMIT_ID

                }
            }
             }                 
         stage('Artifact Upload') {
                steps {
                    script {
                            def now = new Date()
                            MS_VERSION = now.format("yyMMdd_HHmm", TimeZone.getTimeZone('GMT+1'))
                            echo "${COMMIT_ID}"
                            echo "${GROUP_ID}"
                            echo "${ARTIFACT_ID}"
                            echo "${MS_VERSION}"
                            withEnv(['PATH+EXTRA=/usr/sbin:/usr/bin:/sbin:/bin']) {
                                sh '''
                                    commit_id="${COMMIT_ID}"
                                    grp_id="${GROUP_ID}"
                                    art_id="${ARTIFACT_ID}"
                                    ver_id="${MS_VERSION}"
                                    '''
                            }        
                    }
                }   
            }

        }            
}

Echo is working under script{}. But it is not working under sh {}.

user2439278
  • 1,222
  • 7
  • 41
  • 75

2 Answers2

1

You defined the variable as COMMIT_ID = ' ' in the environment block, but you are assigning the value to env.COMMIT_ID. Since the environment block does not have that variable, it's not available in Artifact Upload stage. Try to assign and read without env prefix.

Also the closing parathesis for stage('SCM CheckOut') is missing.

secilusta
  • 86
  • 4
0

When using sh ''' ''' for some reason the changes done to the environment variables doesn't get reflected, we can either change the ''' to """. If you do so you will be able to see the change done to the environment variable. But if you have other commands which affects the change you can define an environment under the stage as below.

stage('Artifact Upload') {
    environment {
        commit = COMMIT_ID
    }
    steps {
        script {
            echo "${commit}"
        }
    }
}