菜单
菜单
文章目录
  1. 部署到Github
    1. Github配置密钥
    2. Github配置Action
  2. 部署到云服务器
    1. 云服务器安装Git
    2. 云服务器生成密钥
    3. 云服务器部署密钥
    4. Pull到云服务器
  3. 结束语
  4. 参考文章

自动化多线部署Hexo博客

本站之前的部署方案是在本地写好文章并生成静态文件后再 Push 到Github 并通过 WINscp 手动上传到个人云服务器,最后再把博客源代码备份到 Github 私有仓库。

每次更新文章都老费劲了,需要操作的步骤有点过于繁琐!

现利用 Github Action 持续集成并部署 Hexo 博客。

大致思路:

  • 源码文件 Push 到 GitHub Blog 私有仓库 master 分支
  • Github Action 在 Blog 私有仓库 master 分支更新后,自动构建生成站点文件
  • Github Action 将生成的静态文件推送到 username.github.io 仓库 master 分支
  • 云服务器从 GitHub Pagemaster 分支拉取更新

也就是说,整个部署过程只需要将写好源码文件 Push 到 GitHub 上存放源码仓库的 master 分支,后面的操作交给 Github Action 处理即可。

部署到Github

Github配置密钥

在博客根目录下生成部署密钥

ssh-keygen -f deploy-key

根目录下会有私钥 deploy-key 和公钥 deploy-key.pub 两个文件,注意排除俩文件不要push到GitHub去。

复制私钥 deploy-key 的内容,在博客源码仓库的 Settings → Secrets → New repository secret 添加。

公钥 deploy-key.pub 的内容,在 username.github.io 仓库 Settings → Secrets → New repository secret 添加。

若以上生成的公钥、私钥Github自动部署失败,可直接复制您电脑上的公钥、私钥配置。

Github配置Action

在存放 Hexo 博客源码的私有仓库开启 Action,并创建 .github/workflows/action.yml 配置文件,其中 action.yml 名字随意取。

如下是一个简单且可用的配置文件,只需修改 Git 的用户名和邮箱即可。

# workflow name
name: Blog CI

# master branch on push, auto run
on:
push:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest

steps:
# check it to your workflow can access it
# from: https://github.com/actions/checkout
- name: Checkout Repository master branch
uses: actions/checkout@master

# from: https://github.com/actions/setup-node
- name: Setup Node.js 12.x
uses: actions/setup-node@master
with:
node-version: "12.x"

- name: Setup Hexo Dependencies
run: |
npm install hexo-cli -g
npm install

- name: Setup Deploy Private Key
env:
HEXO_DEPLOY_PRIVATE_KEY: ${{ secrets.HEXO_DEPLOY_PRIVATE_KEY }}
run: |
mkdir -p ~/.ssh/
echo "$HEXO_DEPLOY_PRIVATE_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts

- name: Setup Git Infomation
run: |
git config --global user.name '用户名'
git config --global user.email '邮箱地址'
- name: Deploy Hexo
run: |
hexo clean
hexo generate
hexo deploy

action.yml文件配置好并保存后会自动执行一次,配置无误后若自动部署成功则会出现绿色的

打开你的 username.github.io 仓库即可看到已经部署成功。


部署到云服务器

云服务器安装Git

在云服务器上执行命令安装Git

yum install git

云服务器生成密钥

依次执行命令

git config --global user.name "用户名"
git config --global user.email "邮箱地址"
ssh-keygen -t rsa -C "邮箱地址"

验证是否配置成功

ssh -T git@github.com

云服务器部署密钥

将生成的私钥复制到username.github.io仓库的 Settings → Secrets → New repository secret 并添加。

而生成的公钥需要复制到 Github 的设置中的 SSH and GPG keysNew SSH key 处添加。

Pull到云服务器

在云服务器上执行命令初始化 Git 仓库并拉取文件。

#To站点目录
cd /www/wwwroot/blog
#初始化git
git init
#添加远程仓库
git remote add origin git@github.com:username/username.github.io.git
#获取远端更新
git fetch origin
#pull远端代码到本地
git pull
#如果报错:refusing to merge unrelated histories,则用下面的命令
git pull --allow-unrelated-histories
#或者直接Git远端覆盖本地
git reset --hard origin/master

在云服务器宝塔面板上开启一个定时任务即可,shell脚本为

cd /www/wwwroot/blog
git remote add origin git@github.com:username/username.github.io.git
git fetch origin
git reset --hard origin/master

这样云服务器就能定时更新静态站内容,而不用手动操作了。

结束语

当完成以上配置以后,更新文章只需修改源代码后,执行命令三连

git add .
git commit
git push

PS: 默认已经初始化过本地仓库

剩下的生成静态文件、部署到静态文件仓库和云服务器仓库的任务就交给 Github Action 和 Shell 脚本即可。

双手瞬间轻松了很多啊👌🤣

参考文章

本文作者: Senorui

本文链接: https://senorui.top/posts/17d1.html

版权声明: 本站所有文章除特别声明外,均采用【CC BY-NC-ND 4.0】国际许可协议,若转载请注明出处!

支持一下
知识无价,欢迎打赏🍖
  • 微信扫一扫
  • 支付宝扫一扫