From 0773a5825f1e2793407d38c5a53406fa987d73d6 Mon Sep 17 00:00:00 2001 From: zyronon Date: Sat, 16 Aug 2025 01:11:34 +0800 Subject: [PATCH] feat:add aliyun oss deploy --- .github/workflows/deploy-pages.yml | 58 ++++++++++++++------ deploy-oss.js | 86 ++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 15 deletions(-) create mode 100644 deploy-oss.js diff --git a/.github/workflows/deploy-pages.yml b/.github/workflows/deploy-pages.yml index 14c0b669..adca2130 100644 --- a/.github/workflows/deploy-pages.yml +++ b/.github/workflows/deploy-pages.yml @@ -1,32 +1,24 @@ -# 将静态内容部署到 GitHub Pages 的简易工作流程 -name: Deploy static content to Pages +name: Deploy to GitHub Pages & Aliyun OSS on: - # 仅在推送到默认分支时运行。 push: branches: [ 'master' ] - - # 这个选项可以使你手动在 Action tab 页面触发工作流 workflow_dispatch: -# 设置 GITHUB_TOKEN 的权限,以允许部署到 GitHub Pages。 permissions: contents: read pages: write id-token: write -# 允许一个并发的部署 concurrency: - group: 'pages' + group: 'deploy' cancel-in-progress: true jobs: - # 单次部署的工作描述 - deploy: - environment: - name: github-pages - url: ${{ steps.deployment.outputs.page_url }} + build: runs-on: ubuntu-latest + outputs: + build-path: dist steps: - name: Checkout uses: actions/checkout@v4 @@ -48,16 +40,52 @@ jobs: - name: Build run: pnpm run build + - name: Upload dist artifact + uses: actions/upload-artifact@v4 + with: + name: build-dist + path: ./dist + + deploy-pages: + runs-on: ubuntu-latest + needs: build + steps: + - name: Download build artifact + uses: actions/download-artifact@v4 + with: + name: build-dist + path: dist + - name: Setup Pages uses: actions/configure-pages@v4 - - name: Upload artifact + - name: Upload artifact for Pages uses: actions/upload-pages-artifact@v3 with: - # Upload dist repository path: './dist' - name: Deploy to GitHub Pages id: deployment uses: actions/deploy-pages@v4 + deploy-oss: + runs-on: ubuntu-latest + needs: build + steps: + - name: Download build artifact + uses: actions/download-artifact@v4 + with: + name: build-dist + path: dist + + - name: Install OSS SDK + run: npm install ali-oss @alicloud/pop-core + + - name: Deploy to OSS + Refresh CDN + run: node ./deploy-oss.js + env: + OSS_KEY_ID: ${{ secrets.OSS_KEY_ID }} + OSS_KEY_SECRET: ${{ secrets.OSS_KEY_SECRET }} + OSS_BUCKET: ${{ secrets.OSS_BUCKET }} + OSS_REGION: ${{ secrets.OSS_REGION }} + CDN_DOMAIN: ${{ secrets.CDN_DOMAIN }} diff --git a/deploy-oss.js b/deploy-oss.js new file mode 100644 index 00000000..423f897f --- /dev/null +++ b/deploy-oss.js @@ -0,0 +1,86 @@ +import OSS from 'ali-oss' +import fs from 'fs' +import path from 'path' +import Core from '@alicloud/pop-core' + +const { + OSS_REGION, + OSS_KEY_ID, + OSS_KEY_SECRET, + OSS_BUCKET, + CDN_DOMAIN +} = process.env + +if (!OSS_REGION || !OSS_KEY_ID || !OSS_KEY_SECRET || !OSS_BUCKET || !CDN_DOMAIN) { + console.error('❌ 缺少必要的环境变量,请检查 GitHub Secrets 配置') + process.exit(1) +} + +const client = new OSS({ + region: OSS_REGION, + accessKeyId: OSS_KEY_ID, + accessKeySecret: OSS_KEY_SECRET, + bucket: OSS_BUCKET +}) + +const cdnClient = new Core({ + accessKeyId: OSS_KEY_ID, + accessKeySecret: OSS_KEY_SECRET, + endpoint: 'https://cdn.aliyuncs.com', + apiVersion: '2018-05-10' +}) + +// 遍历 dist 目录,统计文件 +function getAllFiles(dir, fileList = []) { + const files = fs.readdirSync(dir) + for (const file of files) { + const filePath = path.join(dir, file) + const stat = fs.statSync(filePath) + if (stat.isDirectory()) { + getAllFiles(filePath, fileList) + } else { + fileList.push(filePath) + } + } + return fileList +} + +// 上传文件,显示进度 +async function uploadFiles(files, localBase = './dist') { + const total = files.length + let count = 0 + + for (const file of files) { + const relativePath = path.relative(localBase, file) + const remotePath = relativePath.split(path.sep).join('/') // 转 POSIX 路径 + await client.put(remotePath, file) + count++ + const percent = ((count / total) * 100).toFixed(1) + process.stdout.write(`\r📤 上传进度: ${count}/${total} (${percent}%) ${remotePath} `) + } + console.log('\n✅ 文件全部上传完成') +} + +// 刷新 CDN +async function refreshCDN() { + console.log('🔄 刷新 CDN 缓存...') + const params = { + ObjectPath: `https://${CDN_DOMAIN}/*`, + ObjectType: 'File' + } + const requestOption = { method: 'POST' } + const result = await cdnClient.request('RefreshObjectCaches', params, requestOption) + console.log('✅ CDN 刷新完成:', result) +} + +async function main() { + const files = getAllFiles('./dist') + console.log(`📁 共找到 ${files.length} 个文件,开始上传...`) + await uploadFiles(files) + await refreshCDN() +} + +main().catch(err => { + console.error('❌ 部署失败:', err) + process.exit(1) +})