name: release
#触发机制，当创建tag时
on:
  release:
    types:
      - published
jobs:
  frontend-builder:
    name:   frontend-builder
    runs-on: ubuntu-latest
    steps:
      - name: SetOutput
        id: vars
        run: echo "tag=${GITHUB_REF#refs/*/v}" >> $GITHUB_OUTPUT
      - name: Checkout  #Checkout代码
        uses: actions/checkout@v3
      - name: Set up Node
        uses: actions/setup-node@v3.0.0
        with:
          node-version: '18.12'
      - name: Pnpm install and build
        run: |
          npm install -g pnpm
          pnpm install --registry https://registry.npmmirror.com --dir ./frontend
          echo "Build frontend..."
          cd ./frontend && pnpm run build
      - name: upload frontend release
        uses: actions/upload-artifact@v4
        with:
          name: frontend-package
          path: frontend/dist
  release:
    needs: [frontend-builder]
    name: release
    runs-on: ubuntu-latest
    steps:
      - name: SetOutput #处理Tag字符串并存进outputs
        id: vars
        run: |
          echo "tag=${GITHUB_REF#refs/*/v}" >> $GITHUB_OUTPUT
      - name: Checkout  #Checkout代码
        uses: actions/checkout@v3
      - name: download frontend release
        uses: actions/download-artifact@v4
        with:
          name: frontend-package
          path: frontend/dist
      - name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: '1.21.1'
      - name: Go tidy
        run: |
          go mod tidy
          echo "GOVERSION=$(go version)" >> $GITHUB_ENV
      - name: Create archives on Release   #创建各种系统架构下的二进制包并上传至release assets
        uses: goreleaser/goreleaser-action@v3.1.0
        with:
          version: 1.9.2
          args: release --rm-dist
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  docker-push:
    needs: [frontend-builder]
    name: docker-push
    runs-on: ubuntu-latest
    steps:
      - name: SetOutput
        id: vars
        run: echo "tag=${GITHUB_REF#refs/*/v}" >> $GITHUB_OUTPUT
      - uses: actions/checkout@v3

      - name: download frontend release
        uses: actions/download-artifact@v4
        with:
          name: frontend-package
          path: frontend/dist
      - name: Login Docker   #登录docker
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_TOKEN }}

      - name: Setup qshell
        uses: foxundermoon/setup-qshell@v5
        with:
          version: '2.9.0'

      - name: build
        env:
          AccessKey: ${{ secrets.AK }}
          SecretKey: ${{ secrets.SK }}
          QINIU_BUCKET: ${{ secrets.QINIU_BUCKET }}
          QINIU_NAME: ${{ secrets.QINIU_NAME }}
        run: cd scripts && ./docker_publish.sh ${{ secrets.DOCKER_USERNAME }} "backend" "upload_qiniu"

