name: Docker Build and Push - News Service

on:
  push:
    branches:
      - main
    tags:
      - 'v*'

jobs:
  build-and-push-news-service:
    name: Build and Push News Service Image
    runs-on: internal_docker

    steps:
      - name: Checkout code
        uses: https://github.com/actions/checkout@v4

      - name: Set up environment variables
        run: |
          # 获取短 commit SHA (前10位)
          SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-10)
          # 获取分支名
          BRANCH_NAME=${GITHUB_REF#refs/heads/}
          
          # 设置镜像标签
          echo "SHORT_SHA=$SHORT_SHA" >> $GITHUB_ENV
          echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
          echo "IMAGE_NAME=wjp190420/news-service" >> $GITHUB_ENV

          echo "Build info:"
          echo "  Short SHA: $SHORT_SHA"
          echo "  Branch: $BRANCH_NAME"
          echo "  Image: wjp190420/news-service"

      - name: Login to Docker Hub
        run: |
          echo "dckr_pat_tQJLPPV5NMrYVR9-M81RajldwEM" | docker login docker.io -u wjp190420 --password-stdin

      - name: Build News Service Docker image
        run: |
          docker build \
            -f Dockerfile \
            -t ${{ env.IMAGE_NAME }}:${{ env.SHORT_SHA }} \
            -t ${{ env.IMAGE_NAME }}:${{ env.BRANCH_NAME }}-${{ env.SHORT_SHA }} \
            -t ${{ env.IMAGE_NAME }}:latest \
            .

      - name: Push News Service Docker image
        run: |
          echo "Pushing News Service images with tags:"
          echo "  - ${{ env.IMAGE_NAME }}:${{ env.SHORT_SHA }}"
          echo "  - ${{ env.IMAGE_NAME }}:${{ env.BRANCH_NAME }}-${{ env.SHORT_SHA }}"
          echo "  - ${{ env.IMAGE_NAME }}:latest"

          docker push ${{ env.IMAGE_NAME }}:${{ env.SHORT_SHA }}
          docker push ${{ env.IMAGE_NAME }}:${{ env.BRANCH_NAME }}-${{ env.SHORT_SHA }}
          docker push ${{ env.IMAGE_NAME }}:latest

          echo "✅ News Service image pushed successfully!"

  summary:
    name: Build Summary
    runs-on: internal_docker
    needs: [build-and-push-news-service]
    if: always()

    steps:
      - name: Print summary
        run: |
          echo "=========================================="
          echo "Build Summary"
          echo "=========================================="
          echo "News Service build: ${{ needs.build-and-push-news-service.result }}"
          echo "=========================================="

          if [ "${{ needs.build-and-push-news-service.result }}" == "success" ]; then
            echo "News Service image built and pushed successfully!"
            echo ""
            echo "Pull image with:"
            echo "  docker pull wjp190420/news-service:latest"
            echo ""
            echo "Run container with:"
            echo "  docker run -d -p 3100:3100 -v ./data:/app/data wjp190420/news-service:latest"
            echo ""
            echo "Or use docker-compose:"
            echo "  cd /opt/news-service"
            echo "  docker-compose pull"
            echo "  docker-compose up -d"
            exit 0
          else
            echo "❌  Build failed!"
            exit 1
          fi
