如果不想使用nvm进行node多版本的管理,直接可以使用去node中文网 下载LTS版本(安装的时候记得添加到环境变量选项要勾上),安装后从第四步开始执行
第一步:安装nvm
这一步mac和windows有所区别
- 执行
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
或wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
- 注意:注意后面的
v0.33.8
这是nvm的版本号,最新版本查看
- 注意:注意后面的
- 参考Windows下安装及使用nvm在windows下安装nvm
- 安装完成后关闭终端,然后键入
nvm
看一下是否有输出,如果command not found
请查看 nvm ls
查看所有已安装node的版本,并且使用node use <version>
命令选择一个合适的node用来安装hexo
补充:nvm常用命令介绍
1 | ● nvm install stable 安装最新稳定版 node |
第二步:卸载原来的hexo
- 删除原来的hexo:
npm uninstall -g hexo-cli
第三步:安装node
由于hexo必须要用npm安装,hexo安装的版本取决于node的版本
- 使用nvm选择我们的node版本,如果对应版本没有安装,首先使用nvm安装对应版本的node。
nvm install <version>
- 切换到对应的node版本:
npm use <version>
第四步:安装hexo
- 安装hexo:
npm install -g hexo-cli
第五步:同步并发布自己的博客(CI + CD)
这里假设我们的博客文章已经编写完成
这一部分涉及到两部分内容:
- (备份)同步博客源内容到github:避免之前编写的博客内容丢失
- (发布)将我们编写的博客发布到github:可以以网页的形式看到我们编写之后的内容
具体步骤
- 在我们的博客根目录下新建
.github
目录,然后进入.github
目录新建workflows
目录,进入之后新建deploy.yml
文件 - 键入如下内容:
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
28name: Build and Deploy
on:
push:
branches:
- source
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/checkout@v2 # If you're using actions/checkout@v2 you must set persist-credentials to false in most cases for the deployment to work correctly.
with:
persist-credentials: false
- name: Install and Build 🔧 # This example project is built using npm and outputs the result to the 'build' folder. Replace with the commands required to build your project, or remove this step entirely if your site is pre-built.
run: |
npm install
npm run build
env:
CI: false
- name: Deploy 🚀
uses: JamesIves/github-pages-deploy-action@releases/v3
with:
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
BRANCH: main # The branch the action should deploy to.
FOLDER: public # The folder the action should deploy. - 配置ACCESS_TOKEN
- 进入github token页面
- 点击
generate new token
全选之后将将生成的token赋值 - 进入自己的
用户名.github.io
仓库,然后点击settings
->secrets
->New repository secret
->名字是ACCESS_TOKEN
,值是刚才生成的token
- 使用git的
add
、commit
、push
命令提交
注意事项:
- 博客的源代码存放到
用户名.github.io
这个仓库下的source分支下 - 博客的发布流程我们在上面已经配置到了github的action中,github会在我们push到source分支的时候直接拉取代码然后发布到main分支,之后我们就可以通过
用户名.github.io
这个域名进行访问了
之后每次提交记得在source分支下操作
后续
博客搭建好之后,只需要选一个合适的主题,就可以书写自己的博客了