hugo and travis automation deploy

Jan 3, 2019 00:00 · 788 words · 2 minute read hugo travis

0x00 准备工作

repo:blog-hugo
github-page:ifishzzz.github.io

0x01 travis设置

https://github.com/settings/tokens 先在github生成一个token,勾选第一个回购,注意token只能看一次

进入travis勾选blog-hugo这个repo,并且点击设置

拉到下面填写变量名与token值,比如变量是github_token,把github_token值输入进去,add

0x02 travis脚本设置

在code目录创建.travis.yml

install:
    - uname -a #没用
    #可以有多种安装hugo的方式,可以参考下别人的脚本
    - wget https://github.com/gohugoio/hugo/releases/download/v0.45.1/hugo_0.45.1_Linux-64bit.deb
    - sudo dpkg -i hugo*.deb
    - hugo version #没用
    - ls #没用
    - pwd #没用

script:
    - git submodule foreach git pull origin master #这句不知道什么意思
    - hugo #生成静态页面
    - echo 'Hugo build done!' #没用
after_script:
  - git clone "https://ifishzzz:${githubtoken}@${Github_REF}" github-pages #填入你的名字,以及travis的变量名,克隆gitpage页面
  - cp ./public/* github-pages -rf #本地生成的静态页面cp进gitpage
  - cd github-pages #进入gitpage,hugo上传只需要上传静态页面
  - git add . #gitpage是clone下来的不用git init
  - git commit -m "Update Blog By TravisCI With Build $TRAVIS_BUILD_NUMBER"
  - git tag v0.0.$TRAVIS_BUILD_NUMBER -a -m "Auto Taged By TravisCI With Build $TRAVIS_BUILD_NUMBER" #没用
  # Github Pages
  - git push --force --quiet "https://ifishzzz:${githubtoken}@${Github_REF}" master:master
  - git push --quiet "https://ifishzzz:${githubtoken}@${Github_REF}" master:master --tags
  # deploy: #没软用,其他的假教程说有用
  # local_dir: public # Default static site output dir for Hugo
  # repo: tainzhi/tainzhi.github.io # This is the slug of the repo you want to deploy your site to
  # target_branch: master # GitHub pages branch to deploy to (in other cases it can be gh-pages)
  # provider: pages
  # skip_cleanup: true
  # github_token: $GithubToken # This is the authentication which you will setup in the next step in travis-ci dashboard
  # email: qfq61@qq.com
  # name: "qfq"
  # on:
    # branch: master

env:
 global:
   # Github Pages
   - Github_REF: github.com/ifishzzz/ifishzzz.github.io.git #配置变量

0x03 本地构建hugo

测试没问题,把整个网站推到blog-hugo这个repo

git init
git remote add origin https://github.com/ifishzzz/blog-hugo.git
git status
git add .
git commit -m 'code'
git push -u origin master

travis网站会自动检测,可以看脚本运行状态,自动部署完成后,然后就可以打开ifishzzz.github.io访问啦

0x04 自定义域名

  1. 域名解析
hostname type address
@ a 185.199.110.153
www cname ifishzzz.github.io
  1. 进入github repo setting 加入自定义域名,强制https

  2. 在gihub repo 添加一个CNAME文件,填写域名if1sh.com

0x05 参考资料