name: Build-Deploy-Server

on:
  push:
    branches: [ main, dev ]
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: taskpool

jobs:
  build:
    if: github.event.repository.fork == false
    runs-on: ubuntu-latest
    strategy:
      matrix:
        base_type: [debian]
        include:
          - base_type: debian
            base_tag: base
            tag_suffix: ""

    permissions:
      contents: read
      packages: write

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

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

      - name: Login to GHCR
        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 }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}
          flavor: |
            latest=false
          tags: |
            type=raw,value=latest-amd64${{ matrix.tag_suffix }},enable={{is_default_branch}}
            type=ref,event=branch,suffix=-amd64${{ matrix.tag_suffix }}
            type=sha,prefix={{branch}}-,suffix=-amd64${{ matrix.tag_suffix }}

      - name: Get build time
        id: build_time
        run: echo "time=$(TZ='Asia/Shanghai' date '+%Y-%m-%d %H:%M:%S')" >> $GITHUB_OUTPUT

      - name: Build and push
        uses: docker/build-push-action@v7
        with:
          context: .
          file: ./docker/Dockerfile
          platforms: linux/amd64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            VERSION=${{ github.ref_name }}
            BUILD_TIME=${{ steps.build_time.outputs.time }}
            BASE_TAG=${{ matrix.base_tag }}
          cache-from: type=gha,scope=linux-amd64-${{ matrix.base_type }}
          cache-to: type=gha,scope=linux-amd64-${{ matrix.base_type }},mode=max
          provenance: false

      - name: Deploy to Demo server
        if: github.ref == 'refs/heads/main' && matrix.base_type == 'debian'
        uses: appleboy/ssh-action@v1.2.0
        with:
          host: ${{ secrets.DEPLOY_HOST }}
          username: ${{ secrets.DEPLOY_USER }}
          password: ${{ secrets.DEPLOY_PASSWORD }}
          port: ${{ secrets.DEPLOY_PORT }}
          script: |
            ${{ secrets.DEPLOY_SCRIPT }}


      # - name: Deploy to X server
      #   if: github.ref == 'refs/heads/main' && matrix.base_type == 'debian'
      #   uses: appleboy/ssh-action@v1.2.0
      #   with:
      #     host: ${{ secrets.DEPLOY_HOST }}
      #     username: ${{ secrets.DEPLOY_USER }}
      #     password: ${{ secrets.DEPLOY_PASSWORD }}
      #     port: ${{ secrets.DEPLOY_PORT }}
      #     script: |
      #       cd ${{ secrets.DEPLOY_X_PATH }}
      #       docker-compose -f docker-compose.yml pull
      #       docker-compose -f docker-compose.yml up -d