name: deploy
on:
  push:
    branches: [ master,dev ]
  workflow_dispatch:

# 设置上海时区
env:
  TZ: Asia/Shanghai

jobs:
  sync:
    runs-on: ubuntu-latest
    steps:
      - name: Mirror the Github organization repos to Gitee.
        uses: Yikun/hub-mirror-action@master
        with:
          src: github/h7ml
          dst: gitee/h7ml
          dst_key: ${{ secrets.GITEE_PRIVATE_KEY }}
          dst_token: ${{ secrets.GITEE_TOKEN }}
          white_list: "amis-admin-vue" # 只同步amis-admin-vue
          force_update: true
          account_type: user
          clone_style: ssh
          debug: true
  pages:
    # 任务运行的容器类型（虚拟机环境）
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [ latest ]
        pnpm-version: [ latest ]
    # 任务执行的步骤
    steps:
      # 步骤名称
      - name: Checkout 🚀
        uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }} 🚀
        uses: actions/setup-node@v2
      - name: Cache pnpm modules  🚀
        uses: actions/cache@v2
        env:
          cache-name: cache-pnpm-modules
        with:
          path: ~/.pnpm-store
          key: ${{ runner.os }}-${{ matrix.pnpm-version }}-build-${{ env.cache-name }}-${{ matrix.node-version }}-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.pnpm-version }}-build-${{ env.cache-name }}-${{ matrix.node-version }}-
      - name: Use Pnpm ${{ matrix.pnpm-version }} 🚀
        uses: pnpm/action-setup@v2.2.1
        with:
          version: ${{ matrix.pnpm-version }}
          run_install: true
      - name: Pnpm script 🚀
        run: |
          rm -rf vue.config.js
          wget https://raw.staticdn.net/h7ml/amis-admin-vue/17bc54a0ec1303e47fb023cc9faaac6d4b3cec27/vue.config.js
          pnpm build
        # 构建发布 Github pages
      - name: Deploy 🚀
        # 使用github-pages-deploy-action: https://github.com/JamesIves/github-pages-deploy-action/tree/master
        uses: JamesIves/github-pages-deploy-action@v4.3.3
        with:
          # 存储库中要部署的文件夹。
          # 该步骤会将项目中 FOLDER 指定文件夹下的文件推送到 BRANCH 分支，作为Github Pages 部署的内容。
          BRANCH: gh-pages # The branch the action should deploy to.
          FOLDER: dist # The folder the action should deploy.
          GITHUB_TOKEN: ${{ secrets.ACTION_SECRET }}
    needs: sync
  build:
    name: build 🚀
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [ latest,current,stable ]
        pnpm-version: [ latest ]
    steps:
      - name: Checkout 🚀
        uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }} 🚀
        uses: actions/setup-node@v2
      - name: Cache pnpm modules  🚀
        uses: actions/cache@v2
        env:
          cache-name: cache-pnpm-modules
        with:
          path: ~/.pnpm-store
          key: ${{ runner.os }}-${{ matrix.pnpm-version }}-build-${{ env.cache-name }}-${{ matrix.node-version }}-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.pnpm-version }}-build-${{ env.cache-name }}-${{ matrix.node-version }}-
      - name: Use Pnpm ${{ matrix.pnpm-version }} 🚀
        uses: pnpm/action-setup@v2.2.1
        with:
          version: ${{ matrix.pnpm-version }}
          run_install: true
      - name: Pnpm script 🚀
        run: |
          pnpm build
    needs: pages
  deploy:
    name: deploy 🚀
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [ latest ]
        pnpm-version: [ latest ]
    steps:
      - name: Checkout 🚀
        uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }} 🚀
        uses: actions/setup-node@v2
      - name: Cache pnpm modules  🚀
        uses: actions/cache@v2
        env:
          cache-name: cache-pnpm-modules
        with:
          path: ~/.pnpm-store
          key: ${{ runner.os }}-${{ matrix.pnpm-version }}-build-${{ env.cache-name }}-${{ matrix.node-version }}-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.pnpm-version }}-build-${{ env.cache-name }}-${{ matrix.node-version }}-
      - name: Use Pnpm ${{ matrix.pnpm-version }} 🚀
        uses: pnpm/action-setup@v2.2.1
        with:
          version: ${{ matrix.pnpm-version }}
          run_install: true
      - name: Pnpm script 🚀
        run: pnpm build
