name: Run backup

on:
  schedule:
    - cron: '30 * * * 1-5'
  push:
    branches:
      - main
  workflow_dispatch:
    inputs:
      num:
        description: 'Number of commit to backup (0: unlimited)'
        required: true
        default: '10'

jobs:
  backup-and-push:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set num default value
        run: |
          NUM=${{ github.event.inputs.num }}
          echo "NUM=${NUM:-10}" >> $GITHUB_ENV

      - name: Run backup script
        run: |
          /bin/bash scripts/backup.sh -r https://github.com/descartes-underwriting/devops-technical-test-data.git -b 01-01-2022-test -n ${{ env.NUM }}
          /bin/bash scripts/backup.sh -r https://github.com/descartes-underwriting/devops-technical-test-data.git -b main -d $(pwd)/data_main -n ${{ env.NUM }}
        
      - name: Commit and push changes
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add data data_main
          git commit -m "Auto-update from backup job" || exit 0
          git push

