How to check variable branch name

As part of my build job, I’m looking to perform certain steps only if the branch that was pushed to is of a certain name. Unfortunately those names are variable, but they do hold a pattern. I’ve seen the post here for matching tags, but that has multiple jobs, and I would prefer to keep it down to 1, and keep the checks in the steps, if at all possible.

The branches that I’m looking to check against are refs/heads/main, and refs/heads/project-x.y.z. The catch to this is that for the project- branches, x and y will always be present, but z may not be. Examples being:

project-1.5.7
project-1.12.0
project-3.8

I have attempted to do:

steps:
  ...
  - name: Restricted Step
    if: github.ref == 'refs/heads/main` || github.ref == 'refs/head/project-' + [0-9] + '.' + [0-9]*
    ...

which results in an failure for the action. I have also tried:

steps:
  ...
  - name: Get branch
    run: |
      if [ ${{ github.ref }} == refs/heads/main ] then
        echo "::set-env name=valid_branch::true"
      else
        if [ ${{ github.ref }} == refs/heads/project-[0-9].[0-9]* ] then
          echo "::set-env name=valid_branch::true"
        else
          echo "::set-env name=valid_branch::false"
        fi
      fi
  - name: Restricted step
    if: ${{ env.valid_branch }} == 'true'

which gives me an error: line 3: syntax error near unexpected token 'else'

However I could get the current branch name and check it against the variable branch names would be great, especially if it’s all in the same job and set of steps.

Thanks!

The second approach (with set-env) should work in principle, but there are a bunch of Bash syntax errors in the code:

  • There needs to be a semicolon after the end of the [ or [[ command, before the `then.
  • Pattern matching with == works only with the [[ command, not [.
  • For literal string comparison use =, not ==.

So I think this’ll work, if there aren’t additional issues that I missed:

    run: |
      if [ ${{ github.ref }} = refs/heads/main ]; then
        echo "::set-env name=valid_branch::true"
      else
        if [[ ${{ github.ref }} == refs/heads/project-[0-9].[0-9]* ]]; then
          echo "::set-env name=valid_branch::true"
        else
          echo "::set-env name=valid_branch::false"
        fi
      fi
1 Like

I ran with your changes, and that did fix the bash errors. Thanks a bunch for that, bash scripting is still a learning thing for me.

Now I’ve got a downstream issue, where that restricted step is always running, even on branches that aren’t main or matching the pattern project-[0-9].[0-9]*. For example, using the branch update-workflow, the restricted step from the original post, where it runs if: ${{ env.valid_branch }} == 'true', is running, even on the workflow branch.

I’m guessing either something is being set to always be true, or I’m missing somethine else. For reference, there are no additional steps between the one that gets the branch name, and the restircted step.

Thanks again!

1 Like

Hi @mciolkosz,

You need to remove ${{}} or add it to whole expression, then it will work as expected.

if: env.valid_branch == 'true'
or
if: ${{ env.valid_branch == 'true' }}

For your expression code, if you enable debug logging, you will find how it is evaluated, it’s always true.

Thanks

1 Like