首页 > 解决方案 > 詹金斯管道 - 出错时转到 postFailure 块

问题描述

伙计们,我有下面的 Jenkins 管道。我想运行所有阶段,因此我得到了catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE')到位。我有一个脚本minion_notification.sh生成名为"jk_buildpass.txt"的文件。现在,我希望我的构建在条件if [ !-s jk_buildpass.txt ]匹配。使用出口 1,作业不会失败,并且后续阶段正在执行。任何想法?

pipeline = {
      ansiColor('xterm') {
      FILEVERSION = params.FILEVERSION.trim()
      def remote = [:]
      remote.name = 'xxx'
      remote.host = '10.xxx.xx.x'
      remote.allowAnyHosts = true
            withCredentials([usernamePassword(credentialsId: 'saltmaster', passwordVariable: 'password', usernameVariable: 'ops')]) {
            remote.user = 'ops'
            remote.password = password 
            stage (' Backup') {
              catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {        
              sh './minion_notification.sh "${FILEVERSION}" "Validate" "backupdb"'
              sh(returnStdout: true, script: '''#!/bin/bash
                           if [ ! -s jk_buildpass.txt ];then
                            exit 1
                            else
                            echo "jk_buildpass is not empty"
                            fi
                        '''.stripIndent()) 
                 
               }   
            }
            stage (' Uninstall') {
              catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {         
              sh './minion_notification.sh "${FILEVERSION}" "Validate" "IRMUninstall"'
              sh(returnStdout: true, script: '''#!/bin/bash
                           if [ ! -s jk_buildpass.txt ];then
                           exit 1
                            else
                            echo "jk_buildpass is not empty"
                            fi
                        '''.stripIndent())   
                 
               }   
            }
            
            stage('Run DDMU') {

                 sh './minion_notification.sh "${FILEVERSION}" "jenkins-display"'
            }
            
        }     
         // sh './slk_notify.sh "Upgrade successfully completed for -   ${FILEVERSION} " "*********** " "good" ":jenkins:"
      } 
  } 
postFailure = {
      sh './slk_notify.sh " Upgarde failed for -  ${FILEVERSION} " "danger" ":jenkinserror:"'
}

postAlways = {
    echo 'Cleaning Workspace now'
    env.WORKSPACE = pwd()
    //sh "rm ${env.WORKSPACE}/* -fr"
}

node{
     properties([
     parameters([
            string(name: 'FILEVERSION', defaultValue: '', description: 'Enter the file name to be process ')
             ])
      ]) 
    try {
        pipeline()
    } catch (e) {
        postFailure()
        throw e
    } finally {
        postAlways()
    }
}

标签: bashjenkins-pipeline

解决方案


推荐阅读