bkeyser5280
bkeyser5280

Reputation: 63

Abort, rather than error, stage within a Jenkins declarative pipeline

Problem

Our source is a single large repository that contains multiple projects. We need to be able to avoid building all projects within the repository if a commit happens within specific areas. We are managing our build process using pipelines.

Research

The git plugin provides the ability to ignore commits from certain user, paths, and message content. However, as we are using the pipeline, we believe we are experiencing the issue described by JENKINS-36195. In one of the most recent comments, Jesse suggests examining the changeset and returning early if the changes look boring. He mentions that a return statement does not work inside a library, closure, etc), but he doesn't mention how a job could be aborted.

Potential Approaches

  1. We have considered using the error step, but this would result in the job being marked as having a failure and would need to be investigated.
  2. While a job result could be marked as NOT_BUILT, the job is not aborted but continues to process all stages.

Question

How would you abort a job during an early step without marking it as a failure and processing all stages of the pipeline (and potentially additional pipelines)?

Upvotes: 2

Views: 1942

Answers (1)

Mohamed Thoufeeque
Mohamed Thoufeeque

Reputation: 876

Can you try using try catch finally block in the pipeline.

try{

}
catch {

}
finally{

}

I suppose you can also use post build action in the pipeline.

pipeline {
agent any
stages {
    stage('Test') {
        steps {
            sh 'make check'
        }
    }
}
post {
    always {
        junit '**/target/*.xml'
    }
    failure {
        mail to: [email protected], subject: 'The Pipeline failed :('
    }
}

}

The documentation is below https://jenkins.io/doc/book/pipeline/syntax/#post

you can also try using the below one outside of build step with your conditions as specified by Slawomir Demichowicz in the ticket.

if (stash.isJenkinsLastAuthor() && !params.SKIP_CHANGELOG_CHECK) {
    echo "No more new changes, stopping pipeline"
    currentBuild.result = "SUCCESS"
    return
}

I am not sure this could help you.

Upvotes: 1

Related Questions