bitbucketbitbucket-pipelines

How to write a shell case statement inside a Bitbucket pipeline?


I'm trying to execute a shell case...esac statement inside my Bitbucket pipeline. I have the following. ST_BRANCH and PR_BRANCH are workspace variables, and of course BITBUCKET_PR_DESTINATION_BRANCH is a Bitbucket variable.

definitions:
  caches:
    gradlewrapper: ~/.gradle/wrapper
  steps:
    - step: &build_and_test
        name: Build and Test
        oidc: true
        script:
          - case "$BITBUCKET_PR_DESTINATION_BRANCH" in
              "$ST_BRANCH")
                export restClientGroupId=com.company.staging && export restClientVersion=staging-latest
                ;;
              "$PR_BRANCH")
                export restClientGroupId=com.company && export restClientVersion=production-latest
                ;;
              *)
                export restClientGroupId=com.company.sandbox && export restClientVersion=sandbox-latest
                ;;
            esac
            ./gradlew build integrationTest
        caches:
          - gradlewrapper
          - gradle

pipelines:
  pull-requests:
    '{feature/*,hotfix/*,bugfix/*,patch/*,breaking/*,release/*}':
      - step: *build_and_test

But I get this error when the pipeline runs. What's the correct syntax?

/opt/atlassian/pipelines/agent/tmp/shellScript6744861955930132786.sh: line 5: syntax error: unexpected word

Solution

  • Use the YAML multiline script operator "- |" as @jonrsharpe recommended. Be sure to indent your code block correctly.

            script:
              - |
                case "$BITBUCKET_PR_DESTINATION_BRANCH" in
                    "$ST_BRANCH")
                      export restClientGroupId=com.company.staging && export restClientVersion=staging-latest
                      ;;
                    "$PR_BRANCH")
                      export restClientGroupId=com.company && export restClientVersion=production-latest
                      ;;
                    *)
                      export restClientGroupId=com.company.sandbox && export restClientVersion=sandbox-latest
                      ;;
                 esac
               - ./gradlew build integrationTest