try { sh 'revive -config revive.toml ./...' } catch (Exception e) { currentBuild.result = 'FAILURE' error("revive 代码校验未通过,终止流水线") } 不能终止流水线 完成的流水线脚本
时间: 2024-02-21 08:01:20 浏览: 27
根据您提供的信息,可能是因为 `sh` 命令返回的退出码不是非零值,导致 Jenkins 不认为此次执行是失败的。您可以尝试使用 `returnStatus` 参数来获取命令的退出码,并根据退出码来判断是否终止流水线,具体步骤如下:
```
def reviveResult = sh(returnStatus: true, script: 'revive -config revive.toml ./...')
if (reviveResult != 0) {
currentBuild.result = 'FAILURE'
error("revive 代码校验未通过,终止流水线")
}
```
这里使用 `sh` 命令的 `returnStatus` 参数来获取命令的退出码,然后使用 `if` 语句判断退出码是否为非零值。如果退出码为非零值,则将当前流水线的结果设置为 FAILURE,并使用 `error` 函数抛出异常信息,以便在 Jenkins 控制台中显示错误信息。
请注意,这里使用的是 `def` 关键字来定义一个变量 `reviveResult`,以便在后续的判断语句中使用。
相关问题
try { sh 'revive -config revive.toml ./...' } catch (Exception e) { currentBuild.result = 'FAILURE' error("revive 代码校验未通过,终止流水线") } 不能终止流水线
如果您的 Jenkins 流水线在执行 revive 代码校验时出现错误,但是没有终止流水线,可能是因为 revive 命令返回了非零的退出码,导致 Jenkins 不认为此次执行是失败的。您可以通过设置 `sh` 命令的 `returnStatus` 参数为 `true`,并使用 `if` 语句判断命令是否执行成功来解决这个问题,具体步骤如下:
```
def reviveResult = sh(returnStatus: true, script: 'revive -config revive.toml ./...')
if (reviveResult != 0) {
currentBuild.result = 'FAILURE'
error("revive 代码校验未通过,终止流水线")
}
```
这里使用 `sh` 命令的 `returnStatus` 参数来设置返回命令的退出码而不是命令的输出结果。然后,使用 `if` 语句判断命令的退出码是否为 0。如果不是 0,则将当前流水线的结果设置为 FAILURE,并使用 `error` 函数抛出异常信息,以便在 Jenkins 控制台中显示错误信息。
请注意,这里使用的是 `def` 关键字来定义一个变量 `reviveResult`,以便在后续的判断语句中使用。
def reviveResult = sh(returnStatus: true, script: 'revive -config revive.toml ./...') if (reviveResult != 0) { currentBuild.result = 'FAILURE' error("revive 代码校验未通过,终止流水线") } 不能终止流水线
您好,您的第一个问题是关于Jenkins Pipeline中不能终止流水线的问题。根据您提供的代码片段,这段代码的作用是执行revive命令来校验代码,并且如果校验未通过,则将当前构建的结果设置为“FAILURE”并抛出异常,但是并没有终止流水线的操作。
如果您想要在代码校验未通过时终止流水线,您可以使用Jenkins Pipeline提供的error函数来抛出异常并终止流水线。修改代码如下:
```
def reviveResult = sh(returnStatus: true, script: 'revive -config revive.toml ./...')
if (reviveResult != 0) {
error("revive 代码校验未通过,终止流水线")
}
```
这样,当revive命令校验未通过时,将会抛出异常并终止流水线。希望能够解决您的问题。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)