name: update-ytdl

on:
  workflow_dispatch:
    branches: main
  schedule:
    - cron: '0 0 * * *'

jobs:
  update-ytdl:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          token: ${{ secrets.AUTOUPDATE_PAT }}
      - name: Get tags
        run: git fetch --tags origin
      - name: Set up Node
        uses: actions/setup-node@v2
        with:
          node-version: '18.x'
      - name: Set up docker builder
        run: docker buildx create --name container --driver=docker-container
      - name: Update ytdlp-nodejs and release
        run: |
          docker login --username=${{ secrets.DOCKER_USERNAME }} --password=${{ secrets.DOCKER_PASSWORD }}
          npm ci --only=dev
          npm run check:lib
          if git diff --exit-code; then
            echo "NO changes detected"
          else
            npm update ytdlp-nodejs -w backend
            git config --global user.email "updater@spooty"
            git config --global user.name "AutoUpdater"
            git add .
            git commit -m "fix(ytdl): Upgrade ytdl package (automated)"
            npm run release -- --cli
          fi
        env:
          GITHUB_TOKEN: ${{ secrets.AUTOUPDATE_PAT }}

