name: Release Docker Image

on:
  push:
    tags:
      - '[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9][0-9]'

env:
  REGISTRY: docker.io
  IMAGE_NAME: sillyangel/mice

jobs:
  push_to_registry:
    runs-on: ubuntu-latest
    permissions:
      packages: write
      contents: read
      attestations: write
      id-token: write
    steps:
      - name: Check out the repo
        uses: actions/checkout@v4

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: sillyangel
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '22'

      - name: Get version from package.json
        id: app_version
        run: |
          VERSION=$(node -p "require('./package.json').version")
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"

      - name: Docker metadata (tags, labels)
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.IMAGE_NAME }}
          tags: |
            type=raw,value=latest,enable=${{ github.ref_name == 'main' }}
            type=raw,value=${{ steps.app_version.outputs.version }},enable=${{ startsWith(github.ref, 'refs/tags/') }}
            type=raw,value=${{ github.ref_name }},enable=${{ startsWith(github.ref, 'refs/tags/') }}
            type=sha,prefix=main-,enable=${{ github.ref_name == 'main' }}
          labels: |
            org.opencontainers.image.created=${{ github.event.head_commit.timestamp }}
            org.opencontainers.image.licenses=MIT

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

      - name: Setup Docker buildx
        uses: docker/setup-buildx-action@v3
        with:
          driver-opts: |
            network=host
            
      - name: Build and push
        id: build
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: |
            ${{ steps.meta.outputs.labels }}
            org.opencontainers.image.description=$(cat README.md | head -20 | tr '\n' ' ')
            org.opencontainers.image.documentation=https://github.com/sillyangel/stillnavidrome/blob/main/README.md
          platforms: |
            linux/amd64
            linux/arm64/v8
          cache-from: |
            type=gha,scope=deps-only
          cache-to: |
            type=gha,mode=max,scope=deps-only


      # - name: Docker Hub Description
      #   uses: peter-evans/dockerhub-description@v4
      #   with:
      #     username: ${{ vars.DOCKERHUB_USERNAME }}
      #     password: ${{ secrets.DOCKERHUB_TOKEN }}
      #     repository: sillyangel/mice
