name: Release (nightly)
on:
  schedule:
    - cron: '23 23 * * *'

permissions: {}

jobs:
  check_nightly:
    if: vars.BUILD_NIGHTLY
    permissions:
      contents: read
    runs-on: ubuntu-latest
    outputs:
      commit: ${{ steps.check_for_new_commits.outputs.commit }}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
          persist-credentials: false
      - name: Check for new commits
        id: check_for_new_commits
        run: |
          relevant_files=(
            "yt_dlp/*.py"
            ':!yt_dlp/version.py'
            "bundle/*.py"
            "bundle/docker/compose.yml"
            "bundle/docker/linux/*"
            "pyproject.toml"
            "Makefile"
            ".github/workflows/build.yml"
            ".github/workflows/release.yml"
            ".github/workflows/release-nightly.yml"
          )
          echo "commit=$(git log --format=%H -1 --since="24 hours ago" -- "${relevant_files[@]}")" | tee "$GITHUB_OUTPUT"

  release:
    needs: [check_nightly]
    if: ${{ needs.check_nightly.outputs.commit }}
    permissions:
      contents: write
      id-token: write  # mandatory for trusted publishing
    uses: ./.github/workflows/release.yml
    with:
      prerelease: true
      source: ${{ (github.repository != 'yt-dlp/yt-dlp' && vars.NIGHTLY_ARCHIVE_REPO) || 'nightly' }}
      target: 'nightly'
    secrets: inherit

  publish_pypi:
    needs: [release]
    if: vars.NIGHTLY_PYPI_PROJECT
    permissions:
      id-token: write  # mandatory for trusted publishing
    runs-on: ubuntu-latest
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v7
        with:
          path: dist
          name: build-pypi
      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          verbose: true
