Jenkins 管道 - 尝试捕获特定阶段和后续条件步骤
Jenkins pipeline - try catch for particular stage and subsequent conditional step
我正在尝试使用围绕前一阶段的 try / catch 复制 Jenkins 管道中条件阶段的等价物,然后设置成功变量,用于触发条件阶段。
似乎 try catch 块是可行的方法,将成功 var 设置为 SUCCESS 或 FAILED,稍后用作 when 语句的一部分(作为条件阶段的一部分)。
我使用的代码如下:
pipeline {
agent any
stages {
try{
stage("Run unit tests"){
steps{
sh '''
# Run unit tests without capturing stdout or logs, generates cobetura reports
cd ./python
nosetests3 --with-xcoverage --nocapture --with-xunit --nologcapture --cover-package=application
cd ..
'''
currentBuild.result = 'SUCCESS'
}
}
} catch(Exception e) {
// Do something with the exception
currentBuild.result = 'SUCCESS'
}
stage ('Speak') {
when {
expression { currentBuild.result == 'SUCCESS' }
}
steps{
echo "Hello, CONDITIONAL"
}
}
}
}
我收到的最新语法错误如下:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup
failed:
WorkflowScript: 4: Expected a stage @ line 4, column 9.
try{
我也尝试了很多变体。
我是不是用错了方法?这似乎是一个相当普遍的要求。
谢谢。
这可能会解决您的问题,具体取决于您的目的。当前面的阶段成功时,阶段只有 运行,所以如果你实际上有两个阶段,就像你的例子一样,如果你希望第二个阶段只有 运行 当第一个阶段成功时,你要确保当测试失败时,第一阶段适当失败。捕捉将防止(理想的)失败。 finally会保留失败的,还可以用来抓取你的测试结果。
所以这里,第二阶段只会运行当测试通过时,不管测试结果都会被记录:
pipeline {
agent any
stages {
stage("Run unit tests"){
steps {
script {
try {
sh '''
# Run unit tests without capturing stdout or logs, generates cobetura reports
cd ./python
nosetests3 --with-xcoverage --nocapture --with-xunit --nologcapture --cover-package=application
cd ..
'''
} finally {
junit 'nosetests.xml'
}
}
}
}
stage ('Speak') {
steps{
echo "Hello, CONDITIONAL"
}
}
}
}
请注意,我实际上是在声明性管道中使用 try
,但是 ,你不能直接使用 try(你必须将任意 groovy 代码包装在脚本步骤)。
我正在尝试使用围绕前一阶段的 try / catch 复制 Jenkins 管道中条件阶段的等价物,然后设置成功变量,用于触发条件阶段。
似乎 try catch 块是可行的方法,将成功 var 设置为 SUCCESS 或 FAILED,稍后用作 when 语句的一部分(作为条件阶段的一部分)。
我使用的代码如下:
pipeline {
agent any
stages {
try{
stage("Run unit tests"){
steps{
sh '''
# Run unit tests without capturing stdout or logs, generates cobetura reports
cd ./python
nosetests3 --with-xcoverage --nocapture --with-xunit --nologcapture --cover-package=application
cd ..
'''
currentBuild.result = 'SUCCESS'
}
}
} catch(Exception e) {
// Do something with the exception
currentBuild.result = 'SUCCESS'
}
stage ('Speak') {
when {
expression { currentBuild.result == 'SUCCESS' }
}
steps{
echo "Hello, CONDITIONAL"
}
}
}
}
我收到的最新语法错误如下:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup
failed:
WorkflowScript: 4: Expected a stage @ line 4, column 9.
try{
我也尝试了很多变体。
我是不是用错了方法?这似乎是一个相当普遍的要求。
谢谢。
这可能会解决您的问题,具体取决于您的目的。当前面的阶段成功时,阶段只有 运行,所以如果你实际上有两个阶段,就像你的例子一样,如果你希望第二个阶段只有 运行 当第一个阶段成功时,你要确保当测试失败时,第一阶段适当失败。捕捉将防止(理想的)失败。 finally会保留失败的,还可以用来抓取你的测试结果。
所以这里,第二阶段只会运行当测试通过时,不管测试结果都会被记录:
pipeline {
agent any
stages {
stage("Run unit tests"){
steps {
script {
try {
sh '''
# Run unit tests without capturing stdout or logs, generates cobetura reports
cd ./python
nosetests3 --with-xcoverage --nocapture --with-xunit --nologcapture --cover-package=application
cd ..
'''
} finally {
junit 'nosetests.xml'
}
}
}
}
stage ('Speak') {
steps{
echo "Hello, CONDITIONAL"
}
}
}
}
请注意,我实际上是在声明性管道中使用 try
,但是