如何在 Jenkins 中将 shell 命令作为变量传递

How to pass shell commnds as variables in Jenkins

我有一个命令可以检索我最近的 git 提交哈希。

git rev-parse HEAD

是否可以将其作为环境变量传递给 Jenkins 管道阶段?

我试过了,

environment {
    CURRENT_COMMIT_HASH=`git rev-parse HEAD`
}

但是当我在后期回显它时

stage ('Issues Report') {
    steps {
        sh '''
        echo "${CURRENT_COMMIT_HASH}"
        '''
        }   
    }

结果是空白。

+ echo 

这应该有效

stage ('Stage-Name') {
    environment {
        current_hash = """${sh(
        returnStdout: true,
        script: "git rev-parse HEAD"
        )}"""
    steps {
        sh '''
        echo "My current commit hash is $current_hash"
        '''
        }
      }