修正上传文章链接 #33
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build Check | |
| on: | |
| push: | |
| branches: | |
| - main | |
| pull_request_target: | |
| types: [opened, synchronize, reopened] | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} | |
| cancel-in-progress: true | |
| jobs: | |
| check-merge-target: | |
| name: Check merge target | |
| runs-on: ubuntu-latest | |
| outputs: | |
| branch_error: ${{ steps.check.outputs.branch_error }} | |
| steps: | |
| - name: Block gh-pages merges | |
| continue-on-error: false | |
| id: check | |
| if: github.base_ref == 'gh-pages' | |
| run: | | |
| echo "##################################################" | |
| echo "不允许直接合并到 gh-pages 分支!!!" >> $GITHUB_OUTPUT | |
| exit 1 | |
| build: | |
| name: Build site | |
| needs: check-merge-target | |
| runs-on: ubuntu-latest | |
| outputs: | |
| build_error: ${{ steps.build.outputs.build_error }} | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v3 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v3 | |
| with: | |
| node-version: '22' | |
| - name: Install dependencies | |
| run: | | |
| npm install | |
| npm install -g hexo | |
| npm ci | |
| - name: Install theme dependencies | |
| run: | | |
| cd themes/default | |
| npm install | |
| cd ../.. | |
| - name: Clean | |
| run: npx hexo clean | |
| - name: Build check | |
| id: build | |
| continue-on-error: true | |
| run: | | |
| if ! npx hexo generate --debug; then | |
| echo "build_error<<EOF" >> $GITHUB_OUTPUT | |
| echo "Hexo 构建失败。请检查以下可能的问题:" >> $GITHUB_OUTPUT | |
| echo "- 文章格式是否正确" >> $GITHUB_OUTPUT | |
| echo "- Front-matter 语法是否正确" >> $GITHUB_OUTPUT | |
| echo "- Markdown 语法是否有误" >> $GITHUB_OUTPUT | |
| echo "EOF" >> $GITHUB_OUTPUT | |
| exit 1 | |
| fi | |
| - name: Upload build artifacts | |
| uses: actions/upload-artifact@v3 | |
| with: | |
| name: public | |
| path: public/ | |
| validate-html: | |
| name: Validate HTML | |
| needs: build | |
| runs-on: ubuntu-latest | |
| outputs: | |
| html_error: ${{ steps.validate.outputs.html_error }} | |
| steps: | |
| - name: Download build artifacts | |
| uses: actions/download-artifact@v3 | |
| with: | |
| name: public | |
| path: public/ | |
| - name: Install tidy | |
| run: sudo apt-get install tidy -y | |
| - name: Check HTML files | |
| id: validate | |
| continue-on-error: true | |
| run: | | |
| ERRORS=$(mktemp) | |
| if ! find public -name "*.html" -exec tidy -e {} \; 2> "$ERRORS"; then | |
| echo "html_error<<EOF" >> $GITHUB_OUTPUT | |
| echo "HTML 验证失败。发现以下问题:" >> $GITHUB_OUTPUT | |
| cat "$ERRORS" >> $GITHUB_OUTPUT | |
| echo "EOF" >> $GITHUB_OUTPUT | |
| exit 1 | |
| fi | |
| comment-on-pr: | |
| name: Comment results | |
| needs: [build, validate-html] | |
| runs-on: ubuntu-latest | |
| if: always() && github.event_name == 'pull_request_target' | |
| steps: | |
| - uses: actions/github-script@v6 | |
| env: | |
| BUILD_ERROR: ${{ needs.build.outputs.build_error }} | |
| HTML_ERROR: ${{ needs.validate-html.outputs.html_error }} | |
| with: | |
| script: | | |
| const buildSuccess = '${{ needs.build.result }}' === 'success'; | |
| const validateSuccess = '${{ needs.validate-html.result }}' === 'success'; | |
| let message = ''; | |
| if (buildSuccess && validateSuccess) { | |
| message = '✅ 构建检查完全通过!\n- 代码构建成功\n- HTML验证通过'; | |
| } else { | |
| message = '❌ 构建检查失败!\n\n'; | |
| if (!buildSuccess) { | |
| message += '### 构建错误\n' + process.env.BUILD_ERROR + '\n\n'; | |
| } | |
| if (!validateSuccess) { | |
| message += '### HTML验证错误\n' + process.env.HTML_ERROR + '\n\n'; | |
| } | |
| message += '\n请修复以上问题后重新提交。'; | |
| } | |
| await github.rest.issues.createComment({ | |
| issue_number: context.issue.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: message | |
| }); | |
| // 根据检查结果设置最终状态 | |
| if (!buildSuccess || !validateSuccess) { | |
| process.exit(1); | |
| } | |
| process.exit(0); |