How to perform actions for failed builds in Jenkinsfile

后端 未结 3 1564
心在旅途
心在旅途 2021-01-01 18:21

Is there a way to perform cleanup (or rollback) if the build in Jenkinsfile failed?

I would like to inform our Atlassian Stash instance that the build failed (by doi

3条回答
  •  温柔的废话
    2021-01-01 19:17

    Since 2017-02-03, Declarative Pipeline Syntax 1.0 can be used to achieve this post build step functionality.

    It is a new syntax for constructing Pipelines, that extends Pipeline with a pre-defined structure and some new steps that enable users to define agents, post actions, environment settings, credentials and stages.

    Here is a sample Jenkinsfile with declarative syntax:

    pipeline {
      agent  label:'has-docker', dockerfile: true
      environment {
        GIT_COMMITTER_NAME = "jenkins"
        GIT_COMMITTER_EMAIL = "jenkins@jenkins.io"
      }
      stages {
        stage("Build") {
          steps {
            sh 'mvn clean install -Dmaven.test.failure.ignore=true'
          }
        }
        stage("Archive"){
          steps {
            archive "*/target/**/*"
            junit '*/target/surefire-reports/*.xml'
          }
        }
      }
      post {
        always {
          deleteDir()
        }
        success {
          mail to:"me@example.com", subject:"SUCCESS: ${currentBuild.fullDisplayName}", body: "Yay, we passed."
        }
        failure {
          mail to:"me@example.com", subject:"FAILURE: ${currentBuild.fullDisplayName}", body: "Boo, we failed."
        }
      }
    }
    

    The post code block is what handles that post step action

    Declarative Pipeline Syntax reference is here

提交回复
热议问题