基于Jenkins管道中的构建状态的条件值



这里是pipeline:

pipeline {
agent {
node {
label 'bla bla'
customWorkspace 'D:\'
}
}
environment {
EMAIL = 'some_emails'
HEADER_COLOR = 'teal'
}
stages {
stage('Pull git changes') {
steps {
....
}
}
}     
stage('Run tests') {
steps {
echo "Run tests with tag: ${TESTS_RUN_TAG}"
}
}
}
}
post {
always {
robot enableCache: false, logFileName: 'log.html', outputFileName: 'output.xml', outputPath: '', reportFileName: 'report.html'
emailext attachmentsPattern: 'log.html', subject: "${JOB_NAME} build #${BUILD_NUMBER} automation results", body: """BY_BODY""", to: "${EMAIL}" 
}
}
}

所以在我的帖子里,我用HTML体发送电子邮件(这里省略了体),我想根据${currentBuild.result}值改变一些HTML的颜色。这就是我所做的:

if (${currentBuild.result} == "SUCCESS") {                                          
HEADER_COLOR = 'crimson'
} else {                                   
HEADER_COLOR = 'teal'
}

在我的HTML正文中,我这样使用:

bgcolor="${HEADER_COLOR}"

所以我试着把它放在我的后期阶段,但它失败了WorkflowScript,我想也许我有一些语法错误,或者我需要把它放在其他地方(或两者)

有什么想法吗?

我不擅长groovy脚本编写,所以我建议如下:

post {
always {
put something here 
}
failure {
mail to: team@example.com, subject: 'The Pipeline failed :('
}
success {
mail to: team@example.com, subject: 'The Pipeline succeeded :)'
}
}

问题是你没有在脚本中包含这些if block所以它应该是这样的

post {
always {
script {

if (${currentBuild.currentResult} == "SUCCESS") {                                          
HEADER_COLOR = 'crimson'
} else {                                   
HEADER_COLOR = 'teal'
}

robot enableCache: false, logFileName: 'log.html', outputFileName: 'output.xml', outputPath: '', reportFileName: 'report.html'
emailext attachmentsPattern: 'log.html', subject: "${JOB_NAME} build #${BUILD_NUMBER} automation results", body: """BY_BODY""", to: "${EMAIL}" 
}
}
}

最新更新