博客部署及更新
约 878 字大约 3 分钟
2025-09-16
使用GitHub Actions
name: Deploy VuePress site to Pages
on:
# 在推送到 main 分支时触发
push:
branches: [main]
# 允许手动触发
workflow_dispatch:
# 设置 GITHUB_TOKEN 的权限以允许部署到 GitHub Pages
permissions:
contents: read
pages: write
id-token: write
# 只允许同时进行一次部署,跳过正在运行和最新队列之间的运行队列
concurrency:
group: pages
cancel-in-progress: false
jobs:
# 构建工作
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0 # 如果未启用 lastUpdated,则不需要
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- name: Install dependencies
run: npm ci
- name: Build with VuePress
run: npm run docs:build
- name: Setup Pages
uses: actions/configure-pages@v4
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: docs/.vuepress/dist
# 部署工作
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
needs: build
runs-on: ubuntu-latest
name: Deploy
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
部署
将上面的内容保存到
~/.github/workflows/deploy.yml
,没有这个文件就去创建。在GitHub仓库设置中:
- 进入
Settings
→Pages
- Source 选择
GitHub Actions
- 进入
前往项目根目录
~/
,并依次执行:# 初始化 Git 仓库 git init # 添加远程仓库 git remote add origin [email protected]:<username>/<repo>.git # 或者如果你使用 HTTPS(推荐) git remote add origin https://github.com/<username>/<repo>.git # 创建并切换到 main 分支 git checkout -b main
推送代码到main分支:
git add . git commit -m "setup github actions deployment" git push origin main
更新
使用git-bash
,进入博客文章根目录,依次执行:
git add .
git commit -m "change"
git push origin main
使用sh脚本
将下面的sh文件保存到~/deploy.sh
,使用git-bash
执行bash deploy.sh
即可将博客部署或者更新到网站上。
#!/usr/bin/env sh
# 确保脚本抛出遇到的错误
set -e
# 生成静态文件
npm run docs:build
# 进入生成的文件夹
cd docs/.vuepress/dist
# 添加 CNAME 文件用于自定义域名
echo 'your.cname' > CNAME
# 检查是否已经是git仓库
if [ ! -d ".git" ]; then
git init
git remote add origin [email protected]:<username>/<repo>.git
else
# 如果已经存在,确保远程仓库正确
git remote set-url origin [email protected]:gaifagafin/gaifagafin.github.io.git
fi
git add -A
# 检查是否有变化需要提交
if git diff --staged --quiet; then
echo "没有变化需要部署"
else
git commit -m "deploy: $(date '+%Y-%m-%d %H:%M:%S')"
# 推送到 main 分支(GitHub Pages 默认分支)
git push -f origin HEAD:main
echo "部署完成!"
fi
cd -
使用bat批处理脚本push
将下面的bat文件保存到~/push.bat
,执行脚本即可将博客更新到网站上,这样就不用打开git-bash再依次输入那三条指令这么麻烦了。
@echo off
chcp 65001 >nul
echo 正在执行Git提交和推送...
echo.
echo [1/3] 添加所有文件到暂存区...
git add .
if %errorlevel% neq 0 (
echo 错误:添加文件失败!
pause
exit /b 1
)
echo ✓ 文件添加成功
echo.
echo [2/3] 提交更改...
git commit -m "setup github actions deployment"
if %errorlevel% neq 0 (
echo 错误:提交失败!可能没有需要提交的更改。
pause
exit /b 1
)
echo ✓ 提交成功
echo.
echo [3/3] 推送到远程仓库...
git push origin main
if %errorlevel% neq 0 (
echo 错误:推送失败!请检查网络连接和权限。
pause
exit /b 1
)
echo ✓ 推送成功
echo.
echo 🎉 所有操作完成!GitHub Actions将自动开始部署。
echo 可以在GitHub仓库的Actions标签页查看部署进度。
pause
可能遇到的问题
无法连接到22端口
解决方法:
在本地的~/.ssh/config
文件中添加以下内容:
Host github.com
Hostname ssh.github.com
Port 443
使用git-bash测试ssh -T [email protected]
,成功连接。