此文详细介绍如何部署VuePress[1]到GitHub[2]的gh-pages上
在 GitHub 新建仓库 note
在GitHub
中新建仓库名称为note
![d36b3a0b8625301007c8c39bc0ce770c.png](https://i-blog.csdnimg.cn/blog_migrate/5642da4b3067dbc30ea033a9fc502434.png)
全局安装 VuePress
# 安装
yarn global add vuepress # 或者:npm install -g vuepress
# 克隆项目到本地并新建和仓库同名的文件夹 note
git clone git@github.com:thejtf/note.git note
# 进入本地 note 文件夹
cd note
# 新建 docs 文件夹
mkdir docs
# 新建一个 markdown 文件
echo '# Hello VuePress!' > README.md
# 创建并初始化 package.json
yarn init -y
复制下方代码到 package.json
"scripts": {
"docs:dev": "vuepress dev docs",
"docs:build": "vuepress build docs"
}
如下图所示?
![da66e784ae10705faab25aea9c8f15a5.png](https://i-blog.csdnimg.cn/blog_migrate/0b7b4c34fa72b701774de3aec5fc92b0.png)
本地启动 VuePress
# 运行以下命令
yarn docs:dev
打开浏览器访问 http://localhost:8080/ 效果如下图?
![d6e54d65a20c639163708c59982e7fe2.png](https://i-blog.csdnimg.cn/blog_migrate/d1b23162e2c7499beb0e8c587ffa3bb2.png)
配置 VuePress
在docs
文件夹下新建.vuepress
文件夹,在.vuepress
下新建config.js
文件
# 复制下方代码到 config.js
module.exports = {
title: '学习笔记',
description: '知识点记录'
}
效果如下图?
![0ad4a15d48c1634a01cd1ba7f9988625.png](https://i-blog.csdnimg.cn/blog_migrate/40f906f534d16c91c13f950d66368560.png)
默认主题配置
复制下方代码到 docs
的 README.md
---
home: true
heroImage: /hero.png
heroText: Hero 标题
tagline: Hero 副标题
actionText: 快速上手 →
actionLink: /zh/guide/
features:
- title: 简洁至上
details: 以 Markdown 为中心的项目结构,以最少的配置帮助你专注于写作。
- title: Vue驱动
details: 享受 Vue + webpack 的开发体验,在 Markdown 中使用 Vue 组件,同时可以使用 Vue 来开发自定义主题。
- title: 高性能
details: VuePress 为每个页面预渲染生成静态的 HTML,同时在页面被加载的时候,将作为 SPA 运行。
footer: MIT Licensed | Copyright © 2018-present Evan You
---
效果如下图?
![705c2d818273caa3a2bf4d989b18fe94.png](https://i-blog.csdnimg.cn/blog_migrate/1286e10954e2e33c1409b90295d1efe1.png)
在.vuepress
文件夹下新建一个public
文件夹,再在public
文件夹下新建一个img
文件夹,把图片文件放到此文件夹下?
![40accc46e083a42b9056a10e79fed06b.png](https://i-blog.csdnimg.cn/blog_migrate/5c1879195a933cba109a359eed7b502b.png)
修改首页图片
在docs
文件夹下的README.md
修改heroImage
home: true
# 修改此下方路径添加图片
heroImage: /img/notebook.png
heroText: Hero 标题
tagline: Hero 副标题
actionText: 快速上手 →
actionLink: /zh/guide/
features:
- title: 简洁至上
details: 以 Markdown 为中心的项目结构,以最少的配置帮助你专注于写作。
- title: Vue驱动
details: 享受 Vue + webpack 的开发体验,在 Markdown 中使用 Vue 组件,同时可以使用 Vue 来开发自定义主题。
- title: 高性能
details: VuePress 为每个页面预渲染生成静态的 HTML,同时在页面被加载的时候,将作为 SPA 运行。
footer: MIT Licensed | Copyright © 2018-present Evan You
修改首页导航 logo
在.vuepress
文件夹下修改config.js
文件
module.exports = {
title: '学习笔记',
description: '知识点记录',
themeConfig: {
logo: '/img/novel.png',
}
}
添加网页的 ico 文件
在.vuepress
文件下修改config.js
文件
head: [
['link', { rel: 'icon', href: '/logo.png' }]
]
添加导航栏链接
在.vuepress
文件夹下修改config.js
文件
module.exports = {
title: '学习笔记',
description: '知识点记录',
head: [
['link', { rel: 'icon', href: '/img/favicon.ico' }]
],
themeConfig: {
logo: '/img/novel.png',
// 在下方修改添加
nav: [
{ text: 'Home', link: '/' },
{ text: 'Guide', link: '/guide/' },
{ text: 'External', link: 'https://google.com' },
]
// 在上方修改添加
}
}
部署到 GitHub 仓库中
新建 gh-pages 分支
在GitHub
上新建一个gh-pages
的分支,并在Settings
中在Branches
更改Default branch
为gh-pages
,如下图?
![94183bbeb0bbac002ae3bbede3f47631.png](https://i-blog.csdnimg.cn/blog_migrate/a37ae4b890fb29d18016a063f7b5adc9.png)
更改Settings
中的Options
的GitHub Pages
的 Source 为gh-pages branch
如下图?
![902b3f52a76d09eb36ab176a84d42da4.png](https://i-blog.csdnimg.cn/blog_migrate/d4e24a6aa0a856288acc8c8321b98033.jpeg)
Base URL
本文要把vuepress部署到github page上,部署在https://{your-github-name}.github.io/{repo-name}/
地址上,需要在config.js
中定义base
的值为/{repo-name}/
。
//config.js
module.exports = {
base: '/note/',
title: '学习笔记',
description: '知识点记录',
head: [
['link', { rel: 'icon', href: '/img/favicon.ico' }]
],
themeConfig: {
logo: '/img/novel.png',
nav: [
{ text: 'Home', link: '/' },
{ text: 'Guide', link: '/guide/' },
{ text: 'External', link: 'https://google.com' },
]
}
}
新建 .gitginore
在本地note
根目录下创建忽略文件.gitginore
,填入如下内容。第6行可以根据情况修改,不需要把生成的静态文件提交到github上:
.DS_Store
Thumbs.db
db.json
*.log
node_modules/
sources/.vuepress/dist/
*.bat
*.sh
如下图?
![17a7497a49b99fd94f1e1627bcb8071c.png](https://i-blog.csdnimg.cn/blog_migrate/54fa21439b6b3d1d67d67705fd58a6d9.png)
新建 deploy.sh
在本地note
根目录创建deploy.sh
,填入以下内容:
#!/usr/bin/env sh# 发生错误时停止
set -e
git init
git add -A
git commit -m 'init'
git config --local user.name "{name}"
git config --local user.email {email}
# 如果部署到 https://{USERNAME}.github.io
# git push -f git@github.com:{USERNAME}/{USERNAME}.github.io.git master:{branch-name}
# 如果部署到 https://{USERNAME}.github.io/{REPO}
git push -f git@github.com:{USERNAME}/{REPO}.git master
cd -
提交部署
运行命令. deploy.sh
,把源码提交到github上。
. deploy.sh
如下图?
![967dc023755c59f040b7bfc22bf43d5f.png](https://i-blog.csdnimg.cn/blog_migrate/a09e936542b026edb84ece40f63c2b8c.png)
部署上线
在浏览器打开 jtf.im/note/[3] 即可访问?
![6a8863499003cd0e296ae7ec856b37b7.png](https://i-blog.csdnimg.cn/blog_migrate/57aa8c4edbcab6c90996c7c31dcc41c8.png)
大功告成?。
参考资料
[1]VuePress: https://vuepress.vuejs.org/
[2]GitHub: https://github.com/
[3]jtf.im/note/: https://jtf.im/note/
博 客 地 址
https://jtf.im
?扫我直接访问
? https://jtf.im
长按扫描即可打开?
▽ 点击「」,体验一下。