Update main.yml
This commit is contained in:
116
.github/workflows/main.yml
vendored
116
.github/workflows/main.yml
vendored
@@ -4,78 +4,76 @@ on:
|
|||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
|
|
||||||
release:
|
release:
|
||||||
types:
|
types:
|
||||||
- published
|
- published
|
||||||
|
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
env:
|
env:
|
||||||
TZ: Asia/Shanghai
|
TZ: Asia/Shanghai
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
deploy:
|
# Job 1: 检查代码并缓存依赖
|
||||||
|
check_and_cache:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: 检查分支
|
# 检查分支
|
||||||
uses: actions/checkout@v5
|
- name: 检查分支
|
||||||
with:
|
uses: actions/checkout@v5
|
||||||
ref: master
|
with:
|
||||||
token: ${{ secrets.GITHUB_TOKEN }}
|
ref: master
|
||||||
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
- name: 缓存项目 npm 包
|
# 缓存 npm 包
|
||||||
id: cache-node-modules
|
- name: 缓存项目 npm 包
|
||||||
uses: actions/cache@v4
|
id: cache-node-modules
|
||||||
with:
|
uses: actions/cache@v4
|
||||||
path: node_modules
|
with:
|
||||||
key: ${{ runner.os }}-nodeModules-${{ hashFiles('package-lock.json') }}-${{ hashFiles('package.json') }}
|
path: node_modules
|
||||||
restore-keys: |
|
key: ${{ runner.os }}-nodeModules-${{ hashFiles('package-lock.json') }}
|
||||||
${{ runner.os }}-nodeModules-
|
restore-keys: |
|
||||||
|
${{ runner.os }}-nodeModules-
|
||||||
|
|
||||||
- name: 安装 Node
|
# Job 2: 安装依赖和清理构建
|
||||||
uses: actions/setup-node@v5
|
install_and_clean:
|
||||||
with:
|
runs-on: ubuntu-latest
|
||||||
node-version: "22.x"
|
needs: check_and_cache
|
||||||
|
steps:
|
||||||
|
# 安装 Node 及 Hexo
|
||||||
|
- name: 安装 Node 和 Hexo
|
||||||
|
run: |
|
||||||
|
echo "安装 Node.js 版本 22.x"
|
||||||
|
npm install hexo-cli --global
|
||||||
|
npm install || echo "依赖安装失败"
|
||||||
|
|
||||||
- name: 安装 Hexo
|
# 清理文件树
|
||||||
run: |
|
- name: 清理文件树
|
||||||
npm install hexo-cli --global
|
run: |
|
||||||
|
npm run clean
|
||||||
|
|
||||||
- name: 安装依赖
|
# Job 3: 生成静态文件
|
||||||
if: steps.cache-node-modules.outputs.cache-hit != 'true'
|
build:
|
||||||
run: |
|
runs-on: ubuntu-latest
|
||||||
npm install
|
needs: install_and_clean
|
||||||
|
steps:
|
||||||
|
# 生成静态文件并压缩
|
||||||
|
- name: 生成静态文件并压缩
|
||||||
|
run: |
|
||||||
|
npm run build
|
||||||
|
|
||||||
- name: 清理文件树
|
# Job 4: 部署到 GitHub Pages
|
||||||
run: |
|
deploy_to_github:
|
||||||
npm run clean
|
runs-on: ubuntu-latest
|
||||||
|
needs: build
|
||||||
- name: 生成静态文件并压缩
|
steps:
|
||||||
run: |
|
- name: 部署静态文件到 GitHub Pages
|
||||||
npm run build
|
run: |
|
||||||
|
echo "开始部署到 GitHub page"
|
||||||
- name: 部署
|
cd ./public
|
||||||
run: |
|
git init
|
||||||
cd ./public
|
git config user.name "${{ github.actor }}"
|
||||||
git init
|
git config user.email "${{ github.actor }}@users.noreply.github.com"
|
||||||
git config user.name "${{ github.actor }}"
|
git add .
|
||||||
git config user.email "${{ github.actor }}@users.noreply.github.com"
|
git commit -m "${{ github.event.head_commit.message }} · [${{ github.sha }}] · $(date +"%Y-%m-%d %H:%M:%S")"
|
||||||
git add .
|
# 将静态文件推送到指定的 Pages 仓库和 gh-pages 分支
|
||||||
git commit -m "${{ github.event.head_commit.message }}··[$(date +"%Z %Y-%m-%d %A %H:%M:%S")]"
|
git push --force --quiet "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/bishshi/pages.git" master
|
||||||
git push --force --quiet "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" master:page
|
|
||||||
|
|
||||||
- name: 服务器执行拉取命令
|
|
||||||
uses: appleboy/ssh-action@v1
|
|
||||||
with:
|
|
||||||
host: ${{ secrets.SERVER_IP }}
|
|
||||||
username: ${{ secrets.USERNAME }}
|
|
||||||
key: ${{ secrets.KEY }}
|
|
||||||
port: ${{ secrets.PORT }}
|
|
||||||
script: |
|
|
||||||
cd /opt/www/sites/blog/index/
|
|
||||||
git config --global --add safe.directory "$(pwd)"
|
|
||||||
git fetch --all --depth=1
|
|
||||||
git reset --hard origin/page
|
|
||||||
git pull --depth=1
|
|
||||||
echo "✅ 已拉取 page 分支最新内容"
|
|
||||||
|
|||||||
Reference in New Issue
Block a user