DEV Community

Cover image for TIL: how to disable cron for GitHub forks
Hugo van Kemenade
Hugo van Kemenade

Posted on • Updated on

TIL: how to disable cron for GitHub forks

GitHub Actions has a useful feature to trigger workflows on a cron schedule. For example:

name: Test

on: 
  push:
  pull_request:
  schedule:
    - cron: "0 6 * * *" # daily at 6am

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
... 
Enter fullscreen mode Exit fullscreen mode

But if a contributor has enabled GitHub Actions on their fork (which I recommend: test your contributions before opening a PR), it also runs the cron on their fork. This not only uses up extra CI resources on the fork:

A list of daily GitHub Actions runs

It also sends a regular email to the contributor when the workflow fails:

A daily email from GitHub:

Instead, we only need to run the cron for the upstream. For example, for a https://github.com/octocat/hello-world repo, add:

 jobs:
   test:
+    if: ${{ github.repository_owner == 'octocat' || github.event_name != 'schedule' }}
     runs-on: ubuntu-latest
Enter fullscreen mode Exit fullscreen mode

This only runs the schedule trigger for the octocat upstream, and all other triggers run for both upstream and forks.

Also

For simpler workflows that only trigger on a cron, such as stalebot or CodeQL runs, we can add a simpler condition.

For example:

name: Close stale issues

on:
  schedule:
  - cron: "10 0 * * *" # daily at 12:10am

jobs:
  stale:
    runs-on: ubuntu-latest

    steps:
    - name: "Check issues"
      uses: actions/stale@v8
...
Enter fullscreen mode Exit fullscreen mode

We can completely disable it for forks:

 jobs:
   stale:
+    if: github.repository_owner == 'octocat'
     runs-on: ubuntu-latest
Enter fullscreen mode Exit fullscreen mode

PS

Use single quotes in these lines, double quotes are invalid here (Unexpected symbol).

Thanks

To Alex Waygood for the tip.

To the British Library and Flickr Commons for the illustration of a chronograph.

Top comments (0)