【发布时间】: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