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

Linux 如果复制失败,Jenkins管道将跳过阶段,linux,jenkins,groovy,Linux,Jenkins,Groovy,假设我们有一个简单的管道设置,如下所示: pipeline { stages { stage('Stage1') { sh ''' echo 'Copying files' cp ./file1 ./directory1 ''' } stage('Stage2') { sh ''' echo 'This stage should sti

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

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无法统计。/file1./directory1'
'cp无法统计。/directory2/files./directory2/子目录'

当然,如果文件存在,这两个阶段都可以很好地工作。问题是,如果某个阶段失败,则构建在其余阶段失败。因此,如果Stage1因为没有文件而失败,那么它会在每个阶段之后失败,它们甚至不会运行,如果Stage2失败,那么我们知道Stage1成功了,但是Stage3和以后的阶段失败了,甚至不会运行


如果
cp
命令失败且
cp无法统计
显示,是否有办法跳过该阶段并进入下一阶段?或者至少使其仅使该阶段失败,并且可以继续构建下一个阶段?

这可以通过使用
catchError来实现

管道{
任何代理人
舞台{
阶段('1'){
台阶{
剧本{
catchError(buildResult:'SUCCESS',stagerResult:'FAILURE'){
回显“复制文件”
cp./file1./directory1
}
}
}
}
阶段('2'){
台阶{
剧本{
catchError(buildResult:'SUCCESS',stagerResult:'FAILURE'){
echo“此阶段仍应工作并运行”
cp./directory2/文件。/directory2/子目录
}
}
}
}
阶段('3'){
台阶{
sh“出口0”
}
}
}
}
从上面的管道脚本中,将执行所有阶段。如果
cp
命令对阶段1或阶段2都不起作用,则该特定阶段将显示为失败,但其余所有阶段都将执行

类似于下面的屏幕截图:


修改后的答案

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

只能在
catchError
中包含要捕获错误的命令


管道{
任何代理人
舞台{
阶段('1'){
台阶{
嘘
回声“你好,世界!!!!!!!”
卷曲https://www.google.com/
""" 
catchError(buildResult:'SUCCESS',stagerResult:'FAILURE'){
回显“复制文件”
cp./file1./directory1
}
}
}
阶段('2'){
台阶{
catchError(buildResult:'SUCCESS',stagerResult:'FAILURE'){
echo“此阶段仍应工作并运行”
cp./directory2/文件。/directory2/子目录
}
}
}
阶段('3'){
台阶{
sh“出口0”
}
}
}
}

在尝试使用以下条件复制文件之前,只需检查文件是否存在:

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') { ... }
      ...
   }
}
[-f./directory2/files]&&cp./directory2/files./directory2/子目录| | echo“文件不存在”


以下是一种在文件不存在时跳过阶段的简单方法,使用
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步骤中指定两次路径,最好以另一种方式处理它,例如使用变量或闭包。
由于声明性管道中的限制,我建议您改用脚本管道。

检查文件是否存在,然后才进行复制不是更好吗?脚本{}的原因是什么?在这种情况下,您可以或不能使用
脚本{}
。它用于在声明性管道中执行脚本。完全取决于你是否想要
script{}
。啊,所以我忘了包括我有一个
sh''.'''在每个阶段。您是否可以更新您的代码,使其包含在我的
sh''…''中。如果我有一个
sh''''
正确,我想你不需要脚本{}?另外,如果在
cp
命令上方有其他bash/linux命令,那么我是将其包含在catchError中,还是仅将
cp
行包含在catchError中?不,它不需要
script{}
才能运行
sh''
。此外,您还可以使用
catchError
添加那些希望捕获错误的bash/linux命令。在这里,您只能在
catchError
中使用
cp
命令。我修改了答案。