Home > Software engineering >  How to execute an if condition within a yaml in gitlab ci
How to execute an if condition within a yaml in gitlab ci

Time:07-15

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.

CodePudding user response:

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
  • Related