6

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,

Robert Metzger
  • 4,452
  • 23
  • 50
  • 1
    If you have the workflow in `main` and then create new branches, all the branches contain the workflow, no? – Benjamin W. Sep 15 '21 at 08:10
  • No, the branches are not forked off from `main`. They are forks from an external repository. Here's an example: https://github.com/rmetzger/flink-docker-factory. I would like to run the workflow defined in `master` on every push to `flink-1.13` (or similar branches). – Robert Metzger Sep 15 '21 at 09:48
  • I don't think I understand your workflow, I'm afraid. – Benjamin W. Sep 15 '21 at 09:53
  • If you don't need it to be realtime, you can consider using the [cron](https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#scheduled-events) features, and detect branches that have been added to the repo, check via Github API if a workflow exists for their last commit, and if not, trigger one. This wouldn't be trivial. Maybe you could add a commit on top of each of the external branches with the wanted workflow? – saraf.gahl Sep 28 '21 at 18:34
  • This looks promising: https://stackoverflow.com/a/57903434/7453391 – DavidWeiss2 Sep 29 '21 at 10:22
  • Thanks a lot! But it requires a workflow definition to be present in each branch to trigger a build. – Robert Metzger Sep 29 '21 at 18:11
  • You are correct in assuming that github actions runs when a workflow is present in the branch. Basically pushing workflow files to all branches is a little cumbersome, but having the workflows only defined in the main branch and using workflow dispatch would mean that the workflow in main would need to checkout code from other branches, making it even more cumbersome. – LeadingMoominExpert Oct 08 '21 at 07:18

1 Answers1

0

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.