name: Docker Sz Services Test CI

on:
  push:
    branches: [ "test" ]
  pull_request:
    branches: [ "test" ]
  workflow_dispatch:

jobs:
  build-and-deploy:
    name: Build & Deploy ${{ matrix.app_name }}
    runs-on: ubuntu-latest
    strategy:
      max-parallel: 1 # 串行执行
      matrix:
        include:
          - app_name: sz-service-admin
            service_port: 9991
            log_dir: /home/app/sz-service-admin/logs
            config_dir: /home/conf/sz-service-admin
            jar_dir: ./sz-service/sz-service-admin/target
          - app_name: sz-service-websocket
            service_port: 9993
            log_dir: /home/app/sz-service-websocket/logs
            config_dir: /home/conf/sz-service-websocket
            jar_dir: ./sz-service/sz-service-websocket/target
    env:
      ACR_DOMAIN: registry.cn-beijing.aliyuncs.com
      ACR_ZONE: sz-action
      VERSION: test
      RUNNING_ACTIVE: preview
      SHELL_RUN_DIR: /home/run
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up JDK 21
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: '21'
          cache: 'maven'

      - name: Set up Maven
        uses: stCarolas/setup-maven@v5
        with:
          maven-version: 3.8.2

      - name: Build with Maven
        run: mvn clean package

      - name: Copy JAR file
        run: |
          cd ${{ matrix.jar_dir }}
          cp ./*.jar ../../../app.jar

      - name: Build Docker image
        run: docker build -t ${{ matrix.app_name }}:${{ env.VERSION }} .

      - name: Log in to Docker ACR
        run: echo "${{ secrets.ACR_PASSWORD }}" | docker login --username=${{ secrets.ACR_USERNAME }} ${{ env.ACR_DOMAIN }} --password-stdin

      - name: Tag Docker image
        run: docker tag ${{ matrix.app_name }}:${{ env.VERSION }} ${{ env.ACR_DOMAIN }}/${{ env.ACR_ZONE }}/${{ matrix.app_name }}:${{ env.VERSION }}

      - name: Push Docker image
        run: docker push ${{ env.ACR_DOMAIN }}/${{ env.ACR_ZONE }}/${{ matrix.app_name }}:${{ env.VERSION }}

      - name: Deploy to remote server
        uses: appleboy/ssh-action@v1.2.0
        with:
          host: ${{ secrets.REMOTE_HOST_TEST }}
          username: ${{ secrets.REMOTE_USER_TEST }}
          password: ${{ secrets.REMOTE_PASSWORD_TEST }}
          script: |
            docker pull ${{ env.ACR_DOMAIN }}/${{ env.ACR_ZONE }}/${{ matrix.app_name }}:${{ env.VERSION }}

            echo "==================== 生成启动命令脚本 ===================="
            mkdir -p ${{ env.SHELL_RUN_DIR }}
            START_SCRIPT="${{ env.SHELL_RUN_DIR }}/docker_run_${{ matrix.app_name }}_${{ env.RUNNING_ACTIVE }}.sh"
            cat > $START_SCRIPT <<EOL
            #!/bin/bash
              echo "==================== 停止旧应用容器 ===================="
              docker stop ${{ matrix.app_name }} || true
              docker rm ${{ matrix.app_name }} || true
              docker image prune -f
              docker builder prune -f
              echo "==================== 启动应用容器 ===================="
              docker run -itd \\
              --name ${{ matrix.app_name }} \\
              --restart always \\
              -p ${{ matrix.service_port }}:${{ matrix.service_port }} \\
              -v ${{ matrix.log_dir }}:/logs \\
              -v ${{ matrix.config_dir }}:/config \\
              -e "SPRING_PROFILES_ACTIVE=${{ env.RUNNING_ACTIVE }}" \\
              ${{ env.ACR_DOMAIN }}/${{ env.ACR_ZONE }}/${{ matrix.app_name }}:${{ env.VERSION }}
            EOL
            chmod +x $START_SCRIPT
            echo "启动脚本已生成：$START_SCRIPT"
            echo "可以运行以下命令手动启动容器："
            echo "bash $START_SCRIPT"
            bash $START_SCRIPT