feat:add aliyun oss deploy

This commit is contained in:
zyronon
2025-08-16 01:11:34 +08:00
parent 3b658f5ad5
commit 0773a5825f
2 changed files with 129 additions and 15 deletions

View File

@@ -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 }}

86
deploy-oss.js Normal file
View File

@@ -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)
})