travis-build: preserving the status in $? #3771

Closed
grooverdan opened this Issue Apr 27, 2015 · 8 comments

Projects

None yet

5 participants

@grooverdan

Whether make succeeds or fails $? is always 1.

script:
  - make -j 4
  - COMPILE_RESULT=$?
  - echo COMPILE_RESULT=${COMPILE_RESULT}

output:

..
The command "make -j 4" exited with 0.

$ COMPILE_RESULT=$?
The command "COMPILE_RESULT=$?" exited with 0.

$ echo COMPILE_RESULT=${COMPILE_RESULT}
COMPILE_RESULT=1
@BanzaiMan
Member

Each line in the script section is processed by the travis_cmd bash function. (The value 1 comes from the immediately preceding test [[ -n "$retry" ]] on line 38.)

I understand the motivation for using $? this way (and potentially other special variables) in the script section, but achieving it without breaking the currently working builds may be hard.

I tend to think it is better to document this behavior, and advise writing a custom script if the user wants to do something involving $? in the script section.

@grooverdan

I think anyone who's tried to use $? has come across this limitation and avoided it by writing a custom script (like I should be doing now). Thanks for the prompt response.

@BanzaiMan BanzaiMan added a commit to travis-ci/docs-travis-ci-com that referenced this issue Apr 27, 2015
@BanzaiMan BanzaiMan Add note on $? based on discussion in travis-ci/travis-ci#3771 a5a414b
@BanzaiMan
Member

I've noted this in http://docs.travis-ci.com/user/customizing-the-build/#Note-on-%24%3F. (Improvement suggestions welcome!)

I'm closing this now.

@BanzaiMan BanzaiMan closed this Apr 27, 2015
@weitjong

The trick is, you have to get it right after the command.

make -j4; export COMPILE_RESULT=$?

Though in my .travis.yml I usually just combine the command using "&&" or "||" bash operator or inside another if statement, so I don't really need to use $? explicitly to get the exit status of the earlier command. e.g.:

make -j4 && other-cmd-to-execute-when-make-was-successful

or

make -j4 || other-cmd-to-execute-when-make-was-failed
@grooverdan

@BanzaiMan , while I'm still not convinced that there is a person using $? to extract the value of [[ -n "$retry" ]] the alternative for documentation is here:

- COMPILE_RESULT=0 ; make -j4 || COMPILE_RESULT=$?; expr $COMPILE_RESULT != 0
- [ $COMPILE_RESULT -ge 2 ] && echo makefile error

So the two objectives in deciding this structure where to make $COMPILE_RESULT have the value of the return and to give travis a true/false indicator of the result of the compile. @weitjong suggestions are of course valid (though export is excessive) however sometime additional flexibility is needed.

@BanzaiMan
Member

Improvement to the documentation is also welcome!

@ctubio ctubio referenced this issue in ctubio/ctubio.github.io May 4, 2015
Closed

Fix false positive builds in travis. #12

@l2fprod l2fprod added a commit to IBM-Bluemix/insights-search that referenced this issue Oct 7, 2015
@l2fprod l2fprod use one liner (travis-ci/travis-ci#3771) bc6f6cc
@Artoria2e5 Artoria2e5 referenced this issue in travis-ci/travis-build Oct 15, 2015
Closed

travis_buildtempl_header: Save full PIPESTATUS #528

@ryansmith94 ryansmith94 added a commit to LearningLocker/learninglocker that referenced this issue Feb 23, 2016
@ryansmith94 ryansmith94 Tries using ||. b3f0c34
@ryansmith94

Thanks @weitjong 👍

@wiprodev

Does Travis capture the non zero exit code for init scripts like sudo service xyz start ? In my case though the service fails to start it shows build successful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment