github-actions

How to schedule a GitHub Actions nightly build, but run it only when there where recent code changes?


I want to have automatic nightly builds (daily snapshots from the development branch) with GitHub Actions.

To reduce billing costs, I want the GitHub Actions workflow to trigger (or do stuff) only when there where new commits since the last GitHub Actions nightly build workflow run.

How to schedule a GitHub Actions nightly build, but run it only when there where code changes since last nightly run?

Be aware, that there are also other GitHub Actions workflows, that shall not interfere with this nightly build.


Solution

  • You can do something like this:

    1. first add this job:
      check_date:
        runs-on: ubuntu-latest
        name: Check latest commit
        outputs:
          should_run: ${{ steps.should_run.outputs.should_run }}
        steps:
          - uses: actions/checkout@v2
          - name: print latest_commit
            run: echo ${{ github.sha }}
    
          - id: should_run
            continue-on-error: true
            name: check latest commit is less than a day
            if: ${{ github.event_name == 'schedule' }}
            run: test -z $(git rev-list  --after="24 hours"  ${{ github.sha }}) && echo "::set-output name=should_run::false"
    
    1. add this to all other jobs in your workflow:
        needs: check_date
        if: ${{ needs.check_date.outputs.should_run != 'false' }}
    

    for example:

      do_something:
        needs: check_date
        if: ${{ needs.check_date.outputs.should_run != 'false' }}
        runs-on: windows-latest
        name: do something.
        steps:
          - uses: actions/checkout@v2
    

    source