0

We are using github to backup configuration files automatically from our servers.

To check that the cron job is working correctly is there a way to have an action failing if there is a no commit for the last 24 hours ?

ic3
  • 7,917
  • 14
  • 67
  • 115
  • I don't think you can outrightly do that since GitHub action are event based. Maybe you could somehow compare the time – Frank nike Sep 24 '22 at 10:56
  • ... I think you have things reversed - have GitHub push to your server. It means everything is "backed up", and then you can also do nice things like require review or sign-offs, and pets you test the configs.... – Clockwork-Muse Sep 24 '22 at 11:18
  • @Franknike , you can schedule actions with cron , i.e. run your test each night + check this https://stackoverflow.com/questions/60916931/github-action-does-the-if-have-an-else + https://stackoverflow.com/questions/18093871/how-can-i-do-division-with-variables-in-a-linux-shell – ic3 Sep 24 '22 at 15:54

2 Answers2

1

You can use action with below structure

  1. Github scheduled action, Uses cron syntax
    1. Step actions/checkout step to checkout code
    2. Custom step to get last commit date time e.q.
      1. git log -1 --format=%cd //Outputs: time of last commit
      2. git log -1 --format=%cr //Output: X days ago
    3. Check this and fail the workflow, if condition met.
  • thanks, better git log -1 --format=%cd$ --date=raw | grep -o "^\w*\b" and date +%s to get the date in seconds since for doing the maths – ic3 Sep 24 '22 at 15:52
0

Eventually that the action to check we are using

name: CheckBackup

on:
  workflow_dispatch:
  schedule:
    - cron: '0 1 * * *'  # every day at 1:00am
jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - uses: actions/checkout@v3

    - name: Check dates difference
      run: |
        echo "Github last update : " +  `git log -1 --format=%cd$`
        echo "Now : " +  `date`
        let now=`date +%s`
        let backupTime=`git log -1 --format=%cd$ --date=raw | grep -o "^\w*\b"`
        deltaHours=$((now-backupTime))
        deltaHours=$((deltaHours/3600))
        if [ $deltaHours -ge 8 ]; then
             echo 'failed : ' + $deltaHours + ' hours'
             exit 1
        else     
             echo 'ok : ' + $deltaHours
             exit 0
        fi
ic3
  • 7,917
  • 14
  • 67
  • 115