Hexo 搭建及部署相关笔记
  hmEzA3J2MUwz 2023年11月05日 31 0

搭建

前提:安装 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

部署

  1. GitHub 上新建仓库,名称格式为 username.github.io
  2. 修改博客目录下的 _config.yml 文件
deploy:
  type: git
  repo: git@github.com:username/username.github.io.git
  branch: master
  1. 安装 git 部署插件
npm install hexo-deployer-git --save
  1. 执行命令
hexo clean
hexo g
hexo d

Hexo 源文件备份

  1. github 上新建仓库
  2. 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 主题,其他主题可能会又出入。

  1. 新建一个自定义页面(以关于页面为例)
hexo new page about
  1. 编辑生成的 /source/about/index.md 文件,设置布局为 page
title: about
layout: page
  1. 在主题配置文件中添加链接
# ===========================================
# Menu Settings
# ===========================================
menu:
  Home: /
  Archives: /archives/
  About: /about
  1. 在使用的语言文件下添加对应的字段(默认为 languages/default.yml)
menu:
  home: Home
  archives: Archives
  tags: Tags
  categories: Categories
  about: About

使用 Github Action 自动部署

  1. 上传 Hexo 源文件至 Github
  2. 配置公钥和私钥(仓库目录/settings/sercrets)
  3. 添加 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 上更新博客自动发布了。

更换主题

  1. 下载主题源文件
git clone https://github.com/yinwenbing/hexo-theme-minima.git themes/minima
  1. 修改站点 _config.yml 文件
theme: minima

参考

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  DF5J4hb0hcmT   2023年12月12日   25   0   0 服务器git