<p>Just the way you can exit any stage prematurely by using <code>return</code> keyword, you can use the <code>return</code> keyword (outside of the stage) to exit the entire pipeline prematurely. You can re-use the <code>currentBuild.result</code> value outside of <code>Stage 4</code>. See below.</p>

<pre><code>node{
    withEnv([...]){
        stage('1'){
        ...
        }

        stage('2'){
        .....
        }

        if( env.BRANCH_NAME != 'master' ) {
            currentBuild.result = 'SUCCESS'
            return
        }

        stage('3'){
        ...
        }       
    }

    stage('4'){
        withEnv([...]){
            ...
        }
    }

    if(currentBuild.result == 'SUCCESS') {
        return //this will exit the pipeline
    }

    stage('5'){
        ...
    }

    stage('6'){
    ...
    }
}
</code></pre>

CC BY-SA 4.0
2019-04-02T17:13:46.763Z
2019-04-02T17:13:46.763Z
KatariaA
<p>I think you are looking for <strong>error()</strong> function, this question has been answered somehow in: <a href="https://stackoverflow.com/questions/42667600/abort-current-build-from-pipeline-in-jenkins">This question: Abort current build from pipeline in Jenkins</a></p>

CC BY-SA 4.0
2019-04-01T14:15:27.683Z
Does error() accept SUCCESS build status?
sgargel
CC BY-SA 4.0
2019-04-01T14:03:15.533Z
2019-04-01T14:03:15.533Z
Johan Alexis Duque Cadena
<p>In a Jenkins scripted pipeline with the below structure, the <code>return</code> command will exit only the <code>withEnv</code>.</p>

<pre><code>node{
    withEnv([...]){
        stage('1'){
        ...
        }

        stage('2'){
        .....
        }

        if( env.BRANCH_NAME != 'master' ) {
            currentBuild.result = 'SUCCESS'
            return
        }

        stage('3'){
        ...
        }       
    }

    stage('4'){
        withEnv([...]){
            ...
        }
    }

    stage('5'){
        ...
    }

    stage('6'){
    ...
    }
}
</code></pre>

<p>How can I stop the entire pipeline after stage2 with SUCCESS?</p>

<jenkins-pipeline><jenkinsfile>
Cleanest way to prematurely exit a jenkins pipeline from inside a withEnv
sgargel
# Cleanest way to prematurely exit a jenkins pipeline from inside a withEnv

(Asked by: username_0 on 2019-04-01)

In a Jenkins scripted pipeline with the below structure, the return command will exit only the withEnv.
node{
    withEnv([...]){
        stage('1'){
        ...
        }

        stage('2'){
        .....
        }

        if( env.BRANCH_NAME != 'master' ) {
            currentBuild.result = 'SUCCESS'
            return
        }

        stage('3'){
        ...
        }       
    }

    stage('4'){
        withEnv([...]){
            ...
        }
    }

    stage('5'){
        ...
    }

    stage('6'){
    ...
    }
}

How can I stop the entire pipeline after stage2 with SUCCESS?

**Tags:** jenkins-pipeline, jenkinsfile


---

## 2 answers

### Answer by username_1 on 2019-04-02. Score: 2

Just the way you can exit any stage prematurely by using return keyword, you can use the return keyword (outside of the stage) to exit the entire pipeline prematurely. You can re-use the currentBuild.result value outside of Stage 4. See below.
node{
    withEnv([...]){
        stage('1'){
        ...
        }

        stage('2'){
        .....
        }

        if( env.BRANCH_NAME != 'master' ) {
            currentBuild.result = 'SUCCESS'
            return
        }

        stage('3'){
        ...
        }       
    }

    stage('4'){
        withEnv([...]){
            ...
        }
    }

    if(currentBuild.result == 'SUCCESS') {
        return //this will exit the pipeline
    }

    stage('5'){
        ...
    }

    stage('6'){
    ...
    }
}


---

### Answer by username_2 on 2019-04-01. Score: 1

I think you are looking for **error()** function, this question has been answered somehow in: [This question: Abort current build from pipeline in Jenkins](https://stackoverflow.com/questions/42667600/abort-current-build-from-pipeline-in-jenkins)

> **Comments:**
> **username_0**: Does error() accept SUCCESS build status?  

---

