How to fix 'script returned exit code 1' when running Jenkins pipeline
Asked Answered
I

2

13

I am defining a pipeline using Jenkins Blue Ocean.

I'm trying to do a simple python pep8 coding convention, but if I go inside the shell and type the command directly, it runs fine.

But when the same command is executed in the pipeline, it is executed, but at the end 'script returned exit code 1' is displayed. Because of this error code, it does not go to the next step.

Is there a workaround?

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
Incestuous answered 31/3, 2020 at 5:3 Comment(3)
Why do you want to continue the pipeline after it found issues? Your tool indicates an issue with the return code and Jenkins aborts the pipeline (because of the return value 1). I think your pipeline is working correctly.Manofwar
omg ... It was my illusion. Even if there was an error, I thought that the specified pipeline would be executed and only the part with the error would be displayed. I am really sorry... :(Banka
Yes but when there is "script returned exit code 1" , result is failure. How to convert it into success?Toro
I
15

Use set +e if you intend to ignore the error code exit 1 of code run as a shell script.

Incestuous answered 26/11, 2020 at 2:11 Comment(4)
Exactly thats what I was looking forHirsh
where should i set this value?Tenure
Just put it as a command in your shell script. :)Banka
Note: set +e should be added before calling a command returning non-zeroCristacristabel
C
0

I had the same problem with a batch script calling an executable whose return status was 1 in case of success, and 0 in case of error.

This was a problem for Jenkins as for Jenkins, the success error code is 0 and any other status code means failure so stops the job with the following message: script returned exit code 1

My workaround: check the last error code and invert the return value of the script:

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)'''
        }
    }
}

Explanation:

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)

On Windows cmd, %ERRORLEVEL% holds that last error code encountered in a cmd.exe terminal or at a given point in a batch script.

For PowerShell, you might want to check $? instead of ERRORLEVEL, I'll let you check the equivalent of this for other shell and platform.

Clerical answered 14/10, 2020 at 15:31 Comment(2)
Batch scripts can only be run on Windows nodesSicilia
@Sicilia the equivalent or ERRORLEVEL in Linux is $?, so it can be easily transposedClerical

© 2022 - 2024 — McMap. All rights reserved.