hugo travis github 自动化

hugo 是 go 编写的 blog生成器 比 ruby 的 jekyll 和 nodejs 的 hexo 生成速度快

好处

  • hugo安装方便,生成快
  • 集成travis,只需要下载raw分支,新增post,push上去就可以了,本机什么都不用做,甚至不用安装hugo

安装hugo

go开发,二进制,可直接下载对应版本的releases放到系统PATH 或包关联安装 brew,apt,yaourt

wget https://github.com/spf13/hugo/releases/download/v0.21/hugo_0.21_Linux-64bit.tar.gz
tar -xvf  hugo_0.21_Linux-64bit.tar.gz
sudo cp hugo /usr/local/bin

生成blog

git管理, 挑选theme设置gitsubmodule, 根据theme编辑 config.toml, 否则可能报错

hugo new site blog
cd blog
git init
git check -b raw
git submodule add https://github.com/gyorb/hugo-dusk.git themes/hugo-dusk
echo 'public/' > .gitignore
touch Makefile #否则会报 no buildable Go source files
git add .
git commit -m 'init blog raw'
git checkout --orphan master
git rm -rf .
git commit -m 'init deploy branch' --allow-empty

hugo-duskconfig.toml example

baseurl = "/"
title = "My site."
copyright = "Copyright (c) 2017, all rights reserved."
canonifyurls = true
languageCode = "en-US"
paginate = 3
theme = "hugo-dusk"

googleAnalytics = ""
disqusShortname = ""

[author]
  name = ""

SectionPagesMenu = "main"

[[menu.main]]
  name = "Posts"
  weight = -120
  identifier = "post"
  url = "/post/"

[[menu.main]]
  name = "Tags"
  weight = -110
  identifier = "tag"
  url = "/tags/"

[params.meta]
  keywords = "blog, tech"
  description = "Personal blog."

[params]
  github = "github id"
  gitlab = "gitlab id"
  twitter = "twitter id"
  linkedin = "linkedin id"
  email = "myemail"

生成 浏览 post

hugo new post/test.md
hugo
hugo server -w

关联github 和 travis

  • 关联远程github
  • 生成ssh key
  • 填加github io项目的 deploy keytravis 发布用 ,添加 travis_key 到 gitignore
  • 登录 travis 加密 ssh私钥
gem isntall travis # 安装 *travis* , arch安装 yaourt -S ruby-travis
git checkout raw
git remote add origin  https://github.com/****.io.git
ssh-kengen -t 'rsa' -C 'travis' # 输入travis_key
echo 'travis_key' >> .gitignore
echo 'travis_key.pub' >> .gitignore
travis login --auto
travis whoami
touch .travis.yml
travis encrypt-file travis_key --add #  --add 自动添加 解密信息到 .travis.yaml
cat travis_key.pub | pbcopy # 添加公钥 到 github deploy key,勾选 Allow write access。  linux cat travis_key.pub | xclip -selection cllipboard

如果报错 repository not known to https://api.travis-ci.org/: 进入 travis-ci.org sync account

修改 .travis.yml 填写正确信息 例如

language: go
branches:
  only:
    - raw
env:
  global:
    - SSH_KEY="travis_key"
    - GIT_NAME="xxx"
    - GIT_EMAIL="xxx@gmail.com"
    - SOURCE_DIR="public"
    - DEPLOY_BRANCH="raw"
    - TEMP_DIR=$(sudo mktemp -d /tmp/$REPO_NAME.XXXX)
before_install:
- export TZ='Asia/Shanghai'  # 设置时区
- openssl aes-256-cbc -K $encrypted_xxxx_key -iv $encrypted_xxxxx_iv
  -in travis_key.enc -out travis_key -d
before_script:
  - go get -u -v github.com/spf13/hugo
script:
  - git submodule init
  - hugo
after_success:
  - chmod a+x ./scripts/deploy.sh
  - ./scripts/deploy.sh

下载发布脚本deploy.sh

mkdir scripts && cd scripts
wget https://raw.githubusercontent.com/johnsonwjx/johnsonwjx.github.io/raw/scripts/deploy.sh
git checkout master
git push -u origin master
git checkout raw
git add .
git commit -m 'add deploy resources'
git push -u origin master

https://travis-ci.org/ 启用 io工程 设置 勾选 Build only if .travis.yml is present

success !!

comments powered by Disqus