许多人都希望自己有一个博客,但有时会觉得要搭个博客很繁琐,需要各种各样的环境,其中不知道还会出现什么错误信息,如果你曾经或者现在正有这样的想法,只要你跟着这篇文章动手起来,很快就能让你快速拥有自己的博客网站,记录生活的点滴。

环境准备

  • Git
  • NodeJs

配置github

  • 有一个github 账号
  • 建立一个与你用户名对应的仓库,eg: yourUserName.github.io (挂载GitHub Pages,用于网站浏览)
  • 建立一个在blog仓库用来存放源码
  • 配置ssh-key

安装hexo

  • 安装

    1
    # npm install -g hexo-cli
  • 初始化Hexo
    git clone blog仓库地址 <folder> 到本地

    1
    2
    3
    # hexo init <folder>
    # cd <folder>
    # npm install
  • 启动服务

    1
    2
    # hexo server
    [info] Hexo is running at http://localhost:4000/. Press Ctrl+C to stop.

    这样在本地就能看到你的blog了, 这样就能在本地调试你的blog了

本地调试

1
2
3
4
5
# hexo new "newPost" 新建文章
# vim newPost.md 编辑文章
# hexo new page "pageName" #新建页面
# hexo generate #生成静态页面至public目录
# hexo server #启动本地服务,进行文章预览调试

配置并发布

  • 新建一篇文章

    1
    # hexo new "My New Post"  //会在目录下生成 source\_posts\My-New-Post.md

    可以用 markdown 语法来编写你的文章

  • 配置_config.yml
    找到下面的内容

    1
    2
    3
    4
    5
    6
    ...

    # Deployment
    ## Docs: http://hexo.io/docs/deployment.html
    deploy:
    type:

    把它们修改为

    1
    2
    3
    4
    5
    6
    7
    8

    ...
    # Deployment
    ## Docs: http://hexo.io/docs/deployment.html
    deploy:
    type: git
    repository: git@github.com:yourUserName/yourUserName.github.io.git
    branch: master

    注意:
    repository: 必须是SSH形式的url
    eg: git@github.com:yourUserName/yourUserName.github.io.git
    而不能是HTTPS形式的url
    https://github.com/yourUserName/yourUserName.github.io.git,否则会出现错误
    如果你是为一个项目制作网站,那么需要把branch设置为gh-pages。

  • 发布

    1
    # hexo deploy

配置 GitHub Actions

将本地的博客源文件push到github仓库的时候自动触发发布

  • 准备秘钥

通过ssh-keygen 生成一对公钥密钥

在右上角个人资料中的 Settings->SSH and GPG keys 添加刚刚生成的公钥

在blog仓库的Settings->Secrets里添加刚刚生成的私钥,名称为 ACTION_DEPLOY_KEY

  • blog源码仓库的Actions选项卡下点击新建workflow,编写如下配置。

这里的blog 源码仓库是用来存储博客源文件,上面那个yourUserName.github.io用于挂载GitHub Pages

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on: [push]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest

name: A job to deploy blog.

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v1

# Runs a single command using the runners shell
- name: Use Node.js 10.x
uses: actions/setup-node@v1
with:
node-version: "10.x"

# Runs a set of commands using the runners shell
- name: Setup Hexo env
env:
ACTION_DEPLOY_KEY: ${{ secrets.ACTION_DEPLOY_KEY }}
run: |
# set up private key for deploy
mkdir -p ~/.ssh/
echo "$ACTION_DEPLOY_KEY" | tr -d '\r' > ~/.ssh/id_rsa # 配置秘钥
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
# set git infomation
git config --global user.name 'name' # 换成你自己的邮箱和名字
git config --global user.email 'xxxx@qq.com'
# install dependencies
npm i -g hexo-cli # 安装hexo
npm i
hexo generate

- name: Deploy
run: |
# publish
hexo deploy # 执行部署程序

添加自定义域名解释

  • 把域名用CNAME的方法解析到 yourUserName.github.io.git.

  • 在source文件夹里添加CNAME文件并添加你自己的域名

    1
    xxx.yyy.zzz

日常部署步骤

1
2
3
# hexo clean
# hexo generate
# hexo deploy

添加“Fork me on Github” ribbon

打开这个ribbon 把a 标签的代码粘贴到 themes\next\layout\layout.xxx 中,放置在 最后,标签之前即可,记得修改你的github
地址

常见错误

  • 执行 hexo deploy 后,出现 error deployer not found:github 的错误

    hexo 更新到3.0之后,deploy的type 的github需要改成git, 接着
    npm install hexo-deployer-git –save 改了之后执行,然后再部署