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

Trigger workflow if there is commit in last 24 hours

We have scheduled workflow runs evey day, but we want it to run if we have commit in last 24 hours, is it possible? how?


2 Replies
Pilot Lvl 1
Message 2 of 3

Re: Trigger workflow if there is commit in last 24 hours

Make an API call with desired parameters (since) to and check if anything is returned.
Pilot Lvl 2
Message 3 of 3

Re: Trigger workflow if there is commit in last 24 hours

I'm running conditional schedule in one of our repos, which sounds very similar to what you want to do.

Key differences:

- schedule disables itself after 7 days without commits, instead of 24 hours

- schedule creates commits on its own using bot account - they are ignored when checking last activity

- for testing/forcing autocommits, `repository_dispatch` triggers are added, which ignores 7 days limit


As dawidd6 mentioned, checking API is a way to go. You could - obviously - clone repo and use `git log` to extract commit(s) info, but with `actions/checkout` it might take eternity (~30 seconds); wasted money on private repos which rarely changes.


Last commit info, with user filtering

curl -sL$GITHUB_REPOSITORY/commits | jq -r '[.[] | select(.author.login != "bot-account")][0]' > $HOME/commit.json


Last commit info, without user filtering

curl -sL$GITHUB_REPOSITORY/commits | jq -r '[.[]][0]' > $HOME/commit.json


From there it's a matter of messing with `jq` and `date` to extract timestamp of last commit and check how much time passed since then...

date="$(jq -r '' $HOME/commit.json)"
timestamp=$(date --utc -d "$date" +%s)
days=$(( ( $(date --utc +%s) - $timestamp ) / 86400 ))


...and check if result is within limit (that's where i allow `repository_dispatch` to ignore it). Once it's done, i'm setting a single variable in `env` context which tells me if repo is active...

if [ $alive -eq 1 ]; then
   echo ::set-env name=GHA_REPO_ALIVE::true


...and use that as condition in each and every other step after that

- name: Do Things
  if:   env.GHA_REPO_ALIVE == 'true'
  run:  ...

- name: Do Other Things
if: env.GHA_REPO_ALIVE == 'true'
uses: ...


I think it's simple enough to tweak or port to other shells. Maybe even converted to its own action if someone wants conditional schedules in multiple workflows/jobs. Anyway, here's my complete setup if anyone is bored enough (""little"" messy, but hey, it works! far).