运行 Jenkins 管道时如何修复 'script returned exit code 1'
How to fix 'script returned exit code 1' when running Jenkins pipeline
我正在使用 Jenkins Blue Ocean 定义管道。
我正在尝试做一个简单的 python pep8 编码约定,但如果我进入 shell 并直接键入命令,它运行正常。
但是在管道中执行同样的命令时,执行了,但是在最后
'script returned exit code 1' 显示。
由于这个错误代码,它不会进入下一步。
有解决办法吗?
using credential github
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/YunTaeIl/jenkins_retest.git # timeout=10
Cleaning workspace
> git rev-parse --verify HEAD # timeout=10
Resetting working tree
> git reset --hard # timeout=10
> git clean -fdx # timeout=10
Fetching without tags
Fetching upstream changes from https://github.com/YunTaeIl/jenkins_retest.git
> git --version # timeout=10
using GIT_ASKPASS to set credentials GitHub Access Token
> git fetch --no-tags --progress -- https://github.com/YunTaeIl/jenkins_retest.git +refs/heads/master:refs/remotes/origin/master # timeout=10
Checking out Revision fe49ddf379732305a7a50f014ab4b25f9382c913 (master)
> git config core.sparsecheckout # timeout=10
> git checkout -f fe49ddf379732305a7a50f014ab4b25f9382c913 # timeout=10
> git branch -a -v --no-abbrev # timeout=10
> git branch -D master # timeout=10
> git checkout -b master fe49ddf379732305a7a50f014ab4b25f9382c913 # timeout=10
Commit message: "Added Jenkinsfile"
> git rev-list --no-walk bc12a035337857b29a4399f05d1d4442a2f0d04f # timeout=10
Cleaning workspace
> git rev-parse --verify HEAD # timeout=10
Resetting working tree
> git reset --hard # timeout=10
> git clean -fdx # timeout=10
+ ls
Jenkinsfile
README.md
jenkins-retest
+ python3.7 --version
Python 3.7.3
+ python3.7 -m flake8 jenkins-retest
jenkins-retest/N801_py3.py:3:1: E302 expected 2 blank lines, found 0
jenkins-retest/N801_py3.py:6:1: E302 expected 2 blank lines, found 0
jenkins-retest/N801_py3.py:9:1: E302 expected 2 blank lines, found 0
jenkins-retest/N801_py3.py:12:1: E302 expected 2 blank lines, found 0
jenkins-retest/N801_py3.py:15:1: E302 expected 2 blank lines, found 0
jenkins-retest/N801_py3.py:18:1: E302 expected 2 blank lines, found 0
jenkins-retest/N801_py3.py:24:1: E303 too many blank lines (4)
jenkins-retest/N801_py3.py:24:11: E999 SyntaxError: invalid syntax
jenkins-retest/python_testfile.py:1:1: E999 SyntaxError: invalid syntax
jenkins-retest/python_testfile.py:1:2: E228 missing whitespace around modulo operator
jenkins-retest/python_testfile.py:3:1: E402 module level import not at top of file
jenkins-retest/python_testfile.py:3:20: W291 trailing whitespace
jenkins-retest/python_testfile.py:5:1: E302 expected 2 blank lines, found 1
jenkins-retest/python_testfile.py:8:1: E305 expected 2 blank lines after class or function definition, found 0
jenkins-retest/python_testfile.py:11:33: W291 trailing whitespace
jenkins-retest/python_testfile.py:12:1: E402 module level import not at top of file
jenkins-retest/python_testfile.py:12:19: W291 trailing whitespace
jenkins-retest/python_testfile.py:14:4: E714 test for object identity should be 'is not'
jenkins-retest/python_testfile.py:17:16: W291 trailing whitespace
jenkins-retest/python_testfile.py:18:80: E501 line too long (95 > 79 characters)
script returned exit code 1
我遇到了同样的问题,批处理脚本调用了一个可执行文件,其 return 状态在成功时为 1,在错误时为 0。
对于 Jenkins 来说,这是一个问题,成功错误代码为 0,任何其他状态代码均表示失败,因此停止作业并显示以下消息:script returned exit code 1
我的解决方法:检查最后一个错误代码并反转脚本的 return 值:
stages {
stage("My stage") {
steps {
bat label: 'My batch script',
script: ''' @echo off
return_1_if_success.exe // command which returns 1 in case of success, 0 otherwise
IF %ERRORLEVEL% EQU 1 (exit /B 0) ELSE (exit /B 1)'''
}
}
}
解释:
IF %ERRORLEVEL% EQU 1 (exit /B 0) ELSE (exit /B 1)
// if previous command returned 1 (meaning success for this command),
// then we exit with return code 0 (meaning success for Jenkins),
// otherwise we exit with return code 1 (meaning failure for Jenkins)
在 Windows cmd 上,%ERRORLEVEL%
保存在 cmd.exe 终端或批处理脚本中给定点遇到的最后一个错误代码。
对于 PowerShell,您可能想要检查 $?
而不是 ERRORLEVEL
,我会让您检查其他 shell 和平台的等效项。
令我惊讶的是,寻找这个问题的人比我想象的要多。
如果您打算将代码 运行 的错误代码出口 1 作为 shell 脚本忽略,请使用 set +e
。
我正在使用 Jenkins Blue Ocean 定义管道。
我正在尝试做一个简单的 python pep8 编码约定,但如果我进入 shell 并直接键入命令,它运行正常。
但是在管道中执行同样的命令时,执行了,但是在最后 'script returned exit code 1' 显示。 由于这个错误代码,它不会进入下一步。
有解决办法吗?
using credential github
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/YunTaeIl/jenkins_retest.git # timeout=10
Cleaning workspace
> git rev-parse --verify HEAD # timeout=10
Resetting working tree
> git reset --hard # timeout=10
> git clean -fdx # timeout=10
Fetching without tags
Fetching upstream changes from https://github.com/YunTaeIl/jenkins_retest.git
> git --version # timeout=10
using GIT_ASKPASS to set credentials GitHub Access Token
> git fetch --no-tags --progress -- https://github.com/YunTaeIl/jenkins_retest.git +refs/heads/master:refs/remotes/origin/master # timeout=10
Checking out Revision fe49ddf379732305a7a50f014ab4b25f9382c913 (master)
> git config core.sparsecheckout # timeout=10
> git checkout -f fe49ddf379732305a7a50f014ab4b25f9382c913 # timeout=10
> git branch -a -v --no-abbrev # timeout=10
> git branch -D master # timeout=10
> git checkout -b master fe49ddf379732305a7a50f014ab4b25f9382c913 # timeout=10
Commit message: "Added Jenkinsfile"
> git rev-list --no-walk bc12a035337857b29a4399f05d1d4442a2f0d04f # timeout=10
Cleaning workspace
> git rev-parse --verify HEAD # timeout=10
Resetting working tree
> git reset --hard # timeout=10
> git clean -fdx # timeout=10
+ ls
Jenkinsfile
README.md
jenkins-retest
+ python3.7 --version
Python 3.7.3
+ python3.7 -m flake8 jenkins-retest
jenkins-retest/N801_py3.py:3:1: E302 expected 2 blank lines, found 0
jenkins-retest/N801_py3.py:6:1: E302 expected 2 blank lines, found 0
jenkins-retest/N801_py3.py:9:1: E302 expected 2 blank lines, found 0
jenkins-retest/N801_py3.py:12:1: E302 expected 2 blank lines, found 0
jenkins-retest/N801_py3.py:15:1: E302 expected 2 blank lines, found 0
jenkins-retest/N801_py3.py:18:1: E302 expected 2 blank lines, found 0
jenkins-retest/N801_py3.py:24:1: E303 too many blank lines (4)
jenkins-retest/N801_py3.py:24:11: E999 SyntaxError: invalid syntax
jenkins-retest/python_testfile.py:1:1: E999 SyntaxError: invalid syntax
jenkins-retest/python_testfile.py:1:2: E228 missing whitespace around modulo operator
jenkins-retest/python_testfile.py:3:1: E402 module level import not at top of file
jenkins-retest/python_testfile.py:3:20: W291 trailing whitespace
jenkins-retest/python_testfile.py:5:1: E302 expected 2 blank lines, found 1
jenkins-retest/python_testfile.py:8:1: E305 expected 2 blank lines after class or function definition, found 0
jenkins-retest/python_testfile.py:11:33: W291 trailing whitespace
jenkins-retest/python_testfile.py:12:1: E402 module level import not at top of file
jenkins-retest/python_testfile.py:12:19: W291 trailing whitespace
jenkins-retest/python_testfile.py:14:4: E714 test for object identity should be 'is not'
jenkins-retest/python_testfile.py:17:16: W291 trailing whitespace
jenkins-retest/python_testfile.py:18:80: E501 line too long (95 > 79 characters)
script returned exit code 1
我遇到了同样的问题,批处理脚本调用了一个可执行文件,其 return 状态在成功时为 1,在错误时为 0。
对于 Jenkins 来说,这是一个问题,成功错误代码为 0,任何其他状态代码均表示失败,因此停止作业并显示以下消息:script returned exit code 1
我的解决方法:检查最后一个错误代码并反转脚本的 return 值:
stages {
stage("My stage") {
steps {
bat label: 'My batch script',
script: ''' @echo off
return_1_if_success.exe // command which returns 1 in case of success, 0 otherwise
IF %ERRORLEVEL% EQU 1 (exit /B 0) ELSE (exit /B 1)'''
}
}
}
解释:
IF %ERRORLEVEL% EQU 1 (exit /B 0) ELSE (exit /B 1)
// if previous command returned 1 (meaning success for this command),
// then we exit with return code 0 (meaning success for Jenkins),
// otherwise we exit with return code 1 (meaning failure for Jenkins)
在 Windows cmd 上,%ERRORLEVEL%
保存在 cmd.exe 终端或批处理脚本中给定点遇到的最后一个错误代码。
对于 PowerShell,您可能想要检查 $?
而不是 ERRORLEVEL
,我会让您检查其他 shell 和平台的等效项。
令我惊讶的是,寻找这个问题的人比我想象的要多。
如果您打算将代码 运行 的错误代码出口 1 作为 shell 脚本忽略,请使用 set +e
。