How to continue past a failing stage in Jenkins declarative pipeline syntax

后端 未结 4 517
深忆病人
深忆病人 2021-02-01 06:14

I want to define multiple stages in Jenkins declarative pipeline syntax which can continue past any one of them failing. I cannot find any existing questions which are true dupl

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-01 06:57

    I achieved it using post. My requirement was to send out slack notification irrespective of the build status.

    @Library('instanceGroups')
    import x.z.y.jenkins.libraries.SlackNotifier
    
    def slackHelper = new x.z.y.jenkins.libraries.SlackNotifier(env)
    final String projectName = "pomeranian"
    final String featureBranchPattern = "f_"
    
    pipeline {
        agent any
        options { disableConcurrentBuilds() }
    
        stages {
            stage('clean') {
                steps {
                    script {
                        try {
                            echo 'Current Branch...' + env.BRANCH_NAME
                            sh 'rm -rf /var/lib/jenkins/.gradle/caches'
                            sh './gradlew clean'
                        } catch (e) {
                            currentBuild.result = 'FAILURE'
                            slackHelper.buildGenericJobFailureNotificationMessage()
                            throw e
                        }
                    }
                }
            }
    
            stage('compile') {
                steps {
                    script {
                        try {
                            sh "./gradlew compileJava"
                        } catch (e) {
                            currentBuild.result = 'FAILURE'
                            slackHelper.getCompilationFailureSlackNotificationMessage()
                            throw e
                        }
                    }
                }
            }
    
            stage('test') {
                steps {
                    script {
                        try {
                            sh "./gradlew test"
                        } finally {
                            junit 'build/test-results/test/*.xml'
    
                            slackHelper.getTestStatuses(currentBuild)
                            slackHelper.buildUnitTestSlackNotificationMessage()
                        }
                    }
                }
            }
    
            stage('publish 2 nexus') {
                steps {
                    script {
                      // some code
                    }
                }
            }
    
            stage('git tagging') {
                steps {
                    script {
                        // some more code...
                }
            }
        }
    
    
        post {
            always {
                script {
                    slackHelper.finalBuildStatusMessage(currentBuild)
                    slackSend(channel: '#ci-cd', attachments: slackHelper.getFinalSlackMessage())
                }
            }
        }
    }
    

提交回复
热议问题