Search code examples
github-actions

When commit is pushed to main, re-run github actions on feature-branch


I have a PR that branches off of main:

enter image description here

Whenever a commit is pushed to main or feature-a, I would like to run github actions on feature-a.

AKA:

enter image description here

Is there a github actions trigger for this?


Read through the github action doc, but haven't found any solutions to this yet.


Solution

  • When your workflow is triggered on the main branch, you could trigger the same workflow on a feature branch:

    name: trigger-main-to-branch
    
    on:
      push:
        branches: '**'
      workflow_dispatch:
    
    jobs:
      main:
        runs-on: ubuntu-latest
        if: github.ref_name == github.event.repository.default_branch
        steps:
          - name: Build main
            run: echo Build main branch
          - name: Trigger feature branch build
            run: |
              echo Trigger feature branch build
              gh workflow run --ref feature-a trigger-main-to-branch
            env:
              GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    
      feature:
        runs-on: ubuntu-latest
        if: github.ref_name != github.event.repository.default_branch
        steps:
          - name: Build feature branch
            run: echo Build feature branch
    

    I am using GitHub CLI gh which is available on GitHub runners. It requires an environment variable GH_TOKEN that has permissions to trigger workflows. Default Github token has this permission.