travis-ci加持

准备工作

  1. github生成私钥

微信图片_20190306201812.png

  1. 加密上面的私钥到travis

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

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

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

  1. .travis.yml其他配置

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
language: node_js  #设置语言

node_js: stable #设置相应的版本

cache:
apt: true
directories:
- node_modules # 缓存不经常更改的内容

before_install:
- export TZ='Asia/Shanghai' # 更改时区

install:
- npm install hexo #安装hexo及插件


script:
- hexo clean #清除
- hexo g #生成

after_script:
- git clone https://${GH_REF} .deploy_git # GH_REF是最下面配置的仓库地址
- cd .deploy_git
- git checkout master
- cd ../
- mv .deploy_git/.git/ ./public/ # 这一步之前的操作是为了保留master分支的提交记录,不然每次git init的话只有1条commit
- cd ./public
- git config user.name "username" #修改name
- git config user.email "username@gmail.com" #修改email
- git add .
- git commit -m "Travis CI Auto Builder at `date +"%Y-%m-%d %H:%M"`" # 提交记录包含时间 跟上面更改时区配合
- git push --force --quiet "https://${Travis_Token}@${GH_REF}" master:master #Travis_Token是在Travis中配置环境变量的名称

branches:
only:
- hexo_config #只监测hexo_config分支,按需配置 hexo_config是我hexo项目配置的分支,master是我静态博客输出的分支

env:
global:
- GH_REF: github.com/username/username.github.io.git #设置GH_REF,设置你自己的就行

# configure notifications (email, IRC, campfire etc)
# please update this section to your needs!
# https://docs.travis-ci.com/user/notifications/
notifications:
email:
- username@gmail.com
on_success: change
on_failure: always

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