的动态管道中使用

的动态管道中使用

本文介绍了如何在 Jenkins 的动态管道中使用 failFast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有具有动态并行阶段的管道,如果任何阶段发生故障,我希望我的管道快速失败.我尝试添加 failFast: true 但我的管道卡在在 ABC 阶段失败".

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

推荐答案

解决方案: 在 Jenkins 管道上使用 failFast flag.

Solution: Use failFast flag on Jenkins pipeline.

来自 文档:您可以强制并行当其中一个失败时,通过将 failFast true 添加到包含并行的阶段,所有阶段都将被中止.

注意如果代理节点在每个作业中启动(如果管道中的作业a"失败但作业b"),所有作业都将被触发并退出(如果一个失败)仍在寻找节点但尚未开始,它将继续 - [这是一个边缘情况]).

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]).

示例 - 选项是:

1.在您的选项管道中使用 parallelsAlwaysFailFast 方法:

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.在并行前使用 failFast true

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

3.在map中配置jobs并开启failFast属性执行.

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)

这篇关于如何在 Jenkins 的动态管道中使用 failFast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 01:26