12

I am trying to write a simple If statement on the Execute Shell of Jenkins (1.638). I looked on similar issue and it still didn't work (see below the result output). I tried both [[ and [ and relevant spaces, It appears that Jenkins handles if differently than on regular bash. I even tried the then on the same line as the if with ;. Any idea ?

if [[ "${BRANCH_NAME}" == "master" ]] || [[ "${BRANCH_NAME}" == "master_dev" ]]
then
    ./runUnitTests.sh ${REPOSITORY_NAME} ${BASE_BUILD_CORE} ${BRANCH_NAME} ${BUILD_NUMBER} || echo "The npm may fail but the report exists"
fi

The result on Jenkins is

  • [[ upgrade == master ]] /tmp/hudson11669113852432623.sh: 2: /tmp/hudson11669113852432623.sh: [[: not found
  • [[ upgrade == master_dev ]] /tmp/hudson11669113852432623.sh: 2: /tmp/hudson11669113852432623.sh: [[: not found [core] $ /bin/sh -xe /tmp/hudson7252947297480815560.sh
1
  • What does "didn't work" mean? Errors? Doesn't run?
    – jasonwryan
    Mar 24, 2016 at 6:41

1 Answer 1

21

Have you considered including a bash shebang in your shell?

#!/bin/bash -xe
if [[ "${BRANCH_NAME}" == "master" ]] || [[ "${BRANCH_NAME}" == "master_dev" ]]
....

That will force Jenkins to use your local bash interpreter.

9
  • 1
    Great solution. The only problem is that when I use the shebang, It route the output to a different place and so you can't see if what happened. Jenkins behavior is a Debug mode. So to over come this I had add -x to the shebang. like this : #!/bin/bash -x
    – Chen
    Mar 24, 2016 at 6:59
  • @Chen You can also just use the compatible [ instead of the bash-specific [[: [ "${BRANCH_NAME}" = "master" ] Mar 24, 2016 at 7:28
  • Yes, as you found you'll need to use -x to get the output echoed to your Jenkins log. I find a lot of folks use -xe to get the output and exit on error as well.
    – Michael J
    Mar 29, 2016 at 3:08
  • 1
    @ManuelJordan -x: This option enables debugging by printing each command and its arguments to the standard error output (stderr) before executing it. -e: This option causes the script to exit immediately if any command it runs exits with a non-zero status (i.e., fails). So, when you use #!/bin/bash -xe at the beginning of your script, it will print each command and its arguments to stderr before execution and will exit immediately if any command fails.
    – Michael J
    Oct 10, 2023 at 0:31
  • 1
    @ManuelJordan no problem! I realize now, i didn't really answer your question. :) No, the -xe is not required for Jenkins... but it is very helpful for debugging jobs.
    – Michael J
    Oct 11, 2023 at 17:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .