Search code examples
gitgithubyamlgithub-actions

Why does this job run on GitHub regardless of my conditional


I created a one job in my yaml file. It has a conditional that only allows the job to run if we are on a cat1 or cat2 branch. Here is my yaml file

name: cat actions
on:
  push:
  workflow_dispatch:

concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: true

env:
  BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
  
jobs:
  cat:
    if: ${{ github.head_ref || github.ref_name }} == 'cat1' || startsWith(${{ github.head_ref || github.ref_name }}, 'cat2')
    name: Publish-cat
    runs-on: [ cat ]
    steps:
      - name: checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Display GitHub Context
        run: |
          echo "Head Ref: ${{ github.head_ref }}"
          echo "Ref Name: ${{ github.ref_name }}"
          echo "Ref Name: ${{ github.head_ref || github.ref_name }}"

When I ran this on my dog1 branch, it didn't ignore it in my conditional. How is this possible? I even saw the output of those values with echo and it clearly shows that my branch name is dog1. What I do know is I can have a conditional in that spot because I had done so before with just github.ref_name and that worked for a while until it gave a weird behavior where after I would merge my feature branch into cat1 branch, it would skip the branch and I theorized that it has something to do with github.ref_name not having a value when you do a merge. In any case if anyone has any suggestions please feel free to let me know


Solution

  • You have syntax errors as you don't need to use ${{ }} in if condition. Please try this if: (github.head_ref || github.ref_name) == 'cat1' || startsWith((github.head_ref || github.ref_name), 'cat2')