name: Build docker and publish
run-name: The pipeline for docker build
on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

env:
  # Docker
  REPO: ${{ vars.REPO }}
  DOCKER_USERNAME: ${{ vars.DOCKER_USERNAME }}
  DOCKER_PASSWORD: ${{ vars.DOCKER_PASSWORD}}
  # Gitea
  GIT_USERNAME: ${{ vars.GIT_USERNAME }}
  GIT_PASSWORD: ${{ vars.GIT_PASSWORD }}
  # Host SSH
  SSH_HOST: ${{ vars.SSH_HOST }}
  SSH_PORT: ${{ vars.SSH_PORT }}
  SSH_USER: ${{ vars.SSH_USER }}
  SSH_PASSWORD: ${{ vars.SSH_PASSWORD }}
  # SMTP
  SMTP_SERVER_ADDRESS: ${{ vars.SMTP_SERVER_ADDRESS }}
  SMTP_USERNAME: ${{ vars.SMTP_USERNAME }}
  SMTP_PASSWORD: ${{ vars.SMTP_PASSWORD }}

jobs:
  linux:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        # 使用gitea-tool-cache需要指定具体的版本号
        go: ["1.24.5"]

    steps:
      - name: Checkout
        uses: https://${{ env.GIT_USERNAME }}:${{ env.GIT_PASSWORD }}@${{ vars.DOMAIN_OF_GITEA}}/actions/checkout@v4

      # 将.env环境变量配置文件拷贝致gitea runner容器
      - name: copy env file to runner container
        uses: https://${{ env.GIT_USERNAME }}:${{ env.GIT_PASSWORD }}@${{ vars.DOMAIN_OF_GITEA}}/actions/ssh-action@v0.1.10
        with:
          host: ${{ env.SSH_HOST }}
          username: ${{ env.SSH_USER }}
          password: ${{ env.SSH_PASSWORD }}
          port: ${{ env.SSH_PORT }}
          debug: true
          script: |
            mkdir -p /install/cicd_env_files_mini_chat
            cd /install/cicd_env_files_mini_chat
            docker cp ${{env.JOB_CONTAINER_NAME}}:${{gitea.WORKSPACE}}/deploy/.env ./.env
            source ./.env
            docker cp ${{env.JOB_CONTAINER_NAME}}:${{gitea.WORKSPACE}}/docs/${SERVICE_NAME}.json .
            docker cp .env ${{ vars.RUNNER_CONTAINER_NAME }}:/.env
            docker exec ${{ vars.RUNNER_CONTAINER_NAME }} /bin/bash -c "source /.env"            

      - name: Install Go environment
        uses: https://${{ env.GIT_USERNAME }}:${{ env.GIT_PASSWORD }}@${{ vars.DOMAIN_OF_GITEA}}/actions/gitea-tool-cache@v5
        with:
          # 需要指定具体版本号!
          go-version: ${{ matrix.go }}

      - uses: https://${{ env.GIT_USERNAME }}:${{ env.GIT_PASSWORD }}@${{ vars.DOMAIN_OF_GITEA}}/actions/setup-go@v2
        with:
          go-version: ${{ matrix.go }}

      - uses: https://${{ env.GIT_USERNAME }}:${{ env.GIT_PASSWORD }}@${{ vars.DOMAIN_OF_GITEA}}/actions/cache@v3
        with:
          path: |
            ~/.cache/go-build
            ~/go/pkg/mod            
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-            

      - name: Prepare GO environment
        run: |
          go env -w  GOPROXY=https://goproxy.cn,direct
          go env -w GOPRIVATE="${{ vars.DOMAIN_OF_GITEA}}"
          git config --global url."https://${{ env.GIT_USERNAME }}:${{ env.GIT_PASSWORD }}@${{ vars.DOMAIN_OF_GITEA}}/".insteadOf "https://${{ vars.DOMAIN_OF_GITEA}}/"          

      - name: Build and push docker image
        run: |
          source ${{gitea.WORKSPACE}}/deploy/.env
          make docker
          make publish-docker
          make docker-run          

      - name: Build APIDOC docker image
        run: |
          source ${{gitea.WORKSPACE}}/deploy/.env
          echo ${SERVICE_NAME}
          echo ${APIDOC_CONTAINER_NAME}
          echo ${SWAGGER_JSON}
          make swagger-docker          