我在使用jenkins實(shí)現(xiàn)hexo博客自動(dòng)發(fā)布中介紹了可以使用jenkins實(shí)現(xiàn)博客自動(dòng)發(fā)布,其實(shí)還有一種更簡(jiǎn)單的方法,就是GitHub提供的持續(xù)集成服務(wù):GitHub Actions。下面就來(lái)介紹一下如何使用GitHub Actions來(lái)實(shí)現(xiàn)將hexo博客自動(dòng)編譯及發(fā)布到GitHub Pages和Gitee pages上。
GitHub Actions把代碼拉取,打包,觸發(fā)測(cè)試,發(fā)布等操作當(dāng)成一個(gè)個(gè)的actions,持續(xù)集成就是將這些actions組合起來(lái),jenkins也類似。
在項(xiàng)目的.github/workflows
目錄中創(chuàng)建一個(gè)yaml格式的workflow 文件來(lái)編寫(xiě)GitHub Actions 工作流程,GitHub會(huì)自動(dòng)運(yùn)行該文件。workflow語(yǔ)法可參考:https://docs.github.com/cn/actions/learn-github-actions/workflow-syntax-for-github-actions
開(kāi)發(fā)者可以將actions放到代碼倉(cāng)庫(kù)供其他開(kāi)發(fā)者引用,可以到官網(wǎng)搜索開(kāi)源的actions:https://github.com/marketplace?type=actions
可以使用GitHub Actions實(shí)現(xiàn)博客自動(dòng)發(fā)布,將靜態(tài)博客頁(yè)面部署到多個(gè)服務(wù)器上,比如GitHub Pages,Gitee pages以及云服務(wù)器上。本文使用GitHub Actions實(shí)現(xiàn)將Hexo博客自動(dòng)編譯并發(fā)布到GitHub Pages 和 Gitee pages上,并且實(shí)現(xiàn)Gitee pages自動(dòng)更新。
下面介紹具體的配置流程。
我的個(gè)人博客是使用 hexo 框架搭建的,搭建方法可參考Hexo+Github/Gitee 搭建個(gè)人博客。
主要包括3個(gè)倉(cāng)庫(kù):
使用GitHub Actions要實(shí)現(xiàn)的是當(dāng)Blog倉(cāng)庫(kù)指定目錄有更新時(shí),觸發(fā)自動(dòng)編譯并部署博客到hiyongz.github.io倉(cāng)庫(kù)中,并將hiyongz.github.io倉(cāng)庫(kù)同步到Gitee公共倉(cāng)庫(kù),同步完成后,更新Gitee Pages。
配置發(fā)布秘鑰,用于將生成的靜態(tài)博客文件 push 至 GitHub Pages 所在的倉(cāng)庫(kù)hiyongz.github.io:
1、生成秘鑰
執(zhí)行如下命令生成公鑰和私鑰,替換郵件地址為你的github郵箱地址,其實(shí)也可以不使用郵箱,這里只是為了便于辨識(shí)。
ssh-keygen -f hexo-deploy-key -t rsa -C "username@example.com"
命令執(zhí)行后會(huì)生成兩個(gè)文件:hexo-deploy-key
和 hexo-deploy-key.pub
。
2、將公鑰添加到Github Pages倉(cāng)庫(kù)中
步驟:hiyongz.github.io倉(cāng)庫(kù) -> Settings -> Deploy keys -> Add deploy key
HEXO_DEPLOY_PUB
(可以根據(jù)自己喜好設(shè)置)github-deploy-key.pub
文件內(nèi)容Allow write access
選項(xiàng)3、將私鑰添加到博客源碼倉(cāng)庫(kù)中
步驟:博客倉(cāng)庫(kù) -> Settings -> Secrets -> New repository secret
HEXO_DEPLOY_KEY
。github-deploy-key
文件內(nèi)容。Gitee Token配置和Github類似。
秘鑰不需要重新生成,直接使用前面生成的秘鑰。
1、將公鑰添加到Gitee Pages倉(cāng)庫(kù)中
步驟:gitee pages倉(cāng)庫(kù) -> 管理 -> 公鑰管理 -> 添加部署公鑰
和github一樣需要對(duì)倉(cāng)庫(kù)有寫(xiě)權(quán)限,點(diǎn)擊【添加個(gè)人公鑰】
復(fù)制粘貼文件 github-deploy-key.pub
中的內(nèi)容
2、將私鑰添加到Github的博客源碼倉(cāng)庫(kù)中
不需要重新創(chuàng)建,直接使用 HEXO_DEPLOY_KEY
就行。
3、配置Gitee 賬號(hào)的密碼
步驟:Github博客倉(cāng)庫(kù) -> Settings -> Secrets -> New repository secret
GITEE_PASSWORD
。下面來(lái)配置 Github Actions,編寫(xiě)workflow文件。
在博客倉(cāng)庫(kù)根目錄下創(chuàng)建 .github/workflows/deploy.yml
文件,yaml文件名可以隨意設(shè)置。
下面是我的workflow文件(參考了Hexo Action和Gitee Pages Action提供的例子):
name: Hexo deploy
on:
push:
paths-ignore:
- 'source/_drafts/**'
- '.github/**'
jobs:
build:
runs-on: ubuntu-latest
name: A job to deploy blog.
steps:
- name: Checkout
uses: actions/checkout@v1
with:
submodules: true
- name: Cache node modules
uses: actions/cache@v1
id: cache
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Install Dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: npm ci
# Deploy hexo blog website.
- name: Deploy
id: deploy
uses: sma11black/hexo-action@v1.0.3
with:
deploy_key: ${{ secrets.HEXO_DEPLOY_KEY }}
user_name: github用戶名
user_email: github郵箱
commit_msg: ${{ github.event.head_commit.message }}ion)
- name: Get the output
run: |
echo "${{ steps.deploy.outputs.notify }}"
sync:
needs: build
runs-on: ubuntu-latest
steps:
- name: Sync to Gitee
uses: wearerequired/git-mirror-action@master
env:
SSH_PRIVATE_KEY: ${{ secrets.HEXO_DEPLOY_KEY }}
with:
# 源倉(cāng)庫(kù)
source-repo: git@github.com:hiyongz/hiyongz.github.io.git
# 目標(biāo)倉(cāng)庫(kù)
destination-repo: git@gitee.com:hiyong/hiyong.git
reload-pages:
needs: sync
runs-on: ubuntu-latest
steps:
- name: Build Gitee Pages
uses: yanglbme/gitee-pages-action@main
with:
gitee-username: hiyong
gitee-password: ${{ secrets.GITEE_PASSWORD }}
gitee-repo: hiyong/hiyong
branch: master
部分字段解釋:
1、name:workflow 名稱
2、on:觸發(fā) workflow 的事件
push
:push事件paths-ignore
:忽略指定的目錄,也就是在忽略路徑外的其它目錄文件改動(dòng)時(shí)才觸發(fā)。3、jobs:執(zhí)行任務(wù)
build
:博客編譯和發(fā)布,發(fā)布到Github Pagessync
:將更新后的hiyongz.github.io倉(cāng)庫(kù)同步到Giteereload-pages
:自動(dòng)更新 Pages,因?yàn)镚itee Pages不像GitHub Pages 那樣提交代碼就自動(dòng)更新。runs-on
:運(yùn)行環(huán)境,支持windows,Ubuntu和macOSsteps
:指定每個(gè) Job 的運(yùn)行步驟sma11black/hexo-action@v1.0.3
:博客構(gòu)建發(fā)布,引用了Hexo Action:wearerequired/git-mirror-action@master
:倉(cāng)庫(kù)同步,引用了git-mirror-actionyanglbme/gitee-pages-action@main
:自動(dòng)更新Gitee Pages,引用了Gitee Pages Action更多workflow語(yǔ)法可參考 Workflow syntax for GitHub Actions 。
更新文章后push到博客倉(cāng)庫(kù),如果滿足條件,博客倉(cāng)庫(kù)中的Actions會(huì)自動(dòng)觸發(fā):
有可能報(bào)如下錯(cuò)誤信息:
Error: Need phone captcha validation, please follow wechat official account "Gitee" to bind account to turn off authentication.
微信公眾號(hào)關(guān)注Gitee,綁定Gitee賬號(hào)就可以了。
查看hiyongz.github.io和Gitee倉(cāng)庫(kù)可以發(fā)現(xiàn)有新的更新,并且博客也更新了,這樣實(shí)現(xiàn)了博客的自動(dòng)發(fā)布,只要將文章寫(xiě)好push到博客倉(cāng)庫(kù)就什么也不用管了。
如果Actions執(zhí)行失敗會(huì)收到一個(gè)郵件。
除了GitHub Actions外,還可以使用Travis CI實(shí)現(xiàn)持續(xù)集成,它可以綁定Github 上的項(xiàng)目。
使用這些持續(xù)集成工具能有效提升效率,比如每次文章更新上傳后,GitHub Actions會(huì)自動(dòng)觸發(fā)編譯發(fā)布操作,只專注寫(xiě)文章就行了。
參考文檔:
GitHub Actions 快速入門:https://docs.github.com/cn/actions/quickstart
GitHub Actions:https://github.com/actions
Gitee Pages Action:https://github.com/marketplace/actions/gitee-pages-action
git-mirror-action:https://github.com/marketplace/actions/git-mirror-action
Hexo Action:https://github.com/marketplace/actions/hexo-action
awesome-actions:awesome-actions
workflow語(yǔ)法: https://docs.github.com/cn/actions/learn-github-actions/workflow-syntax-for-github-actions
聯(lián)系客服