如何使用 jenkins 声明式管道添加条件构建后操作?
How to add conditional postbuild actions using jenkins Declarative Pipeline?
我需要在作业触发 post构建操作之前检查条件
我可以看到管道中的 post 部分支持始终、更改、失败、成功、不稳定和中止。作为 post 构建条件。但我想检查 post 构建操作中的另一个条件。我尝试使用 when{},但 post 构建操作不支持它。
pipeline {
agent any
stages {
stage('Example') {
steps {
sh 'mvn --version'
}
}
}
post {
}
}
如你所说。 when
选项在 post 部分中不可用。要创建条件,您只需在 post 部分中创建一个脚本块,如本例所示:
pipeline {
agent { node { label 'xxx' } }
environment {
STAGE='PRD'
}
options {
buildDiscarder(logRotator(numToKeepStr: '3', artifactNumToKeepStr: '1'))
}
stages {
stage('test') {
steps {
sh 'echo "hello world"'
}
}
}
post {
always {
script {
if (env.STAGE == 'PRD') {
echo 'PRD ENVIRONMENT..'
} else {
echo 'OTHER ENVIRONMENT'
}
}
}
}
}
当 STAGE 的环境变量为 PRD 时,它将在 post 部分打印 PRD ENVIRONMENT
。如果不是,它将打印:DIFFERENT ENVIRONMENT
.
运行 与 STAGE='PRD'
:
[Pipeline] sh
[test] Running shell script
+ echo 'hello world'
hello world
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
PRD ENVIRONMENT..
运行 其中 STAGE='UAT'
(当然你可以使用参数而不是环境变量):
[Pipeline] sh
[test] Running shell script
+ echo 'hello world'
hello world
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
OTHER ENVIRONMENT
[Pipeline] }
[Pipeline] // script
您可以在 post 构建操作中添加一个脚本标签,并且可以使用 if() 来检查您的条件
post {
failure {
script{
if( expression ){
//Post build action which satisfy the condition
}
}
}
}
您还可以在管道中为 阶段 添加 post 构建操作。在这种情况下,您可以在 when
中指定条件
pipeline {
stages {
stage('Example') {
when {
//condition that you want check
}
steps {
}
post { //post build action for the stage
failure {
//post build action that you want to check
}
}
}
}}
我需要在作业触发 post构建操作之前检查条件
我可以看到管道中的 post 部分支持始终、更改、失败、成功、不稳定和中止。作为 post 构建条件。但我想检查 post 构建操作中的另一个条件。我尝试使用 when{},但 post 构建操作不支持它。
pipeline {
agent any
stages {
stage('Example') {
steps {
sh 'mvn --version'
}
}
}
post {
}
}
如你所说。 when
选项在 post 部分中不可用。要创建条件,您只需在 post 部分中创建一个脚本块,如本例所示:
pipeline {
agent { node { label 'xxx' } }
environment {
STAGE='PRD'
}
options {
buildDiscarder(logRotator(numToKeepStr: '3', artifactNumToKeepStr: '1'))
}
stages {
stage('test') {
steps {
sh 'echo "hello world"'
}
}
}
post {
always {
script {
if (env.STAGE == 'PRD') {
echo 'PRD ENVIRONMENT..'
} else {
echo 'OTHER ENVIRONMENT'
}
}
}
}
}
当 STAGE 的环境变量为 PRD 时,它将在 post 部分打印 PRD ENVIRONMENT
。如果不是,它将打印:DIFFERENT ENVIRONMENT
.
运行 与 STAGE='PRD'
:
[Pipeline] sh
[test] Running shell script
+ echo 'hello world'
hello world
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
PRD ENVIRONMENT..
运行 其中 STAGE='UAT'
(当然你可以使用参数而不是环境变量):
[Pipeline] sh
[test] Running shell script
+ echo 'hello world'
hello world
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
OTHER ENVIRONMENT
[Pipeline] }
[Pipeline] // script
您可以在 post 构建操作中添加一个脚本标签,并且可以使用 if() 来检查您的条件
post {
failure {
script{
if( expression ){
//Post build action which satisfy the condition
}
}
}
}
您还可以在管道中为 阶段 添加 post 构建操作。在这种情况下,您可以在 when
中指定条件pipeline {
stages {
stage('Example') {
when {
//condition that you want check
}
steps {
}
post { //post build action for the stage
failure {
//post build action that you want to check
}
}
}
}}