11

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
Chen
  • 113
  • 1
  • 1
  • 6

1 Answers1

20

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.

Michael J
  • 573
  • 4
  • 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 '16 at 06:59
  • @Chen You can also just use the compatible `[` instead of the `bash`-specific `[[`: `[ "${BRANCH_NAME}" = "master" ]` – Martin Tournoij Mar 24 '16 at 07: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 03:08
  • Thanks, I was going crazy with this. Just forgot to add the bash shebang – nmat Dec 16 '16 at 01:36
  • What does `-xe` mean? I saw similar issues of this type but without `-xe` - is that mandatory for Jenkins? – Manuel Jordan Sep 16 '21 at 16:17