AI 文章摘要
XinruiGPT
加载中...
此内容根据文章生成,并未经过人工审核,仅用于文章内容的解释与总结

前言

最近把自己的hexo网站上传到了github上,然后就兴趣上来想构建一下看看,于是就用AI写了一个脚本,可以把构建的文件生成到别的分支上。

只需要将脚本放到.github/workflows/目录下的文件下即可,文件名可以自己随便取,然后将代码放进去

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
name: Hexo Build and Deploy to Pages Branch

on:
release:
types: [created]
workflow_dispatch:
inputs:
buildEnvironment:
description: '构建环境'
required: true
default: 'production'
type: choice
options:
- production
- development
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build-and-deploy:
runs-on: ubuntu-latest
permissions:
contents: write # 增加写权限,解决权限不足问题
steps:
- uses: actions/checkout@v4
with:
submodules: true
fetch-depth: 0 # 拉取完整历史,避免git操作失败

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'

- name: Install dependencies
run: npm ci

- name: Install Hexo CLI
run: npm install -g hexo-cli

- name: Build Hexo site
run: hexo generate
env:
NODE_ENV: ${{ github.event.inputs.buildEnvironment || 'production' }}

- name: Clean pages branch (if exists)
run: |
git fetch origin pages || true
git checkout pages || git checkout --orphan pages
git rm -rf . || true
continue-on-error: true # 分支不存在时也继续执行

- name: Copy build files
run: cp -r ./public/* .

- name: Deploy to pages branch
run: |
git config --global user.email "action@github.com"
git config --global user.name "GitHub Action"
git add .
git commit -m "Deploy from ${{ github.sha }}" || echo "No changes to commit"
git push origin pages --force

经测试是正常的,我也看不懂Github的脚本,有懂的大佬可以自行更改,并留言。