8

I've made an action. I'd like for this to run on branch foo at a schedule(in my case, twice a day).

Here's what I've got so far:

repo/.github/workflows/daily-foo-tests.yml

name: Run integration tests on foo branch twice a day
on:
  schedule:
    # Execute at 00:01 and 13:01 UTC daily
    - cron:  '00 01,13 * * *'

jobs:
  build:
    name: Run UI Automation
    runs-on: [self-hosted, macOS, X64]
    steps:
      - uses: actions/checkout@v2
      - name: dotnet build
        with: { ref: foo }
        // continues to do stuff, not important

Now, this action has been pushed to said foo branch. However, going to github.com/org/repo/actions, it does not trigger(I've waited 24 hours now; it should've done something at this point).

What is the correct way to trigger a scheduled github action workflow on a specified branch?

cbll
  • 6,499
  • 26
  • 74
  • 117
  • 1
    See [this answer](https://stackoverflow.com/a/58800550/11934042). Your workflow must be committed to the default branch to trigger `on: schedule`. – peterevans Aug 27 '20 at 08:40
  • So, say `foo` is a the `staging` branch, and `master` is the default branch of the repository - the workflow yaml must exist on the `master` branch before anything is triggered? I don't want it to run on the latest commit SHA on the default branch; I want the latest commit sha on the `foo` branch. – cbll Aug 27 '20 at 08:41
  • Yes, exactly. `on: schedule` workflows do not work in non-default branches. But you can still checkout other branches when it runs. – peterevans Aug 27 '20 at 08:44
  • I'm not sure if I follow, so I'll ask directly: Can I run a scheduled workflow on a non-default branch in any way? Is that the last part of the answer you sent me? – cbll Aug 27 '20 at 08:45

1 Answers1

10

The workflow must be committed to the default branch to be triggered. It won't work if you only commit it to a non-default branch.

Scheduled workflows run on the latest commit on the default or base branch.

ref: https://docs.github.com/en/actions/reference/events-that-trigger-workflows#schedule

For example, this should be committed to the default branch. When it runs, it checks out branch foo and then you can build, test, etc.

on:
  schedule:
    - cron:  '0 1 * * 4'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: foo
      
      # Build steps go here
peterevans
  • 34,297
  • 7
  • 84
  • 83
  • Thanks. I'll try this solution. I'll accept it... within 12 hours, if green ;-) – cbll Aug 27 '20 at 08:55
  • If there any way to run this in two branches, e.g. default and foo? Of course I can write yml so that there are separate steps for each branch or implement separate yml file for each branch, but no simpler way to do it? – diidu Sep 07 '21 at 08:43
  • @diidu The only way I know to simplify that scenario is to use a [job matrix](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix) to avoid having duplicate steps for each branch. – peterevans Sep 07 '21 at 08:58