Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run github action when pushing to a certain branch

I want to run an action to check if my code compiles whenever I push to dev branch. But since my actions are in main it never runs whenever I push to dev branch. I have tried to understand the documentation but it really isn't any help. Here is the code:

name: check-compile
on:
  push:
    branches: 
    - dev
like image 585
zactrixo Avatar asked Sep 03 '26 20:09

zactrixo


1 Answers

There are many ways to run workflow for specific branches.

First option

The one you wrote only work if dev is the default branch, to make it work with other ref/heads you need to add ' around the branch name:

name: check-compile
on:
  push:
    branches: 
     - 'dev'

More information here

Note: It will only work with this implementation if the workflow .yml file exists on the specific branch.

Second option

You can run your workflow for all push, but perform specific jobs of steps only for specific branches. To do so, you can use the if conditional on the job or the step level:

name: check-compile
on: [push] #to any branch
jobs:
  job:
    runs-on: ubuntu-latest  
    if: github.ref == 'refs/heads/dev' # run this job only for the dev branch
    steps:
      ...

More information here

Third option

You can also filter branches using !, to run the workflow on any branch except the ones informed:

on:
  push:
    branches:
      - '*'
      - '!master'
like image 112
GuiFalourd Avatar answered Sep 07 '26 12:09

GuiFalourd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!