name: Docker Deployment

on:
  workflow_run:
    workflows: ["Node.js CI Test"]
    types:
      - completed

permissions:
  contents: read
  packages: write

jobs:
  docker:
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v6

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

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      
      - name: Force insecure registry config for buildx
        run: |
          mkdir -p ~/.docker
          echo '{
            "insecure-registries": ["52.156.106.39.nip.io"]
          }' > ~/.docker/config.json
    
      - name: Debug secrets
        run: |
          echo "USERNAME=$GIT_USERNAME"
          echo "TOKEN_LENGTH=${#GIT_TOKEN}"
        env:
          GIT_USERNAME: ${{ secrets.GIT_USERNAME }}
          GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
      
      - name: Check Docker config
        run: cat /etc/docker/daemon.json || true

      - name: Login to Gitea Registry
        run: |
          echo "${{ secrets.GIT_TOKEN }}" | docker login 52.156.106.39.nip.io \
            -u "${{ secrets.GIT_USERNAME }}" \
            --password-stdin

      - name: Build and Push Docker Image
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          insecure: true
          tags: |
            52.156.106.39.nip.io/${{ secrets.GIT_USERNAME }}/node-js-test:latest