准备工作

  1. github生成私钥

微信图片_20190306201812.png

  1. 加密上面的私钥到travis

项目根目录内打开命令,运行下面的命令

  1. travis encrypt KEY=value //KEY 为自定义变量名,value1步骤中的token的值
  1. 会输出下列信息
  1. secure:一串很长的字符串...... //下面会用到

将生成的字符串复制,配置到travis-ci.org
微信图片_20190306205038.png

  1. .travis.yml其他配置

下面是我的文件配置,直接拷贝修改即可

  1. language: node_js #设置语言
  2. node_js: stable #设置相应的版本
  3. cache:
  4. apt: true
  5. directories:
  6. - node_modules # 缓存不经常更改的内容
  7. before_install:
  8. - export TZ='Asia/Shanghai' # 更改时区
  9. install:
  10. - npm install hexo #安装hexo及插件
  11. script:
  12. - hexo clean #清除
  13. - hexo g #生成
  14. after_script:
  15. - git clone https://${GH_REF} .deploy_git # GH_REF是最下面配置的仓库地址
  16. - cd .deploy_git
  17. - git checkout master
  18. - cd ../
  19. - mv .deploy_git/.git/ ./public/ # 这一步之前的操作是为了保留master分支的提交记录,不然每次git init的话只有1条commit
  20. - cd ./public
  21. - git config user.name "username" #修改name
  22. - git config user.email "username@gmail.com" #修改email
  23. - git add .
  24. - git commit -m "Travis CI Auto Builder at `date +"%Y-%m-%d %H:%M"`" # 提交记录包含时间 跟上面更改时区配合
  25. - git push --force --quiet "https://${Travis_Token}@${GH_REF}" master:master #Travis_Token是在Travis中配置环境变量的名称
  26. branches:
  27. only:
  28. - hexo_config #只监测hexo_config分支,按需配置 hexo_config是我hexo项目配置的分支,master是我静态博客输出的分支
  29. env:
  30. global:
  31. - GH_REF: github.com/username/username.github.io.git #设置GH_REF,设置你自己的就行
  32. # configure notifications (email, IRC, campfire etc)
  33. # please update this section to your needs!
  34. # https://docs.travis-ci.com/user/notifications/
  35. notifications:
  36. email:
  37. - username@gmail.com
  38. on_success: change
  39. on_failure: always

更多配置介绍查看https://docs.travis-ci.com/
下一篇讲一下,next主题