Skip to content

Merge pull request #3174 from nfs0619/translate #6

Merge pull request #3174 from nfs0619/translate

Merge pull request #3174 from nfs0619/translate #6

name: Test deployment
permissions:
contents: read
statuses: write
on:
pull_request:
paths-ignore:
- "ISSUE_TEMPLATE/**"
- ".github/**.md"
- ".gitignore"
- "demo/**"
- "docker/**"
- "HOW_TO.md"
- "TASK.md"
- "SECURITY.md"
- "README.md"
- "README_zh-CN.md"
# Review gh actions docs if you want to further define triggers, paths, etc
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#on
# 添加push事件,确保翻译后的提交也会触发检查
push:
# 只在PR分支上触发,避免在主分支上重复运行
branches-ignore:
- main
- master
- docusaurus-version
paths-ignore:
- "ISSUE_TEMPLATE/**"
- ".github/**.md"
- ".gitignore"
- "demo/**"
- "docker/**"
- "HOW_TO.md"
- "TASK.md"
- "SECURITY.md"
- "README.md"
- "README_zh-CN.md"
# 添加workflow_dispatch,允许从其他工作流触发
workflow_dispatch:
inputs:
pr_number:
description: 'PR number to test'
required: true
type: string
ref:
description: 'Branch or commit to test'
required: true
type: string
head_repo:
description: 'Head repo full_name (owner/repo)'
required: false
type: string
trigger_source:
description: 'What triggered this test'
required: false
type: string
default: 'manual'
env:
NODE_OPTIONS: --max_old_space_size=8192
jobs:
test-deploy:
name: Test deployment
runs-on: ubuntu-latest
steps:
- name: Get PR info (for workflow_dispatch)
if: github.event_name == 'workflow_dispatch'
id: pr-info
uses: actions/github-script@v7
with:
script: |
const { owner, repo } = context.repo;
const pr_number = '${{ inputs.pr_number }}';
const inputRef = '${{ inputs.ref }}';
const inputHeadRepo = '${{ inputs.head_repo }}';
try {
const { data: pr } = await github.rest.pulls.get({
owner, repo, pull_number: parseInt(pr_number)
});
core.setOutput('sha', pr.head.sha);
core.setOutput('ref', pr.head.ref);
core.setOutput('head-repo', pr.head.repo.full_name);
core.info(`PR #${pr_number}: head=${pr.head.repo.full_name} ref=${pr.head.ref} sha=${pr.head.sha}`);
} catch (error) {
core.warning('Failed to get PR via API, fallback to inputs: ' + error.message);
core.setOutput('sha', inputRef); // 只是占位;稍后 checkout 后我们不会用它
core.setOutput('ref', inputRef);
core.setOutput('head-repo', inputHeadRepo || '');
}
- uses: actions/checkout@v4
with:
repository: ${{ github.event_name == 'workflow_dispatch' && steps.pr-info.outputs['head-repo'] || github.repository }}
ref: ${{ steps.pr-info.outputs.ref || github.ref }}
- uses: actions/setup-node@v3
with:
node-version: 20
cache: yarn
# ==== 新增:观测与加 swap(放在安装依赖前) ====
- name: Show runner memory
run: node -v && free -h && df -h
- name: Add swap (8G)
run: |
sudo fallocate -l 8G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
free -h
# ==== 新增结束 ====
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Test build website
run: yarn docusaurus build
# 报告状态到PR(不添加评论)
- name: Report status to PR
if: always()
continue-on-error: true
uses: actions/github-script@v7
with:
script: |
const { owner, repo } = context.repo;
const success = '${{ job.status }}' === 'success';
let sha;
if (context.eventName === 'pull_request') {
sha = context.payload.pull_request.head.sha;
} else if (context.eventName === 'workflow_dispatch') {
sha = '${{ steps.pr-info.outputs.sha }}' || '${{ inputs.ref }}';
} else {
sha = context.sha;
}
const statusContext = 'Test deployment (required)';
try {
await github.rest.repos.createCommitStatus({
owner,
repo,
sha,
state: success ? 'success' : 'failure',
target_url: `https://github.com/${owner}/${repo}/actions/runs/${{ github.run_id }}`,
description: success ? '✅ Build succeeded' : '❌ Build failed',
context: statusContext
});
core.info(`Status reported on ${sha}: ${statusContext} = ${success ? 'success' : 'failure'}`);
} catch (error) {
core.warning('Failed to report status: ' + error.message);
}