name: Build Docker(dev)

on:
  push:
    tags:
      - '\d+\.\d+\.\d+-beta\d+'
      - '\d+\.\d+-beta\d+'
      - '\d+\.\d+-alpha\d+'

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python 3.11
        uses: actions/setup-python@v3
        with:
          python-version: "3.11"
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
          pip install pytest
      - name: Test
        working-directory: ./src
        run: |
          mkdir -p config
          pytest

  dev-latest:
    runs-on: ubuntu-latest
    needs: [test]
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Create Version info
        working-directory: ./src
        run: |
          echo "VERSION = '$GITHUB_REF_NAME'" > module/__version__.py

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Docker metadata
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            estrellaxd/auto_bangumi
            ghcr.io/${{ github.repository }}
          tags: |
            type=raw,value=${{ github.ref_name }}
            type=raw,value=dev-latest

      - name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

      - name: Login to ghcr.io
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.ACCESS_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v3
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          file: Dockerfile

  generate_release:
    runs-on: ubuntu-latest
    needs: [test]
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Generate Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ github.ref_name }}
          name: 🌙${{ github.ref_name }}
          draft: true
          prerelease: true
        env:
          GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}

