【问题标题】:How to access $GIT_COMMIT in Jenkins declarative pipeline's post phase and emailext?如何在 Jenkins 声明式管道的 post 阶段和 emailext 中访问 $GIT_COMMIT?
【发布时间】:2018-08-21 07:30:44
【问题描述】:

我想在构建成功/失败时发送电子邮件以及一些有用的 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'
        }
    }
}

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

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

agent {
    ...
}
environment {
    MY_GIT_COMMIT = "${GIT_COMMIT}"
}
stages {
    ...
}

感谢任何帮助。提前致谢。

【问题讨论】:

    标签: jenkins jenkins-pipeline jenkins-email-ext


    【解决方案1】:

    另一种方法是使用命令替换,这对我们有用:

    post {
            success {
            sh  '''    
                echo "Commit $(git rev-parse HEAD)" | mail -s "Deployment completed successfully" ADDRESS@EMAIL.COM
                '''                  
            }
        }
    

    【讨论】:

      【解决方案2】:

      您可以通过以下方式执行此操作 - 尽管这可能不适用于声明性管道,但也许它会给您一些东西 - 从多分支管道设置...

      node("my-node") {
      
          withCredentials([string(credentialsId: 'my-credential-id', variable: 'MY_CREDENTIAL')]) {
      
          def commitHash = checkout(scm).GIT_COMMIT
      
          stage ("stage 1") {
              // …
          }
      
      }
      

      或在一个阶段内(对我来说效果更好),

      node("my-node") {
          def scmVars
      
          stage("stage-1") {
              scmVars = git branch: env.BRANCH_NAME, credentialsId: 'my-git-credentials', url: 'https://myrepo.git'
      
              commitHash = scmVars.GIT_COMMIT
          }
      }
      

      我更喜欢在一个阶段,因为checkout(scm).GIT_COMMIT 将获得目标分支的合并提交,而不是来自 PR 的最新提交

      【讨论】:

      • 感谢您指出“checkout scm”和“git branch: ....”之间的 GIT_COMMIT 区别
      【解决方案3】:

      JENKINS-26100 建议

      node {
          withCheckout(scm) {
               echo "GIT_COMMIT is ${env.GIT_COMMIT}"
          }
      }
      

      【讨论】:

      • 感谢您的回复。我想指定我想从管道脚本构建哪个分支,我可以用这个设置来做吗?更新了我的问题以反映这一点。或者有没有一种好方法可以定义在一个地方构建哪些分支,这样我就不必更新 10 多个工作?
      • 我个人使用多分支管道构建所有分支。但是您可以为分支名称添加一个作业参数。不知道该怎么做,但应该有很多例子
      • 是的,我有用于其他目的的参数化变量,但这不可扩展。也许我可以像你说的那样考虑建立任何分支。
      • 你如何得到这个的简写形式?
      猜你喜欢
      • 2021-08-02
      • 1970-01-01
      • 2022-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多