Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use failFast in dynamic pipeline in Jenkins

I have pipeline which has dynamic parallel stages and I want my pipeline to fail fast, if any of the stage fail. I tried to add failFast: true but my pipeline is stuck at "Failed at Stage ABC".

  stage("Deploy") {             
        steps {
            script {
                def stages = createStages("Name", "Project")
                fastFail: true
                for (stage in stages) {                        
                    parallel stage                      
                }
            }
        }
    }
like image 902
aditi gupta Avatar asked Sep 20 '19 10:09

aditi gupta


People also ask

What is failFast in Jenkins?

Solution: Use failFast flag on Jenkins pipeline. From Documentation: You can force your parallel stages to all be aborted when one of them fails, by adding failFast true to the stage containing the parallel.

How do I add a choice parameter in Jenkins pipeline?

Go to Jenkins Home, select New Item, add a name for your Job, for the project type, select Pipeline project and click on Ok. On the configure job page select the This project is parameterized checkbox in the general tab. Now, we will add an Active Choices Parameter which renders our Application Tiers as a Dropdown.

How do you pass a parameter in Jenkins pipeline script?

Using build parameters, we can pass any data we want: git branch name, secret credentials, hostnames and ports, and so on. Any Jenkins job or pipeline can be parameterized. All we need to do is check the box on the General settings tab, “This project is parameterized”: Then we click the Add Parameter button.

How do you skip stage in Jenkins scripted pipeline?

The way to do this is by using Jenkins' module that can be found here. So to mark a stage as skipped you need to call static method markStageSkippedForConditional passing the name of the stage you are skipping.


2 Answers

Solution: Use failFast flag on Jenkins pipeline.

From Documentation: You can force your parallel stages to all be aborted when one of them fails, by adding failFast true to the stage containing the parallel.

Pay attention that all jobs would be triggered and quit (if one fails) if the agent node was started in each one of them (if job 'a' in pipeline fails but job 'b' is still looking for node and not started yet, it will continue - [this is an edge case]).

Examples - The options are :

1.Use parallelsAlwaysFailFast method in your options pipeline:

pipeline {
agent any
options {
    parallelsAlwaysFailFast()
}
stages {
    stage('Non-Parallel Stage') {
        steps {
            echo 'This stage will be executed first.'
        }
    }
    stage('Parallel Stage') {
        when {
            branch 'master'
        }
        parallel {
            stage('Branch A') {
                agent {
                    label "for-branch-a"
                }
                steps {
                    echo "On Branch A"
                }
            }
            stage('Branch B') {
                agent {
                    label "for-branch-b"
                }
                steps {
                    echo "On Branch B"
                }
            }
            stage('Branch C') {
                agent {
                    label "for-branch-c"
                }
                stages {
                    stage('Nested 1') {
                        steps {
                            echo "In stage Nested 1 within Branch C"
                        }
                    }
                    stage('Nested 2') {
                        steps {
                            echo "In stage Nested 2 within Branch C"
                        }
                    }
                }
            }
        }
    }
}

2.Use before parallel using failFast true

stage('Parallel Stage') {
        when {
            branch 'master'
        }
        failFast true
        parallel {

3.Configure jobs in map and execute with failFast attribute on.

 jobsList = [
    {job: 'jobA', parameters: [booleanParam(name: 'flag', value: true)]},
    {job: 'jobB', parameters: [booleanParam(name: 'flag', value: true)]}
 ]

 jobsList.failFast = true
 parallel(jobsList)
like image 174
avivamg Avatar answered Oct 13 '22 23:10

avivamg


I couldn't reply to the answer provided by @avivamg but I wasn't able to use his/her solution directly. This worked for me:

stages.failFast = true
parallel stages

Or in your case:

stage("Deploy") {             
        steps {
            script {
                def stages = createStages("Name", "Project")
                stages.fastFail = true
                // I'm not sure if the for loop will work as failFast is on the map
                // so if that doesn't work then you could use this instead:
                // parallel stages
                for (stage in stages) {                        
                    parallel stage                      
                }
            }
        }
    }
like image 24
Gabe Villarreal Avatar answered Oct 13 '22 23:10

Gabe Villarreal