【原文鏈接】:https://www.tecchen.xyz/blog-hexo-env-03.html
我的個人博客:https://www.tecchen.xyz,博文同步發佈到博客園。
由於精力有限,對文章的更新可能不能及時同步,請點擊上面的原文鏈接訪問最新內容。
相關鏈接
- GitLab Pages:https://about.gitlab.com/product/pages/
前言
這裏不多說Git作爲版本控制系統的優勢,Gitlab作爲支持私有倉庫的開源Git系統,另外開放了Pages的功能,可以實現個人靜態博客的部署及訪問。
本文僅將Hexo打包後的代碼提交到Gitlab,並利用CI/CD發佈到Gitlab Pages,實現可以通過 https://username.gitlab.io/projectname 或者 https://username.gitlab.io可以訪問博客的目的。
創建倉庫
Gitlab支持project page和user page兩種pages,只需要創建對應的倉庫即可。
如果想通過https://username.gitlab.io/projectname形式訪問,需要創建projectname的倉庫。
如果想通過https://username.gitlab.io,需要創建username.gitlab.io的倉庫
代碼發佈
修改站點配置文件,找到deploy配置,將type設置爲git,並填寫自己的repo地址,分支默認爲master即可。
deploy:
type: git
repo: [email protected]:java4candy/java4candy.gitlab.io.git
branch: master
執行hexo clean && hexo d -g,即可將發佈後的代碼提交到Gitlab。
Gitlab 配置CI/CD
新建文件方式:
在Gitlab中,根據.gitlab-ci.yml模板New file並上傳。
上傳文件方式:
選擇Upload file,將本地的.gitlab-ci.yml文件上傳到Gitlab。
文件內容:
# This file is a template, and might need editing before it works on your project.
# Full project: https://gitlab.com/pages/plain-html
pages:
stage: deploy
script:
- mkdir .public
- cp -r * .public
- mv .public public
artifacts:
paths:
- public
only:
- master
上傳後,Gitlab會自動校驗.gitlab-ci.yml文件語法,校驗通過後,自動執行。
另外,在左邊菜單CI/CD->Pipelines中能夠查看Pipeline、對應的Jobs及腳本的執行情況。
如果需要定時發佈,可以使用Schedules創建Cron定時器。
最後在在Settings->Pages查看正確的的訪問路徑。
通過https://username.gitlab.io/projectname 或者 https://username.gitlab.io訪問博客。