5

I have a script and a condition where the branch name changes based on which branch you're using.

test:ui:with_sauce:
  ...
  script:
    - export MASTER_URL=https://masterurlexample.io
    - export TEST_PREVIEW_APP=$CI_COMMIT_REF_SLUG
    - cd $MAVEN_DIRECTORY
    - if [ "$CI_COMMIT_BRANCH" == "master" || "$EMULATE_BRANCH" == "master" ]; then
        export TEST_PREVIEW_APP=$MASTER_URL;
      fi;
    - echo "Testing on $TEST_PREVIEW_APP"
    - echo "starting test"
    - sleep 30
    - mvn -U $MAVEN_CLI_OPTS ...

When this job runs I don't believe the condition doesn't execute.

/bin/bash: line 210: [: missing `]'
/bin/bash: line 210: : command not found

Not sure if it's looking for specific quotes around the variables.

1 Answer 1

5

Either write the expression in a single line or use a multiline string - |.

There's also a small issue with your bash (you need [[ and ]])

    - | 
      if [[ "$CI_COMMIT_BRANCH" == "master" || "$EMULATE_BRANCH" == "master" ]]; then
        export TEST_PREVIEW_APP=$MASTER_URL
      fi
    - ...

or

    - if [[ "$CI_COMMIT_BRANCH" == "master" || "$EMULATE_BRANCH" == "master" ]]; then export TEST_PREVIEW_APP=$MASTER_URL; fi
4
  • after making it a single line it now shows this error line 210: : command not found
    – Paul Blart
    Commented Jul 14, 2022 at 16:56
  • @PaulBlart that's a bash syntax error. You need to double up your brackets. I'll update the answer.
    – sytech
    Commented Jul 14, 2022 at 17:04
  • Thanks @sytech for the quick responses. Adding the double brackets worked. Thanks!
    – Paul Blart
    Commented Jul 14, 2022 at 17:12
  • Can you add a "needs" within the if condition @sytech?
    – Paul Blart
    Commented Jul 18, 2022 at 23:26

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