简体   繁体   中英

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                      
                }
            }
        }
    }

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)

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                      
                }
            }
        }
    }

You can defined a func like this:

void parallelRunner(Map parallelSteps = [:], Closure body) {
    def pjs = parallelSteps.collectEntries { stageName, config ->
        ["${stageName}" : {
            stage("${stageName}") {
                body(config)
            }
        }]
    }

    parallel pjs.plus(([failFast: false]))
}

and invoke it

def parallelMap = [
    parallel-runner-1: [
       // there are some configs
       git: gitConfig,
       k8s: k8sConfig,
    ],
    parallel-runner-2: [
       // there are some configs
       git: gitConfig,
       k8s: k8sConfig,
    ],
]

parallelRunner(parallelSteps: parallelMap) { config ->
    installXRpm config: [
        git: config.git,
        k8s: config.k8s,
    ]
}

Two parallel jobs with different parameters will be run (job parallel-runner-1 and job parallel-runner-2)

If you're using a scripted pipeline then you need to add the failFast into the parallel step like so -

    stage('SomeStage') {
    parallel (
        "Process1" : { //do something },
        "Process2" : { //do something else },
        failFast: true
    )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM