Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 3

Do not run cron workflows in forks

Solved! Go to Solution.

I have an action which runs on a cron schedule. However I only want it to run on the main repo, not in forks. Currently it runs in all forks too.

 

Is there a way to limit the workflow to a specific repo name, similar to how you can filter on modified files?

2 Replies
Highlighted
Solution
GitHub Partner
Message 2 of 3

Re: Do not run cron workflows in forks

@jacobtomlinson ,

In the workflow, you can use the property github.repository of github context, or the environment variable GITHUB_REPOSITORY, to view the repository where the current workflow is running on.

You can use the if conditional to skip all the jobs of the scheduled workflow according to the repository.

For example, the main repository is main_org/main_repo:

Jobs:
  build:
    if: (github.event_name == 'schedule' && github.repository == 'main_org/main_repo') || (github.event_name != 'schedule')
    . . .

This workflow will execute the job build only when meeting one of the following situations:

1) The event is schedule and the repository is main_org/main_repo, if the repository is not main_org/main_repo the job build will be skipped.

2) The event is not schedule and the repository can be anyone.

 

You just need to setup this configuration once on the main repository, and every time you create a new fork from the main repository, this configuration will be copy to the fork repository.

 

Of course, you also can try to remove the schedule event and only retain the needed events in the YAML file from the fork repository. However, if you need to create multiple fork repositories, this may become more troublesome.

Highlighted
Copilot Lvl 2
Message 3 of 3

Re: Do not run cron workflows in forks

Perfect thanks @BrightRan! The workflow raises a PR automatically to update dependencies. So raising this PR on forks was just noise for anyone who contributes to the project.

 

Adding the following resolved it for me:

 

jobs:
  job-name:
    if: github.repository == 'org_name/repo_name'

steps:
...