更改的环境变量未反映在Jenkins Pipeline的外壳内部

时间:2018-10-30 14:37:58

标签: jenkins jenkins-pipeline jenkins-groovy jenkins-declarative-pipeline

在Jenkins中,我尝试将环境变量设置为空值,然后分阶段更新变量的值。但是shell脚本无法获取最新值,而旧值是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正在使用脚本{}。但是它在sh {}下不起作用。

1 个答案:

答案 0 :(得分:0)

您在环境块中将变量定义为 COMMIT_ID ='',但是您正在将值分配给 env.COMMIT_ID 。由于环境区块没有该变量,因此在工件上传阶段不可用。尝试分配和读取没有env前缀的

也缺少stage('SCM CheckOut')的结束语。