How to set "schedule" event for two branches and "push"/"pull_request" events for only one?

Currently, pushing to a specific branch triggers in my workflow a push event for both branches pointed by matrix.branch, but I rather have it triggered in only one branch.

Selecting the relevant parts from my workflow:

on:
  schedule:
    - cron: '0 0 * * *'
  push:
    branches:
     - 3.7
     - 3.8
  pull_request:
    branches:
     - 3.7
     - 3.8

jobs:
  run:
    strategy:
      matrix:
        branch:
         - 3.8
         - 3.7

Hi @rffontenelle ,

This can be achieved via step if expression. code sample as below:

jobs:
  run:
    runs-on: ubuntu-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: ['3.7']
        branch:
         - 3.8
         - 3.7

    steps:
      - name: Checkout branch for push and pr event
        if: (github.event_name != 'schedule') # for push&pull request, only checkout branch 3.8
        uses: actions/checkout@v2
        with:
          ref: 3.8
      - name: checkout branch for schedule event
        if: (github.event_name == 'schedule') #for schedule event, checkout matrix branches seperately.
        uses: actions/checkout@v2
        with:
          ref: ${{ matrix.branch }}

However, according to your matrix setting, it will always create 2 jobs whatever you specified in job steps and whatever events triggered.  For push/pull_request events, in both jobs you checkout the same branch, it’s actually duplicated. 

To avoid the duplication jobs, i would recommend to move ‘push&pull_request’ event into another yaml file, checkout target branch only.

Hope it’s clear and helpful!

1 Like

Yeah, having two jobs was one thing that I would rather avoid, and I was hoping to not need duplication. Thanks!

Hi
I want cron.php to run every minute but I can’t find solutions. I have an site and I have the option of Reposted Ad and the cron file runs at one hour which I don’t want. Please help me to edit the cron file to run every minute

You might want to start your own question in the forums to get more attention, as it is completely unrelated to the question I made and my question “Solved”.