繁体   English   中英

如果复制失败,Jenkins 管道跳过阶段

[英]Jenkins pipeline skip stage if copying fails

假设我们有一个简单的管道设置,如下所示:

pipeline {
   stages {
      stage('Stage1') {
         sh '''
           echo 'Copying files'
           cp ./file1 ./directory1
         '''
      }
      stage('Stage2') {
         sh '''
           echo 'This stage should still work and run'
           cp ./directory2/files ./directory2/subdirectory
         '''
      }
      stage('Stage3') { ... }
      ...
   }
}

每当我在 Stage1 或 Stage2 中没有文件时,它都会使构建失败,说:

'cp cannot stat./file1./directory1''cp cannot stat./directory2/files./directory2/subdirectory'

当然,如果文件存在,则两个阶段都可以正常工作。 问题是,如果阶段失败,则阶段的 rest 的构建会失败。 因此,如果 Stage1 因为没有文件而失败,它在之后的每个阶段都失败并且它们甚至不运行,如果 Stage2 失败也是如此,那么我们知道 Stage1 成功但是 Stage3 及以后的失败并且甚至不运行。

有没有办法做到这一点,如果cp命令失败并且cp cannot stat显示,则跳过该阶段并继续下一个阶段? 或者至少让它只有那个阶段失败,它可以继续构建下一个阶段?

这可以使用catchError来实现

pipeline {
    agent any
    stages {
       
        stage('1') {
            steps {
                script{
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    echo 'Copying files'
                    cp ./file1 ./directory1
                }
              }
            }
        }
        stage('2') {
            steps {
                script{
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    echo 'This stage should still work and run'
                    cp ./directory2/files ./directory2/subdirectory
                }
              }
            }
        }

        stage('3') {
            steps {
                sh 'exit 0'
            }
        }
    }
}

从上面的管道脚本,所有阶段都将执行。 如果cp命令对第 1 阶段或第 2 阶段都不起作用,它将显示该特定阶段失败,但 rest 将执行所有阶段。

类似于下面的截图:

在此处输入图像描述


修改后的答案

以下管道脚本包括sh ''' ''' ,它不必出现在catchError块中。

您只能在catchError中包含您想要捕获错误的那些命令。


pipeline {
    agent any
    stages {

        stage('1') {
            steps {
                sh """
                
                echo 'Hello World!!!!!!!!'
                curl https://www.google.com/
                
                """ 

                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    echo 'Copying files'
                    cp ./file1 ./directory1
                }
            }
        }
        stage('2') {
            steps {
                catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
                    echo 'This stage should still work and run'
                    cp ./directory2/files ./directory2/subdirectory
                }
            }
        }

        stage('3') {
            steps {
                sh 'exit 0'
            }
        }
    }
}

这是一种在文件不存在时跳过该阶段的简单方法,使用when指令:

pipeline {
   agent any   
   stages {
      stage('Stage1') {
         when { expression { fileExists './file1' } }
         steps {
            sh '''
            echo 'Copying files'
            cp ./file1 ./directory1
            '''
         }
      }
      stage('Stage2') {
         when { expression { fileExists './directory2/files' } }
         steps {
            sh '''
            echo 'This stage should still work and run'
            cp ./directory2/files ./directory2/subdirectory
            '''
         }
      }
      stage('Stage3') {
         steps {
            echo "stage 3"
         }
      }
   }
}

在上述情况下,您必须在when指令和 sh 步骤中指定路径两次,最好以另一种方式处理它,例如使用变量或闭包。 由于声明式管道的限制,我建议您改用脚本式管道。

您可以在尝试使用如下条件复制文件之前检查文件是否存在:

[ -f./directory2/files ] && cp./directory2/files./directory2/subdirectory || echo "File does not exist"

来源和更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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