Published on

Executing GitHub Actions step in a Specific Git Branch


Similar to traditional programming, you can also add an if condition for the GitHub Action step.

For example, if you want to run the command only for a specific git branch, you can add the following condition

if:  github.ref == 'refs/heads/main'

What does github.ref mean?

It's a default property which is available in the context when a GitHub action is running. And it'll point to the current branch or tag that has triggered the PR.

This ref is fully-formated. You can learn more about it in their docs


name: Example Workflow

    branches: [develop, main] 

    runs-on: ubuntu-latest
      - name: Checkout
        uses: actions/checkout@v2
      - name: Copy build artifacts to AWS S3
        run: |
          aws s3 sync ${{ github.workspace }}/build s3://example-bucket-name
          echo "Build artifacts are uploaded to AWS S3 ✅"
        if: github.ref == 'refs/heads/main'

Happy adding conditions!