How to trigger workflow run once a week if push has occurred?

I would like to trigger my github actions workflow to run once a week,only if there have been any pushes made to the repo in that week. Is there any way  to do this?

Thanks.

@benstear ,

If you want your workflow run weekly, you need to set Scheduled events for the workflow.
If you want the workflow runs only when have pushes in that week, you need to compare whether the commit SHA ( github.sha ) of current workflow run is different with that of the last workflow run.
You can use cache action to store and pass the commit SHA in each workflow run.

A simple demo:

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

jobs:
  test_schedule:
    name: Test schedule
    runs-on: ubuntu-latest
    steps:
      - name: Create cache file
        run: |
          mkdir check-SHA
          echo ${{ github.sha }} > github-sha.txt

      - name: Check SHA
        id: check_sha
        uses: actions/cache@v1
        with:
          path: check-SHA
          key: check-SHA-${{ github.sha }}

      - name: Some subsequent steps
        if: steps.check_sha.outputs.cache-hit != 'true'
        run: echo "Exccute the subsequent steps"

You can see I set the key of the cache contains the  commit SHA  of current workflow run, when execute the cache action , it will check whether the specified key  matchs an existing cache. If the key  matchs an existing cache ( cache-hit is true ), it will restore this cache to the specified path on the local, and overwrite the file created in the " Create cache file" step in the path. If the key doesn’t match any existing cache ( cache-hit is false ), the action will save the file in the " Create cache file" step in the specified path  as a cache with the specified key.

In each subsequent step, use the if conditional to skip the subsequent step when the  cache-hit is true.

1 Like