Prevent special character in variable from executing on echo

I have a GitHub Actions job where I check whether to continue with deployment by checking the contents of the commit message. Deployment happens only when the commit message is exactly this: chore(release): publish.

name: Test & Deploy Packages
      - master

    runs-on: ubuntu-latest
      shouldContinue: ${{ steps.decideContinue.outputs.shouldContinue }}

    - name: Checkout source code
      uses: actions/checkout@v2

    - name: Get GitHub context
        GITHUB_CONTEXT: ${{ toJson(github) }}
      run: echo "$GITHUB_CONTEXT"

    - name: Decide whether to continue based on commit message
      id: decideContinue
      run: |
        commitMessage=$(git log --oneline --format=%B -n 1 ${{ github.event.after }} | head -n 1)
        echo $commitMessage
        if [[$commitMessage == "chore(release): publish"]]; then
          echo "::set-output name=shouldContinue::true"
          echo "publish commit, continuing with deployment"
          echo "::set-output name=shouldContinue::false"
          echo "not a publish commit, skipping deployment"

My next job checks whether needs.check_if_deploy.outputs.shouldContinue is true and continues or skips based on that condition. This seems to be working fine.

However, a command not found error is always printed during execution of the check_if_deploy job. Line 13 in the below screenshot says /home/runner/work/_temp/ line 3: [[chore:: command not found:

I understand this has something to do with the echo on line 3 executing the contents of the variable instead of just printing it but don’t know how to resolve it.

The problem is that there are no spaces after [[ and before ]]. The [[ is a bash command, and without the space it’s combined with the first word in the $commitMessage variable instead, which is no valid command. Additionally string comparison with the [[ or [ commands is just one equals sign. Finally you should quote both sides of the comparison.

So your if needs to be:

        if [[ "$commitMessage" = "chore(release): publish" ]]; then
1 Like

Thank you so much! Never realised that this was the problem.

1 Like