name: deploy landing

# push main 且 landing 目录变化 → 自动部署到 OSS bucket
# 权限：最小权限 RAM 子账号 aliyun-landing-publisher（只能写
# agentaily-xiangqin-landing bucket）。AK 存 GH Secrets。

on:
  push:
    branches: [main]
    paths:
      - 'landing/**'
      - '.github/workflows/deploy-landing.yml'
  workflow_dispatch:

jobs:
  deploy:
    name: 部署 landing 到阿里云 OSS
    runs-on: ubuntu-latest
    timeout-minutes: 5
    env:
      BUCKET: agentaily-xiangqin-landing
      REGION: cn-hangzhou
      DOMAIN: xiangqin.agentaily.com
    steps:
      - uses: actions/checkout@v4

      - name: 装 aliyun CLI
        run: |
          ARCH=$(uname -m | sed s/x86_64/amd64/ | sed s/aarch64/arm64/)
          cd /tmp
          curl -fsSL "https://aliyuncli.alicdn.com/aliyun-cli-linux-latest-${ARCH}.tgz" -o aliyun.tgz
          tar xzf aliyun.tgz
          sudo mv aliyun /usr/local/bin/
          aliyun --version

      - name: 配 aliyun profile（landing RAM）
        env:
          AK_ID: ${{ secrets.ALIYUN_LANDING_AK_ID }}
          AK_SECRET: ${{ secrets.ALIYUN_LANDING_AK_SECRET }}
        run: |
          aliyun configure set --profile default --mode AK \
            --access-key-id "$AK_ID" \
            --access-key-secret "$AK_SECRET" \
            --region "$REGION"

      - name: 上传 index.html 到 OSS
        run: |
          aliyun ossutil cp landing/index.html "oss://$BUCKET/index.html" \
            --cache-control "public, max-age=300, must-revalidate" \
            --content-type "text/html; charset=utf-8" -f

      - name: 验证生产访问
        run: |
          curl -sSf -o /dev/null -w "OSS endpoint: %{http_code}\n" \
            "https://$BUCKET.oss-$REGION.aliyuncs.com/index.html"
          curl -sSL -o /dev/null -w "custom domain $DOMAIN: %{http_code}\n" \
            "http://$DOMAIN/"

      - name: Summary
        run: |
          cat <<EOF >> "$GITHUB_STEP_SUMMARY"
          ### landing 部署完成 ✓
          - OSS endpoint: https://$BUCKET.oss-$REGION.aliyuncs.com/
          - 自定义域名: http://$DOMAIN/
          - 下次 push landing/ 目录自动再跑
          EOF
