name: Build

# 打标签时触发构建，另外标签需v开头，例如v1.0.0，需要配置DOCKER_PASSWORD的secrets
# 构建后镜像为 ${docker_registry}/${docker_username}/${repo_name}:1.0.0
on:
  push:
    tags:
      - v*

env:
  DOCKER_REGISTRY: registry.cn-hangzhou.aliyuncs.com
  DOCKER_USERNAME: rsjst

jobs:
  build-image:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 1

      - name: Setup Pnpm and Install
        uses: seepine/action-setup-pnpm@v1

      - name: Project Build
        run: pnpm run build

      - name: Get Meta
        id: meta
        run: |
          echo REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F"/" '{print $2}') >> $GITHUB_OUTPUT
          echo REPO_VERSION=$(echo ${{ github.ref }} | awk -F"/" '{print $3}' | awk -F"v" '{print $2}') >> $GITHUB_OUTPUT

      - name: Docker build push
        uses: seepine/action-docker-build-push@v1
        with:
          registry: ${{ env.DOCKER_REGISTRY }}
          username: ${{ env.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
          context: .
          file: ./docker/Dockerfile
          platforms: linux/amd64,linux/arm64
          tags: |
            ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_USERNAME }}/${{ steps.meta.outputs.REPO_NAME }}:latest
            ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_USERNAME }}/${{ steps.meta.outputs.REPO_NAME }}:${{ steps.meta.outputs.REPO_VERSION }}

      - name: WeChat Work notification
        uses: seepine/action-wechat-work@master
        env:
          WECHAT_WORK_BOT_WEBHOOK: ${{ secrets.WECHAT_WORK_BOT_WEBHOOK }}
        if: ${{ env.WECHAT_WORK_BOT_WEBHOOK != '' }}
        with:
          msgtype: markdown
          content: "${{ steps.meta.outputs.REPO_NAME }} build docker image success.\n
            > Tag: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_USERNAME }}/${{ steps.meta.outputs.REPO_NAME }}:${{ steps.meta.outputs.REPO_VERSION }}"
