name: Build and Publish n8n Docker Image

on:
  push:
    branches:
      - main
    tags:
      - 'v*'
    paths-ignore:
      - '**.md'
      - '**.txt'
      - 'docs/**'
      - 'examples/**'
      - '.github/FUNDING.yml'
      - '.github/ISSUE_TEMPLATE/**'
      - '.github/pull_request_template.md'
      - '.gitignore'
      - 'LICENSE*'
      - 'ATTRIBUTION.md'
      - 'SECURITY.md'
      - 'CODE_OF_CONDUCT.md'
  pull_request:
    branches:
      - main
    paths-ignore:
      - '**.md'
      - '**.txt'
      - 'docs/**'
      - 'examples/**'
      - '.github/FUNDING.yml'
      - '.github/ISSUE_TEMPLATE/**'
      - '.github/pull_request_template.md'
      - '.gitignore'
      - 'LICENSE*'
      - 'ATTRIBUTION.md'
      - 'SECURITY.md'
      - 'CODE_OF_CONDUCT.md'
  workflow_dispatch:

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}/n8n-mcp

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

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

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

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

      - name: Log in to GitHub Container Registry
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v4
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract metadata
        id: meta
        uses: docker/metadata-action@v6
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          tags: |
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=raw,value=latest,enable={{is_default_branch}}

      - name: Build and push Docker image
        uses: docker/build-push-action@v7
        with:
          context: .
          file: ./Dockerfile
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
          platforms: linux/amd64,linux/arm64

  test-image:
    needs: build-and-push
    runs-on: ubuntu-latest
    if: github.event_name != 'pull_request'
    permissions:
      contents: read
      packages: read

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v4
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Test Docker image
        run: |
          # Test that the image starts correctly with N8N_MODE
          docker run --rm \
            -e N8N_MODE=true \
            -e MCP_MODE=http \
            -e N8N_API_URL=http://localhost:5678 \
            -e N8N_API_KEY=test \
            -e MCP_AUTH_TOKEN=test-token-minimum-32-chars-long \
            -e AUTH_TOKEN=test-token-minimum-32-chars-long \
            ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest \
            node -e "console.log('N8N_MODE:', process.env.N8N_MODE); process.exit(0);"

      - name: Test health endpoint
        run: |
          # Start container in background
          docker run -d \
            --name n8n-mcp-test \
            -p 3000:3000 \
            -e N8N_MODE=true \
            -e MCP_MODE=http \
            -e N8N_API_URL=http://localhost:5678 \
            -e N8N_API_KEY=test \
            -e MCP_AUTH_TOKEN=test-token-minimum-32-chars-long \
            -e AUTH_TOKEN=test-token-minimum-32-chars-long \
            ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
          
          # Wait for container to start
          sleep 10
          
          # Test health endpoint
          curl -f http://localhost:3000/health || exit 1
          
          # Test MCP endpoint (requires auth since v2.47.6)
          curl -f -H "Authorization: Bearer test-token-minimum-32-chars-long" http://localhost:3000/mcp || exit 1
          
          # Cleanup
          docker stop n8n-mcp-test
          docker rm n8n-mcp-test

  create-release:
    needs: [build-and-push, test-image]
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    permissions:
      contents: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          body: |
            ## Docker Image

            The n8n-specific Docker image is available at:
            ```
            docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }}
            ```

            ## Quick Deploy

            Use the quick deploy script for easy setup:
            ```bash
            ./deploy/quick-deploy-n8n.sh setup
            ```

            See the [deployment documentation](https://github.com/${{ github.repository }}/blob/main/docs/deployment-n8n.md) for detailed instructions.