name: 部署测试环境

on:
  push:
    branches:
      - staging

env:
  IMAGE_NAME: ${{ gitea.repository_owner }}/usercenter.web
  PUBLISH_PATH: usercenter.web
  PUBLISH_HOST: 10.255.255.3
  PUBLISH_PORT: 22
  PUBLISH_REPLICAS: 1 #启动副本数量

jobs:
  job1:
    name: 编译发布
    runs-on: linux
    container:
      image: node:16-bullseye
      volumes:
        #挂载缓存
        - /opt/.runner/node_modules:/cache

    steps:
      - name: 下载源码
        uses: actions/checkout@v3

      - name: 使用缓存
        uses: actions/cache@master
        with:
          mount: |
            node_modules

      - name: 编译项目
        shell: bash
        run: |
          echo =========构建项目开始========
          rm -rf package-lock.json #删除文件
          npm config set registry https://registry.npmmirror.com/
          npm install
          npm run build
          echo =========构建项目结束========

      - name: 打包上传镜像
        uses: actions/docker@master
        with:
          registry: ${{ secrets.DOCKER_REGISTRY }}
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
          image: ${{ env.IMAGE_NAME }}
          context: .
          tags: latest,${{ gitea.ref_name }}

      - name: 生成环境变量文件
        env:
          env_file: |
            REGISTRY=${{ secrets.DOCKER_REGISTRY }}
            OWNER=${{ gitea.repository_owner }}
            TAG=${{ gitea.ref_name }}
            REPLICAS=${{ env.PUBLISH_REPLICAS }}
        run: |
          echo "$env_file">.env

      - name: 复制发布脚本
        uses: docker://appleboy/drone-scp
        with:
          host: ${{ env.PUBLISH_HOST }}
          port: ${{ env.PUBLISH_PORT }}
          username: ${{ secrets.PUBLISH_USER_NAME }}
          password: ${{ secrets.PUBLISH_PASSWORD }}
          target: ${{ env.PUBLISH_PATH }}
          source: docker-swarm.yaml,.env

      - name: 部署到服务器
        uses: docker://appleboy/drone-ssh
        with:
          host: ${{ env.PUBLISH_HOST }}
          port: ${{ env.PUBLISH_PORT }}
          username: ${{ secrets.PUBLISH_USER_NAME }}
          password: ${{ secrets.PUBLISH_PASSWORD }}
          script: |

            cd ${{ env.PUBLISH_PATH }}

            [ -f .env ] && export $(sed '/^#/d' .env)

            docker stack deploy -c docker-swarm.yaml --with-registry-auth mk

      - name: 发送构建通知
        if: ${{ always() }}
        uses: actions/webhook@master
        with:
          urls: https://oapi.dingtalk.com/robot/send?access_token=6ddafcada8f44f4bad4a7314c4d9bd19a895ded0a1ba1afdaff5dd01a5af6781
          content_type: application/json
          template: |
            {
                "msgtype": "markdown",
                "markdown": {
                    "title":"项目部署通知",
                    "text": "${{ job.status == 'success' && '✅' || '❌' }}**${{ gitea.repository }}**\n
                        >**构建结果**: ${{ job.status }}
                        >**构建详情**: [点击查看](${{ gitea.server_url }}/${{ gitea.repository }}/actions/runs/${{ gitea.run_number }})
                        >**代码分支**: ${{ gitea.ref_name }}
                        >**提交标识**: ${{ gitea.sha }}
                        >**提交发起**: ${{ gitea.actor }}
                        >**提交信息**: ${{ gitea.event.head_commit.message }}\n
                      "
                }
            }
