Trigger GitHub Actions for all branches except main, where the only workflow definition file is

Viewed 862

I'm trying to build a repository which allows me to build Docker images for different versions of a project I'm forking. The repo should have the following layout:

  • main branch where the workflow is defined, with a trigger such as:
    on:
       push:
         branches-ignore:
           - main
    
    The workflow builds the software from any branch (basically a mvn clean package, docker build and docker push that applies to all versions of the software)
  • many software-1.2.3 branches which don't contain any .github/workflow files (it would be cumbersome to copy this file into each branch, and maintain it there)

From my research so far, it seems that GitHub Actions only runs when a workflow definition is present. However, I wonder if there's a way using webhooks or something to trick the system into doing what I want.

My next best option would probably be using workflow_dispatch,

1 Answers

on: push !main wouldn't work, because that !main branch would need to have this workflow in it.

Running the workflow manually is the easiest solution to implement.

on:
  workflow_dispatch:
    inputs:
      BRANCH_OF_FORK:
        description: 'branch name, on which this pipeline should run'
        required: true
jobs:
  doSomething:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        ref: ${{ github.event.inputs.BRANCH_OF_FORK }}

      - run: docker build something something

There's a on:fork event, that could be used to trigger a run, but it could only trigger a fork event of this specific branch, that has the on:fork code in it's workflow.

It's possible to run the workflow on a cron job without on-fork event, but you would have to pick the correct branch programmatically.

steps:
 - id: branch-selector
   run: |
     git clone ${{ github.event.repo.url }} .

     all_branches=`git branch --all | grep -v " main\$" | grep -v "/main\$"`
     correct_branch=`pick_correct_branch.py $all_branches`
     git checkout -b $correct_branch

  - run: docker build something something

pick_correct_branch.py is left as an exercise for the reader to implement.

Related