如何在 Jenkins 声明性管道的发布阶段和 emailext 中访问 $GIT_COMMIT?

Gom*_*ius 10 jenkins jenkins-pipeline jenkins-email-ext

我想在构建成功/失败时发送电子邮件以及一些有用的 Git 信息,例如提交 SHA、先前成功的 SHA、提交消息等。我能够以旧的 UI 方式执行此操作,但现在我已经创建了声明性管道和现在我收到GIT_BRANCH is not supported in this context了收到的电子邮件。我正在使用 Jenkins 版本。2.89.3. 我的脚本:

pipeline {
    agent { 
        ...
    }
    stages {
        stage('Checkout') {
            steps {
                sh 'printenv'
                checkout scm: [$class: 'GitSCM', branches: [[[name: '*/development']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [url: 'https://github.com/myrepo/']]]
                sh 'git submodule foreach --recursive \'git submodule sync\''
                sh 'git submodule update --init --recursive'
            }
        }
        stage('Build') {
            steps {
               ...         
            }
        }
    }
    post {
        success {
            sh 'printenv'
            emailext body: '$PROJECT_DEFAULT_CONTENT Commit message: ${FILE, path="commit_message.txt"}\nThis commit: ${GIT_COMMIT}Build URL: ${BUILD_URL}',
            recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider']],
            subject: 'Some subject'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

printenv将打印的一切,我希望包括GIT_COMMIT无论是在“结帐”阶段和后期的成功。因为我将在 10 多个 Jenkins 作业中重复使用这个脚本,所以我想避免添加手动 UI 工作来传递参数和东西。

我尝试在 之前声明环境,stages但无法从以下上下文中使用它emailext

agent {
    ...
}
environment {
    MY_GIT_COMMIT = "${GIT_COMMIT}"
}
stages {
    ...
}
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏。提前致谢。

小智 7

JENKINS-26100建议

node {
    withCheckout(scm) {
         echo "GIT_COMMIT is ${env.GIT_COMMIT}"
    }
}
Run Code Online (Sandbox Code Playgroud)