【发布时间】:2021-04-17 14:29:52
【问题描述】:
假设我们有一个像这样的简单管道设置:
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'
当然,如果文件存在,则两个阶段都可以正常工作。问题是如果某个阶段失败,则其余阶段的构建将失败。因此,如果 Stage1 因为没有文件而失败,那么它之后的每个阶段都会失败并且它们甚至不会运行,如果 Stage2 失败也是如此,那么我们知道 Stage1 成功但随后 Stage3 及以后的失败并且甚至不运行。
有没有办法让它在cp 命令失败并且cp cannot stat 显示时,跳过这个阶段并继续下一个阶段?或者至少让它只有那个阶段失败,它可以继续构建下一个阶段?
【问题讨论】:
-
检查文件是否存在然后再复制不是更好吗?