搭建
前提:安装 Node.js(v12.14.1)、Git,配置 SSH Key
// 1. 安装 hexo
npm install -g hexo-cli
// 2. 生成 hexo 博客
hexo init Blog // Blog 是本地文件夹名称,可自定义
// 3. 进入博客目录
cd Blog
// 4. 创建文章
hexo n "title"
// 5. 编译
hexo g
// 6. 启动预览
hexo s
部署
- GitHub 上新建仓库,名称格式为
username.github.io
- 修改博客目录下的 _config.yml 文件
deploy:
type: git
repo: git@github.com:username/username.github.io.git
branch: master
- 安装 git 部署插件
npm install hexo-deployer-git --save
- 执行命令
hexo clean
hexo g
hexo d
Hexo 源文件备份
- github 上新建仓库
- Hexo 源文件关联 Github 远程仓库
// 初始化本地仓库
git init
// 关联远程仓库
git remote add origin git@github.com:username/Blog.git
// 同步远程仓库
git pull origin master
// 本地变动添加至缓存区
git add .
// 提交
git commit -m "backup: 2023-01-11 16:59"
// 推送至远程仓库
git push origin master
添加自定义页面
基于 hexo-polarBear 主题,其他主题可能会又出入。
- 新建一个自定义页面(以关于页面为例)
hexo new page about
- 编辑生成的 /source/about/index.md 文件,设置布局为 page
title: about
layout: page
- 在主题配置文件中添加链接
# ===========================================
# Menu Settings
# ===========================================
menu:
Home: /
Archives: /archives/
About: /about
- 在使用的语言文件下添加对应的字段(默认为 languages/default.yml)
menu:
home: Home
archives: Archives
tags: Tags
categories: Categories
about: About
使用 Github Action 自动部署
- 上传 Hexo 源文件至 Github
- 配置公钥和私钥(仓库目录/settings/sercrets)
- 添加 github action,参考下方脚本(中文位置需替换)
# workflow name
name: Hexo Blog CI
# master branch on push, auto run
on:
push:
branches:
- <当前分支>
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 '<你登录的email>'
- name: Deploy Hexo
run: |
hexo clean
hexo generate
hexo deploy
完成上述操作后,即可实现 github 上更新博客自动发布了。
更换主题
- 下载主题源文件
git clone https://github.com/yinwenbing/hexo-theme-minima.git themes/minima
- 修改站点
_config.yml
文件
theme: minima
参考