下面是我的 jenkins 管道脚本,我在其中一个阶段更改 env 值之一,并在邮件正文中使用这些自定义 env。
pipeline {
agent none
environment {
ENV_STAGE = 'dev'
UNTTST_SNRSCN = 'false'
SNRSCN_OUTPUT = ''
BRANCHNAME = 'development'
EMAIL_RECIPIENTS = 'abc@xyz.com'
}
stages {
stage("Sonar-api"){
when{
expression { params.UNTTST_SNRSCN == 'true' }
}
agent{ label "worker" }
stages{
stage('SonarQube analysis') {
steps {
script {
scannerHome = tool 'sonar-scanner';
}
withSonarQubeEnv('sonarqube9000') {
git branch: '${BRANCHNAME}', credentialsId: '', url: ''
dir ("$WORKSPACE/api/") {
sh "npm install && npm install -D typescript"
sh "npm run test"
sh """/opt/sonar-scanner/bin/sonar-scanner"""
sh "sleep 20"
}
}
}
}
stage ("Clearing Workspace"){
steps{
cleanWs()
}
}
}
}
stage("Build and deploy"){
agent{ label 'worker' }
stages {
stage("Checkout Sourcecode") {
steps{
git branch: "${BRANCHNAME}", credentialsId: '', url: ''
}
}
stage ("Set scan output"){
steps {
script {
if (env.UNTTST_SNRSCN == 'true') {
def SNRSCN_OUTPUT = 'Unit test and sonar scanning performed.'
sh "echo $SNRSCN_OUTPUT"
} else if (env.UNTTST_SNRSCN == 'false') {
def SNRSCN_OUTPUT = 'Unit test and sonar scanning not performed.'
sh "echo $SNRSCN_OUTPUT"
}
}
}
}
stage ("Clearing Workspace"){
steps{
cleanWs()
}
}
}
}
}
post {
failure {
emailext body: """Hi,
Env stage ${env.ENV_STAGE} deployment failed. Please check jenkins error logs and redeploy.
${env.SNRSCN_OUTPUT}
Regards,
DT""", subject: "Deployment #${BUILD_NUMBER} - ${currentBuild.currentResult}", to: "${env.EMAIL_RECIPIENTS}"
}
}
}
我期待电子邮件中 SNRSCN_OUTPUT 的值,但我一直将其设为空。我如何实现这一目标?我希望“设置扫描输出”阶段的 SNRSCN_OUTPUT 的新值显示在电子邮件中。感谢您的宝贵意见。
Subject: Deployment #26 - FAILURE
Hi,
Env stage dev deployment failed. Please check jenkins error logs and redeploy.
null
Regards,
DT