# 当分支 main 有 push 事件，并且此次 push 包含了 demo 目录下的文件时触发，使用 GitHub Actions 自动构建 Demo 并部署到服务器（使用 ubuntu-latest），具体步骤如下：
# 1. 安装 nodejs , python3 , pip
# 2. pip 依次安装 aliyun-python-sdk-cdn , aliyun-python-sdk-core , logging
# 3. cd demo 安装依赖 npm install
# 4. 打包 npm run build
# 5. 使用 appleboy/scp-action@master 将 demo/dist 目录下所有文件上传到服务器上 demo 目录下
# 6. 执行 demo/Refresh-auto.py 刷新 CDN 缓存

name: BUILD DEMO

on:
    push:
        branches:
            - main
        paths:
            - 'demo/**'

jobs:
    build-and-deploy:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout
              uses: actions/checkout@v2
            - name: Setup Node.js environment
              uses: actions/setup-node@v1
              with:
                  node-version: 18.x

            - name: Setup Python environment
              uses: actions/setup-python@v2
              with:
                  python-version: 3.x
            - name: Install pip
              run: python3 -m pip install --upgrade pip
            - name: Install aliyun-python-sdk-cdn
              run: python3 -m pip install aliyun-python-sdk-cdn
            - name: Install aliyun-python-sdk-core
              run: python3 -m pip install aliyun-python-sdk-core
            - name: Install logging
              run: python3 -m pip install logging

            # cd packages/rollup-plugin-stdf-icon 目录下
            # 安装依赖 npm install
            # 打包 npm run build
            - name: Build rollup-plugin-stdf-icon
              run: |
                  cd packages/rollup-plugin-stdf-icon
                  npm install
                  npm run build

            # cd packages/stdf 目录下
            # 执行 node/scripts/dist.js 脚本
            - name: Build stdf
              run: |
                  cd packages/stdf
                  node scripts/dist.js

            - name: Install dependencies and build
              run: |
                  cd demo
                  npm install
                  npm run build

            - name: Upload
              uses: appleboy/scp-action@master
              with:
                  host: ${{ secrets.ALIYUN_HOST }}
                  username: ${{ secrets.ALIYUN_USERNAME }}
                  port: ${{ secrets.ALIYUN_PORT }}
                  password: ${{ secrets.ALIYUN_PRIVATE_KEY }}
                  source: demo/dist/*
                  target: /usr/share/nginx/demo_stdf/
                  strip_components: 2

            - name: Refresh CDN
              env:
                  ALIYUN_CDN_AK: ${{ secrets.ALIYUN_CDN_AK }}
                  ALIYUN_CDN_SK: ${{ secrets.ALIYUN_CDN_SK }}
              run: python3 demo/Refresh-auto.py -r demo/cdn-list.txt -t clear -o Directory
