DEV Community

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

Posted on • Edited 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.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay