JenkinsPost避坑

post支持根据stage核pipeline执行的结果而进行特定处理,但在post根据stage结果执行指定操作时有深坑

场景:

某个stage失败不影响整体pipeline运行,只需要在stage失败时邮件通知相关人员即可。

思路:

stage中设置catchError和post,当steps运行失败时,将stageResult设置为失败,通过post发送失败邮件通知。

结果:steps执行失败时,catchError将stageResult设置为失败,但failurepost没有触发


 JenkinsPost避坑_第1张图片

 

解决方案:

在failurepost前需要加一个always触发post。

pipeline {
    agent any
    environment{
        toEmailUser='zhenzhen.wu'
		}
    stages {
        stage('Build') {
            steps {
            catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
               sh'aaa'             
            }            
            }
            post {
	        	always {
	        		echo "post condition executed: always ..."
	        	}
			failure {
				step([$class: 'Mailer',
					notifyEveryUnstableBuild: true,
					recipients: 'zhenzhen.wu',
					sendToIndividuals: true])
			  }

        	}
        }

        stage('Deploy') {
            steps {
                sh 'echo Deploy stage ...'
            }
        }
    }
        post {
    	unsuccessful {
    		echo "post condition executed: unsuccessful ..."
    	}
    }
  }

JenkinsPost避坑_第2张图片

 

你可能感兴趣的:(jenkins,groovy,jenkins,groovy)