Skip to content

ci: deploy to tencent cos #3837

ci: deploy to tencent cos

ci: deploy to tencent cos #3837

Workflow file for this run

name: Build Hugo and Deploy
on:
push:
branches:
- main
schedule:
- cron: '0 0 1 * *'
workflow_dispatch:
repository_dispatch:
types: [build]
jobs:
build-deploy:
runs-on: ubuntu-latest
env:
ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }}
ACCESS_KEY_SECRET: ${{ secrets.ACCESS_KEY_SECRET }}
COS_BUCKET: ${{ secrets.COS_CN_BUCKET }}
COS_SECRET_ID: ${{ secrets.SECRET_COS_ID }}
COS_SECRET_KEY: ${{ secrets.SECRET_COS_KEY }}
DIRECTUS_ACCESS_TOKEN: ${{ secrets.DIRECTUS_ACCESS_TOKEN }}
DIRECTUS_API_URL: ${{ secrets.DIRECTUS_API_URL }}
DIRECTUS_FILES_URL: ${{ secrets.DIRECTUS_FILES_URL }}
DIRECTUS_S3_URL: ${{ secrets.DIRECTUS_S3_URL }}
ESA_SITE_ID: ${{ secrets.ESA_SITE_ID }}
HUGOMODS_CACHEDIR: /tmp/.hugo_mods_cache
HUGO_CACHEDIR: /tmp/.hugo_cache
NEODB_ACCESS_TOKEN: ${{ secrets.NEODB_ACCESS_TOKEN }}
PNPM_CACHEDIR: /tmp/.pnpm-store
STEAM_WEB_API_KEY: ${{ secrets.STEAM_WEB_API_KEY }}
steps:
- uses: actions/checkout@v5
with:
submodules: recursive
fetch-depth: 0
- name: Setup Hugo
uses: peaceiris/actions-hugo@v3
with:
hugo-version: '0.152.2'
extended: false
- name: Cache Hugo Resources
uses: actions/cache@v4
id: cache-hugo
with:
path: |
resources/_gen
${{ env.HUGO_CACHEDIR }}
key: ${{ runner.os }}-hugo-${{ hashFiles('content/**', 'assets/**', 'layouts/**', 'config/**', 'static/**', 'pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-hugo-
- name: Cache Hugo Modules
uses: actions/cache@v4
with:
path: ${{ env.HUGOMODS_CACHEDIR }}
key: ${{ runner.os }}-hugomod-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-hugomod-
- name: Setup PNPM
uses: pnpm/action-setup@v4
with:
version: 10
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 22.x
cache: 'pnpm'
cache-dependency-path: '**/pnpm-lock.yaml'
- name: Install Dependencies
run: |
pnpm install
- name: Setup Aliyun CLI
uses: aliyun/setup-aliyun-cli-action@v1
with:
version: "latest"
- name: Configure Aliyun CLI
run: |
aliyun configure set --profile default \
--mode "AK" \
--access-key-id ${{ secrets.ACCESS_KEY_ID }} \
--access-key-secret ${{ secrets.ACCESS_KEY_SECRET }} \
--region cn-shanghai \
--language zh
- name: Build Site
id: build
timeout-minutes: 5
run: |
echo "🔨 Starting build..."
echo "📚 Fetching full site data..."
# 使用 set +e 允许命令失败而不立即退出
set +e
# 执行 fetch 脚本,如果任何一个失败,FETCH_STATUS 将非 0
(
set -e
pnpm run directus:article
pnpm run directus:friendslinks
pnpm run directus:pages
pnpm run fetch:stats
)
FETCH_STATUS=$?
# 恢复 set -e
set -e
if [ $FETCH_STATUS -ne 0 ]; then
echo "⚠️ Data fetching failed. Skipping build and deployment."
echo "should_deploy=false" >> $GITHUB_OUTPUT
# 正常退出,保持 workflow 为绿色
exit 0
fi
# Build Hugo site
echo "🏗️ Building Hugo site..."
pnpm run gen:llms
pnpm run build:hugo
pnpm run build:pagefind
echo "✅ Build completed"
echo "should_deploy=true" >> $GITHUB_OUTPUT
- name: Deploy to Tencent COS
if: steps.build.outputs.should_deploy == 'true'
uses: eallion/s3-deploy-action@dev
with:
provider: tencent
cos_secret_id: ${{ secrets.SECRET_COS_ID }}
cos_secret_key: ${{ secrets.SECRET_COS_KEY }}
cos_bucket: ${{ secrets.COS_CN_BUCKET }}
cos_region: ap-shanghai
folder: public
incremental: true
skipSetting: true
# - name: Purge Aliyun ESA
# if: steps.build.outputs.should_deploy == 'true'
# timeout-minutes: 10
# run: |
# echo "🗑️ Purging cache..."
# echo "📚 Full site purge - purging all cache..."
# aliyun esa PurgeCaches --SiteId ${{ secrets.ESA_SITE_ID }} --Type purgeall --Content '{"PurgeAll":true}'
# echo "✅ Cache purge completed"