前言
最近把自己的hexo网站上传到了github上,然后就兴趣上来想构建一下看看,于是就用AI写了一个脚本,可以把构建的文件生成到别的分支上。
只需要将脚本放到.github/workflows/目录下的文件下即可,文件名可以自己随便取,然后将代码放进去
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的脚本,有懂的大佬可以自行更改,并留言。