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" ]]
    ./runUnitTests.sh ${REPOSITORY_NAME} ${BASE_BUILD_CORE} ${BRANCH_NAME} ${BUILD_NUMBER} || echo "The npm may fail but the report exists"

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
  • What does "didn't work" mean? Errors? Doesn't run? – jasonwryan Mar 24 '16 at 6:41

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.

  • 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 '16 at 6:59
  • @Chen You can also just use the compatible [ instead of the bash-specific [[: [ "${BRANCH_NAME}" = "master" ] – Martin Tournoij Mar 24 '16 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 '16 at 3:08
  • Thanks, I was going crazy with this. Just forgot to add the bash shebang – nmat Dec 16 '16 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.