之后就可以直接在私人倉庫source/_posts/里面添加.md文件啦,可以隨時隨地寫文章發文章
文章來源:
不知所措的新哥
https://xin520.xyz
https://xin520.site
https://xin520.plus
https://anxinweb.github.io
https://xin-lac.vercel.app
參考借鑒來源:
GitHub Actions 來自動部署 Hexo:https://zhuanlan.zhihu.com/p/170563000
github將整個文件夾推送到自己的倉庫:https://blog.csdn.net/viafcccy/article/details/85527118
一、配置github actions部分
HEXO正常運作
前提
node.js 環境 和 git 都已正確安裝 (其實能正常運行 hexo 就已經說明正確安裝了)
hexo 可以正常運行 可以正常部署(這里介紹部署到 github pages )
-
配置好hexo的主題,博客名稱等等
ok,有了以上前提,可以繼續了
改 _config.yml 配置
_config.yml 文件中在前提情況下,已經配置好了:
deploy:
type: git
repo: https://github.com/用戶名/倉庫名.git
branch: master
此時我們需要將上面 repo 的配置改成 ssh 格式——即 git@github.com:用戶名/倉庫名.git
避免在執行 actions 時 部署出錯
再次生成密鑰
隨便在任何文件位置可以直接右鍵 git bash here
復制粘貼這個 ssh-keygen -t rsa -b 4096 -C "Hexo Deploy Key" -f github-deploy-key -N ""
會在當前目錄生成兩個文件
- github-deploy-key —— 私鑰
- github-deploy-key.pub —— 公鑰
私鑰直接存放在 hexo 原始文件(hexo源文件)的倉庫代碼里
- 自行新建一個私人倉庫來存放hexo源文件
- 然后訪問私人代碼倉庫
Settings -> Secrets
,New secret
- Name 填寫
EXO_DEPLOY_KEY
注意大小寫,這個后面的 GitHub Actions Workflow 要用到,一定不能寫錯。 - 在 Value 填入 github-deploy-key(私鑰) 中的內容
公鑰放到 GitHub Pages 對應的代碼倉庫里面
- 訪問 github pages 對應的代碼倉庫
Settings -> Deploy keys
,Add deploy key
- Title:HEXO_DEPLOY_PUB 可自定義名字
- 在 Key 填入 github-deploy-key.pub(公鑰)中的內容
- Allow write access 一定要勾上
創建 workflow
在私人代碼倉庫里點 Actions
然后創建一個新文件 .github/workflows/deploy.yml
deploy 名字可以自取但是一定要放在.github/workflows
目錄中
- deploy.yml 內容如下:
name: Hexo Deploy
on:
push:
branches:
- master
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: master
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: '12'
- name: Setup Hexo
env:
ACTION_DEPLOY_KEY: ${{ secrets.HEXO_DEPLOY_KEY }}
run: |
mkdir -p ~/.ssh/
echo "$ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.email "改成你的郵箱"
git config --global user.name "改成你的用戶名"
npm install hexo-cli -g
npm install
- name: Deploy
run: |
hexo clean
hexo deploy
ok,這樣就完美搞定 github action 和 GitHub pages 的連接啦,并且可以自動觸發 Workflow 執行動作
二、推送部署 hexo 博客源文件到私人倉庫
俺是一個純小白,只能傻瓜式的推送部署到倉庫了
在任意位置 git bash here 然后 輸入
git clone https://github.com/用戶名/倉庫名.git
將私人倉庫給克隆下來將所有的hexo文件都復制到剛剛克隆下來的文件夾里面
然后
git init
將該克隆下的文件夾變成Git可以管理的倉庫git add .
通過git add將所有文件提交到暫存區git commit -m 'the initial edition'
版本描述git remote add origin https://github.com/用戶名/倉庫名.git
與倉庫關聯git pull
第一次推送需要git push -u origin master
帶有-u這個參數是指,將master分支的所有內容都提交,第一次關聯之后后邊你再提交就可以不用這個參數了-
git push origin master
之后你的每一次修改,你就可以只將你修改用這個push就好了此時,所有的hexo文件全都git到倉庫了,之后就可以直接在私人倉庫source/_posts/里面添加.md文件啦,可以隨時隨地寫文章發文章,不受設備和配置環境干擾啦。
俺是一個純小白,都是一步一步按照別人的步驟踩坑摸索來的,不容易嗚嗚嗚。