name: Deploy

on:
  push:
    branches:
      - deploy

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout 
        uses: actions/checkout@v3
        with:
          path: webs
      
      - name: Checkout meta2d.js repo
        uses: actions/checkout@v3
        with:
          repository: le5le-com/meta2d.js
          path: meta2d.js

      - name: Checkout 2d-components repo
        uses: actions/checkout@v3
        with:
          repository: le5le-com/2d-components
          token: ${{ secrets.PAT }}
          path: 2d-components

      - name: Install core
        run:  |
          cd meta2d.js
          yarn    

      - uses: pnpm/action-setup@v2
        with:
          version: latest

      - name: Build
        run:  |
          cd webs
          pnpm i && pnpm build    

      - name: 安装COS
        run: |
          sudo pip install coscmd
      - name: 配置COS
        env:
          SECRET_ID: ${{ secrets.SECRET_ID }}
          SECRET_KEY: ${{ secrets.SECRET_KEY }}
          BUCKET: ${{ secrets.BUCKET }}
          REGION: ${{ secrets.COS_REGION }}
        run: |
          coscmd config -a $SECRET_ID -s $SECRET_KEY -b $BUCKET -r $REGION
      - name: Upload COS
        run: |
          coscmd upload -rfs ./webs/v/ /v/
        
      - name: Upload index.html
        uses: appleboy/scp-action@v0.1.4
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          password: ${{ secrets.PASSWORD }}
          port: 22
          source: webs/v/index.html
          target: ${{ secrets.WEB }} 
