'Github Actions scheduled nightly job only if there have a been a change (Windows)

I have a nightly job running and I would like it to run only if there has been a commit during the past 24 hours

I have seen this question but it is a linux command. I am looking to achieve the same thing on a Windows self-hosted environment so that command does not work.

I tried the solution there but there is not equivalent to the test command and --after doesn't exist

  check_date:
    runs-on: self-hosted
    name: Check latest commit
    outputs:
      should_run: ${{ steps.should_run.outputs.should_run }}
    steps:
      - uses: actions/[email protected]
      - name: Print Latest Commit
        run: echo ${{ github.sha }}

      - id: should_run
        continue-on-error: true
        name: Check if 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"

but I get this error message

Run test -z $(git rev-list  --after="24 hours"  db8f6566733fd7240baaa51607783d54305efa7d)
test : The term 'test' is not recognized as the name of a cmdlet, function, script file, or operable program. Check 
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At D:\workspace\_temp\4d4d15eb-e2be-422e-a826-44984f2860dc.ps1:2 char:1
+ test -z $(git rev-list  --after="24 hours"  db8f6566733fd7240baaa5160 ...
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (test:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : CommandNotFoundException
 
Error: Process completed with exit code 1.


Solution 1:[1]

One issue that I had was that my git version was too old (2.33).
--after was introduced from 2.35

The other one, I "hacked" it by using a script. Not a very elegant solution but it works..
I did put this into a batch file and call it.

@echo off
setlocal enabledelayedexpansion

set result=
for /f "tokens=*" %%i in ('git rev-list %~1 --after="24 hours"') do (
  set result=%%i
)

if "!result!"=="" ( 
  echo ::set-output name=should_run::false
) else (
  echo ::set-output name=should_run::true
)

And use it like this:

  check_date:
    runs-on: self-hosted
    name: Check latest commit
    outputs:
      should_run: ${{ steps.should_run.outputs.should_run }}
    steps:
      - uses: actions/[email protected]
      - name: Print Latest Commit
        run: echo ${{ github.sha }}

      - id: should_run
        continue-on-error: true
        name: Check if latest commit is less than a day
        shell: cmd
        run: ci_check_last_commit.bat ${{ github.sha }}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1