Jenkins 声明式管道。 post 块中的条件语句
Jenkins declarative pipeline. Conditional statement in post block
有一个 Jenkins 管道。 Need/want 构建成功后发送邮件。通过电子邮件将所有分支发送至 maillist-1 并将主分支的构建过滤至 maillist-master.
我尝试使用 if
和 when
语句步骤,但它们都在 post 块中失败。
pipeline {
agent ...
stages {...}
post{
success{
archiveArtifacts: ...
if( env.BRANCH_NAME == 'master' ){
emailext( to: 'maillist-master@domain.com'
, replyTo: 'maillist-master@domain.com'
, subject: 'Jenkins. Build succeeded :^) '
, body: params.EmailBody
, attachmentsPattern: '**/App*.tar.gz'
)
}
emailext( to: 'maillist-1@domain.com'
, replyTo: 'maillist-1@domain.com'
, subject: 'Jenkins. Build succeeded :^) '
, body: params.EmailBody
, attachmentsPattern: '**/App*.tar.gz'
)
}
}
}
如何实现想要的行为?
目前确实不能在全局 post 块中使用 when
。 When
必须在阶段指令内使用。
使用 if else
是一个合乎逻辑的选择,但您需要在声明性管道中使用一个脚本块来完成这项工作:
pipeline {
agent any
parameters {
string(defaultValue: "master", description: 'Which branch?', name: 'BRANCH_NAME')
}
stages {
stage('test'){
steps {
echo "my branch is " + params.BRANCH_NAME
}
}
}
post {
success{
script {
if( params.BRANCH_NAME == 'master' ){
echo "mail list master"
}
else {
echo "mail list others"
}
}
}
}
}
参数为master时的输出:
[Pipeline] {
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] echo
my branch is master
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
mail list master
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
参数为'test'时的输出:
[Pipeline] {
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] echo
my branch is test
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
mail list others
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
或者为了让它更干净,您可以将脚本作为函数调用:
pipeline {
agent any
parameters {
string(defaultValue: "master", description: 'Which branch?', name: 'BRANCH_NAME')
}
stages {
stage('test'){
steps {
echo "my branch is " + params.BRANCH_NAME
}
}
}
post {
success{
getMailList(params.BRANCH_NAME)
}
}
}
def getMailList(String branch){
if( branch == 'master' ){
echo "mail list master"
}
else {
echo "mail list others"
}
}
有一个 Jenkins 管道。 Need/want 构建成功后发送邮件。通过电子邮件将所有分支发送至 maillist-1 并将主分支的构建过滤至 maillist-master.
我尝试使用 if
和 when
语句步骤,但它们都在 post 块中失败。
pipeline {
agent ...
stages {...}
post{
success{
archiveArtifacts: ...
if( env.BRANCH_NAME == 'master' ){
emailext( to: 'maillist-master@domain.com'
, replyTo: 'maillist-master@domain.com'
, subject: 'Jenkins. Build succeeded :^) '
, body: params.EmailBody
, attachmentsPattern: '**/App*.tar.gz'
)
}
emailext( to: 'maillist-1@domain.com'
, replyTo: 'maillist-1@domain.com'
, subject: 'Jenkins. Build succeeded :^) '
, body: params.EmailBody
, attachmentsPattern: '**/App*.tar.gz'
)
}
}
}
如何实现想要的行为?
目前确实不能在全局 post 块中使用 when
。 When
必须在阶段指令内使用。
使用 if else
是一个合乎逻辑的选择,但您需要在声明性管道中使用一个脚本块来完成这项工作:
pipeline {
agent any
parameters {
string(defaultValue: "master", description: 'Which branch?', name: 'BRANCH_NAME')
}
stages {
stage('test'){
steps {
echo "my branch is " + params.BRANCH_NAME
}
}
}
post {
success{
script {
if( params.BRANCH_NAME == 'master' ){
echo "mail list master"
}
else {
echo "mail list others"
}
}
}
}
}
参数为master时的输出:
[Pipeline] {
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] echo
my branch is master
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
mail list master
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
参数为'test'时的输出:
[Pipeline] {
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] echo
my branch is test
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
mail list others
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
或者为了让它更干净,您可以将脚本作为函数调用:
pipeline {
agent any
parameters {
string(defaultValue: "master", description: 'Which branch?', name: 'BRANCH_NAME')
}
stages {
stage('test'){
steps {
echo "my branch is " + params.BRANCH_NAME
}
}
}
post {
success{
getMailList(params.BRANCH_NAME)
}
}
}
def getMailList(String branch){
if( branch == 'master' ){
echo "mail list master"
}
else {
echo "mail list others"
}
}