gpt4 book ai didi

jenkins-pipeline - 基于声明性管道中前一阶段结果的条件执行

转载 作者:行者123 更新时间:2023-12-04 01:02:58 27 4
gpt4 key购买 nike

我的管道设置如下。
enter image description here

我需要让它在遵守以下条件的情况下工作。帮助我定义何时使用块和其他代码以及在哪个阶段使用?

  • 如果 A 失败,则不会执行其他阶段,并且作业运行被标记为失败。
  • 如果任何 B 阶段失败,则不应调用相应的 C 阶段。
  • 当 C1 或 C2 被执行时,阶段 D 应该被执行,而不管它们的执行是否失败。
  • 此外,如果任何阶段失败,则整个作业运行状态应为失败。

  • 我尝试并观察到了什么?
    从上面定义的条件来看,1 和 2 按预期工作,但我的以下尝试不是 3 和 4。

    在 C1 和 C2 中,我添加了 catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE')Continue Jenkins pipeline past failed stage .

    但我观察到的是——
  • 如果 C1 或 C2 失败,则 D 执行,但在所有作业运行中都标记为成功。预期是失败,因为其中一个 C 步骤失败了。但由于捕获它获得成功状态。
  • 当任何 B 阶段失败时,它们对应的 C 也不会执行(预期),但也不会触发 D。因为我需要触发 D,因为已经执行了其他一些 C。
  • 最佳答案

    这是你需要的:

    stageResultMap = [:]

    pipeline {
    agent any
    stages {
    stage('A') {
    steps {
    println("This is stage: ${STAGE_NAME}")
    }
    }
    stage('BC') {
    parallel {
    stage ('1'){
    stages {
    stage('B1') {
    steps {
    script {
    // Catch exceptions, set the stage result as unstable,
    // build result as failure, and the variable didB1Succeed to false
    try {
    sh "exit 1"
    stageResultMap.didB1Succeed = true
    }
    catch (Exception e) {
    unstable("${STAGE_NAME} failed!")
    currentBuild.result = 'FAILURE'
    stageResultMap.didB1Succeed = false
    }
    }
    }
    }
    stage('C1') {
    // Execute only if B1 succeeded
    when {
    expression {
    return stageResultMap.find{ it.key == "didB1Succeed" }?.value
    }
    }
    steps {
    // Mark the stage and build results as failure on error but continue pipeline execution
    catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
    sh "echo Hello"
    }
    }
    }
    }
    }
    stage ('2'){
    stages {
    stage('B2') {
    steps {
    script {
    // Catch exceptions, set the stage result as unstable,
    // build result as failure, and the variable didB2Succeed to false
    try {
    sh "echo Hello"
    stageResultMap.didB2Succeed = true
    }
    catch (Exception e) {
    unstable("${STAGE_NAME} failed!")
    currentBuild.result = 'FAILURE'
    stageResultMap.didB2Succeed = false
    }
    }
    }
    }
    stage('C2') {
    // Execute only if B2 succeeded
    when {
    expression {
    return stageResultMap.find{ it.key == "didB2Succeed" }?.value
    }
    }
    steps {
    // Mark the stage and build results as failure on error but continue pipeline execution
    catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
    sh "echo Hello"
    }
    }
    }
    }
    }
    }
    }
    stage('D') {
    // Execute only when C1 or C2 have executed, that is B1 or B2 have succeeded
    when {
    expression {
    return stageResultMap.any {it.value}
    }
    }
    steps {
    println("This is stage: ${STAGE_NAME}")
    }
    }
    }
    }
  • 对于阶段 C1 和 C2,使用 catchError(buildResult: 'FAILURE', stageResult: 'FAILURE')将阶段和构建结果标记为 FAILURE 但继续管道执行。

  • enter image description here
    enter image description here
  • 对于阶段 B1 和 B2,首先初始化一个空映射 stageResultMap = [:]在 Jenkinsfile 的顶部以捕获每个阶段的结果。现在,对于每个 Bn阶段,使用 try-catch阻止以便成功时唯一的键值对 stageResultsMap.didBnSucceed = true被添加到 map 中,并且在异常情况下阶段结果设置为 UNSTABLE,构建结果设置为 FAILURE,和 stageResultsMap.didBnSucceed = false .我们使用的方法是unstable这里是因为其他两种方法都没有catchErrorwarnError让我们添加到 map 上。然后,在相应的 Cn 阶段,评估映射并仅在 Bn 成功时才执行。此外,如果 B1 和 B2 都失败,即 C1 和 C2 都没有执行,D 也不会执行,因为 when {expression {return stageResultMap.any {it.value}}}将评估为 false .但是如果 C1 或 C2 执行而不管失败,D 都会执行。

  • enter image description here
    enter image description here

    在上述两种情况下,如果任何阶段失败,整体构建状态将被标记为 FAILURE。

    当然,绿色构建没有失败。

    enter image description here

    关于jenkins-pipeline - 基于声明性管道中前一阶段结果的条件执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57830920/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com