#      - name: Deploy to github gh-pages 🚀
#        uses: JamesIves/github-pages-deploy-action@master
#        with:
##          repository-name: h7ml/amis-admin-vue
#          branch: gh-pages
##          commit-message: "deploy: :rocket: ${{ github.repository }}@${{ github.sha }}"
#          folder: dist
#          token: ${{ secrets.ACCESS_TOKEN }}
      - name: Deploy to Netlify  🚀
        uses: nwtgck/actions-netlify@v1.2
        with:
          publish-dir: './dist'
          production-branch: master
          github-token: ${{ secrets.GIT_TOKEN }}
          deploy-message: "Deploy from GitHub Actions"
          enable-pull-request-comment: false
          enable-commit-comment: true
          netlify-config-path: './dist/netlify.toml'
          overwrites-pull-request-comment: false
        env:
          NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
          NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
      - name: Deploy to surge 🚀
        uses: dswistowski/surge-sh-action@v1
        with:
          domain: 'amis-admin-vue.surge.sh'
          project: './dist'
          login: ${{ secrets.surge_login }}
          token: ${{ secrets.surge_token }}
      - name: Upload surge service and generate preview URL 🚀
        id: deploy
        run: |
          export GITHUB_SHA=${{ github.sha }}
          export GITHUB_SHA_SUB=${GITHUB_SHA: 0: 7 }
          export DEPLOY_DOMAIN=https://amis-admin-vue-$GITHUB_SHA_SUB.surge.sh
          npx surge --project ./dist --domain $DEPLOY_DOMAIN --token ${{ secrets.surge_token }}
          echo the preview URL is $DEPLOY_DOMAIN
          echo "::set-output name=url::$DEPLOY_DOMAIN"
      #      - name: Deploy to vercel 🚀
      #        uses: amondnet/vercel-action@v20
      #        with:
      #          vercel-token: ${{ secrets.VERCEL_TOKEN }}
      #          github-token: ${{ secrets.GIT_TOKEN }}
      #          vercel-args: '--prod' #Optional
      #          vercel-org-id: ${{ secrets.ORG_ID}}
      #          vercel-project-id: ${{ secrets.PROJECT_ID}}
      #          working-directory: ./dev
      #      # 使用云开发 Github Action 部署
      #      - name: Deploy CloudBase 🚀
      #        uses: TencentCloudBase/cloudbase-action@v1.1.0
      #        with:
      #          # 云开发的访问密钥 secretId 和 secretKey
      #          secretId: ${{ secrets.SECRET_ID }}
      #          secretKey: ${{ secrets.SECRET_KEY }}
      #          # 云开发的环境id
      #          envId: ${{ secrets.ENV_ID }}
      #          # Github 项目静态文件的路径
      #          staticSrcPath: ./dev
      - name: Accurics Scan 🚀
        id: accurics-scan
        uses: docker://accurics/terrascan-action:latest
        with:
          only_warn: true
          verbose: true
          webhook_url: https://app.accurics.com/v1/api/terrascan
          webhook_token: d73c0d62-aab9-4d65-940d-2e6b690a48a7
      - name: Upload  🚀
        uses: actions/upload-artifact@v2
        with:
          name: amis-admin-vue
          path: dist
      - name: lighthouse 🚀
        uses: treosh/lighthouse-ci-action@v2
        with:
          runs: 5
          uploadArtifacts: true
          configPath: './.github/workflows/setup/lighthouse-audit.json'
    needs: build
