前言
使用hexo博客也挺久的,最開始是本地hexo clean && hexo g,最后hexo d推送到服務器。后來是本地hexo clean && hexo g,最后將生成文件推送到GitHub,GitHub actions 推送到服務器。后來本地文件丟失,GitHub 僅僅存着編譯好public文件夾內容,該丟失就丟失了。有GitHub actions何不直接把編譯、部署都交給他,還能備份博客,本地也不用安裝一堆環境,使用這個方法也有段時間了,記錄下。為了方便建站和編寫,還是本地建議安裝hexo。
一、初始配置
ssh密鑰配置
ps: 只有且使用一個密鑰就不要看了
github配置
建立私有倉庫fungit.org
,不要初始化倉庫。
本地生成兩份ssh密鑰,一份用來本地push到GitHub,一份GitHub actions用來推送到服務器
ssh-keygen -t rsa -b 4096 -f /c/Users/Administrator/Documents/ssh/fungit_blog_github
ssh-keygen -t rsa -b 4096 -f /c/Users/Administrator/Documents/ssh/github_to_server
在GitHub私有倉庫fungit.org
添加 Depoly key,把剛剛生成的fungit_blog_github.pub添加進去,勾選write權限,用來推送本地文件。
添加一個Secret,把剛剛生成的github_to_server添加進去(注意是私鑰)名為GITHUB_TO_SERVER_KEY
,用來GitHub actions推送到服務器。
服務器配置
新建git用戶並配置密碼
useradd -m -s /bin/bash git
passwd git
將認證公鑰加入git用戶認證文件
ssh-copy-id -i /c/Users/Administrator/Documents/ssh/github_to_server.pub git@ip -p 22222
- -i: 指定文件
- -p: ssh連接端口
你也可以手動在/home/git 新建.ssh/authorized_keys
文件,把github_to_server.pub添加到authorized_keys。.ssh
文件夾默認權限為700,authorized_keys文件默認權限是600。
本地配置
安裝hexo最新版本,最好安裝最新版本git-scm, git bash挺好用的。
本地配置多個ssh-key,需要手動配置使用指定的ssh密鑰。使用git bash,新建config
文件
vim ~/.ssh/config
#添加以下內容
Host fungit.blog
HostName github.com
IdentityFile C:\\Users\\Administrator\\.ssh\\fungit_blog_github
PreferredAuthentications publickey
IdentitiesOnly yes
- Host: 區分默認的github.com,倉庫鏈接使用
fungit.blog
替換github.com
推送時就會匹配IdentityFile對應的密鑰文件。
配置hexo站點
hexo init fungit.org
cd fungit.org
rm -rf .git
git init
連接私有倉庫fungit.org
git remote -add origin git@fungit.blog:kroyoo/fungit.org.git
- 注意
git@github.com
使用上面config配置的git@fungit.blog
替換。
測試是否可以正常通信
$ ssh -T git@fungit.org
Hi fungit.org! You've successfully authenticated, but GitHub does not provide shell access.
至此,ssh密鑰配置完成,當然,如果本地只有使用一個默認密鑰id_rsa,不用那么麻煩,直接新建站點連接倉庫就行了。
二、自動化部署
本地配置
如果你是跟我一樣不喜歡本地安裝依賴,只需手動把依賴添加到package.json
我的package.json
文件參考:
{
....
...
"dependencies": {
"hexo": "^5.0.0",
"hexo-deployer-git": "^2.1.0",
"hexo-generator-archive": "^1.0.0",
"hexo-generator-category": "^1.0.0",
"hexo-generator-index": "^2.0.0",
"hexo-generator-tag": "^1.0.0",
"hexo-renderer-ejs": "^1.0.0",
"hexo-renderer-jade": "^0.5.0",
"hexo-renderer-marked": "^3.0.0",
"hexo-renderer-stylus": "^2.0.0",
"hexo-server": "^2.0.0",
"hexo-wordcount": "^6.0.1"
}
}
GitHub Actions配置
不多說,先看yaml文件:
name: Auto Deploy
on:
watch:
types: [started]
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-18.04
if: github.event.repository.owner.id == github.event.sender.id
steps:
- name: Checkout source
uses: actions/checkout@v2
with:
ref: main
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: '12'
- name: Setup Hexo
run: |
git config --global user.email "deploy@fungit.org"
git config --global user.name "deploy"
npm install hexo-cli -g --no-audit
npm install --no-audit
- name: hexo clean and hexo g
run: |
hexo clean
hexo g
- name: Deploy to Server
uses: easingthemes/ssh-deploy@v2.1.5
env:
SSH_PRIVATE_KEY: ${{ secrets.GITHUB_TO_SERVER_KEY }}
ARGS: "-rltgoDzvO --delete"
SOURCE: "public/"
REMOTE_HOST: ${{ secrets.SERVER_IP }}
REMOTE_PORT: ${{ secrets.SERVER_PORT }}
REMOTE_USER: ${{ secrets.SERVER_USER }}
TARGET: ${{ secrets.REMOTE_TARGET }}
- name: Deploy gh-pages
env:
ACTION_DEPLOY_KEY: ${{ secrets.HEXO_DEPLOY_KEY }}
run: |
rm -rf ~/.ssh
mkdir -p ~/.ssh/
echo 'fungit.org' > public/CNAME
echo "$ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
hexo deploy
為了方便,使用了easingthemes/ssh-deploy@v2.1.5
推送工具,詳細可以去了解下。隨便看看peaceiris/actions-gh-pages@v3
還有最后我推送到gh-pages分支的寫法。
- SOURCE: 需要推送的目錄,hexo生成在public。
- SSH_PRIVATE_KEY: ssh私鑰、私鑰。前面配置的GITHUB_TO_SERVER_KEY。
- TARGET: 服務器目錄如/home/wwwroot/fungit.org
ps: 所需依賴最好放在package.json
里面,這樣Vervel部署也方便。 部署到ph-pages分支和連接vercel是為了以后服務器炸了、遷移方便切過去(懶,不想修)
Vercel:
GitHub Actions:
作者: Yeksha
連結: https://fungit.org/2021/github-acionts-automatically-compile-deploy/
來源: Fungit
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。