以前也折腾过我的博客,从大学时候玩的 Wordpress、Ghost,最近又开始折腾博客。因为我我的是比较喜欢经过Github来作博客系统的,对比了目前市面上比较主流的博客系统,好比Hexo、Hugo, 顺便推荐一下国内比较活跃的 Java 开源博客系统 Halo,更多可移步:https://github.com/halo-dev/halogit
这一次我选择了 Hugo,一方面是为了下降维护成本,最实际的则是下降服务器成本。至于对Github pages速度太慢的问题我将采用CDN加速解决。github
本地添加文章,提交到Github,以后会自动触发Github Actions帮助咱们把刚刚添加的文章经过Hugo发布到Github Pages进行托管。以后便可经过 Github 给 Pages 生成的 URL 访问便可。我将Hugo源码和Pages分别用两个仓库来管理,Github Actions会将Hugo仓库(JaredTan95.github.io.source)源码编译成静态资源推到Pages仓库(JaredTan95.github.io)ubuntu
JaredTan95.github.io.source
仓库:我将源码仓库设置为了 Private,看我的需求。segmentfault
JaredTan95.github.io
仓库:由于当咱们在经过Git提交源码以后,Github Actions会编译生成静态文件并经过Git Push到 JaredTan95.github.io
,所以这一步须要 Git 帐户认证。服务器
咱们先生成一对SSH Key,生成的Public Key和Private Key都会用到,我采用以下方式生成:ssh
注意:上图红框标注的这里覆盖了默认的生成路径,这样就不会影响到电脑中旧的SSH Key。ui
这一步比较重要,咱们要将生成的 Public Key 添加到 JaredTan95.github.io
仓库:spa
而后将 Private Key 添加到 JaredTan95.github.io.source
仓库:
这里 Secrets 变量名要必定是: ACTIONS_DEPLOY_KEY, 后面会用到。3d
JaredTan95.github.io.source
仓库克隆到本地,开始初始化 Hugo 系统:# 选取一个目录 cd ~/Desktop/ # 克隆 source 仓库 git clone git@github.com:JaredTan95/JaredTan95.github.io.source.git # 进入仓库 cd JaredTan95.github.io.source/
生成 Hugo 源码并进行配置:日志
# 在当前目录生成 Hugo 源码 hugo new site . # 为当前博客选取一个主题,你能够不执行这一命令使用默认的主题 git submodule add https://github.com/halogenica/beautifulhugo.git themes/beautifulhugo # 编辑 config.toml 配置文件,使 beautifulhugo 主题生效 echo 'theme = "beautifulhugo"' >> config.tomlecho 'theme = "beautifulhugo"' >> config.tom # 此时你就能够运行预览效果 hugo serve
若是你以为满意没问题以后,便可推送到 Github
git add . git commit -m "first commit" git push -u origin master
能够经过 Github 自动为咱们仓库生成,注意是为 JaredTan95.github.io.source
仓库配置 Actions。
直接将如下文件贴进去,修改远程仓库便可,其余的基本上不用更新:
name: Deploy Hugo Site to Github Pages on Master Branch on: push: branches: - master jobs: build-deploy: runs-on: ubuntu-18.04 steps: - uses: actions/checkout@v1 # v2 does not have submodules option now # with: # submodules: true - name: Setup Hugo uses: peaceiris/actions-hugo@v2 with: hugo-version: '0.62.2' # extended: true - name: Build run: hugo --minify - name: Deploy uses: peaceiris/actions-gh-pages@v3 with: deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }} # 这里的 ACTIONS_DEPLOY_KEY 则是上面设置 Private Key的变量名 external_repository: JaredTan95/JaredTan95.github.io # Pages 远程仓库 publish_dir: "" keep_files: false # remove existing files publish_branch: master # deploying branch commit_message: ${{ github.event.head_commit.message }}
修改好以后,点击右上角 commit 提交便可。咱们能够手动触发打包动做也能够在本地从新commit一次,检查流水线是否会被触发。
触发以后能够查看流水线日志:
自此,整个搭建就结束了,咱们能够访问Github为JaredTan95/JaredTan95.github.io
仓库生成的域名: https://jaredtan95.github.io/
查看效果。
本次搭建耗费了很多时间,主要在于Github访问较慢,克隆Hugo主题也比较慢。目前惟一的不足则是Github Pages访问较慢,后面经过自定义域名+CDN加快访问速度。
文章首发在公众号:抠腚Coding笔记, 欢迎关注!