Travis CI Build Continuous Integration
什么是持续集成
持续集成(Continuous Integration)是经常合并小的代码更改的实践,而不是在开发周期结束时合并大的更改。目的是通过以较小的增量开发和测试来构建更健康的软件。这就是Travis CI出现的地方。
作为一个持续集成平台,Travis CI通过自动构建和测试代码更改来支持您的开发过程,并提供有关更改成功的即时反馈。Travis CI还可以通过管理部署和通知来自动化开发过程的其他部分。
Travis CI 介绍
Travis CI 提供的是持续集成服务(Continuous Integration,简称 CI)。它绑定 Github 上面的项目,只要有新的代码,就会自动抓取。然后,提供一个运行环境,执行测试,完成构建,还能部署到服务器。 持续集成指的是只要代码有变更,就自动运行构建和测试,反馈运行结果。确保符合预期以后,再将新代码"集成"到主干。 持续集成的好处在于,每次代码的小幅变更,就能看到运行结果,从而不断累积小的变更,而不是在开发周期结束时,一下子合并一大块代码。
CI构建和自动化:构建,测试,部署
运行构建时,Travis CI将GitHub存储库克隆到全新的虚拟环境中,并执行一系列任务来构建和测试代码。如果这些任务中的一项或多项失败,则将构建视为已 损坏。
如果所有任务均未失败,则认为构建已通过,并且Travis CI可以将您的代码部署到Web服务器或应用程序主机。
CI版本也可以自动化交付工作流程的其他部分。这意味着您可以通过Build Stages,设置通知,在构建后准备 部署以及许多其他任务来使工作相互依赖。
Travis CI 使用
使用前准备
操作系统:ubuntu 18.04
node: v10.16.0
travis version: 1.8.9
转到Travis-ci.com/Travis-ci.org并使用GitHub注册 接受Travis CI的授权。您将被重定向到GitHub。 单击Travis仪表板右上角的个人资料图片,单击绿色的Activate按钮,然后选择要用于Travis CI的存储库。
为命令行创建个人访问令牌
You can create a personal access token and use it in place of a password when performing Git operations over HTTPS with Git on the command line or the API.
A personal access token is required to authenticate to GitHub in the following situations:
- When you're using two-factor authentication
- To access protected content in an organization that uses SAML single sign-on (SSO). Tokens used with organizations that use SAML SSO must be authorized.
As a security precaution, GitHub automatically removes personal access tokens that haven't been used in a year.
Creating a token
Verify your email address, if it hasn't been verified yet.
In the upper-right corner of any page, click your profile photo, then click Settings.

In the left sidebar, click Developer settings.

In the left sidebar, click Personal access tokens.

Click Generate new token.

Give your token a descriptive name.

Select the scopes, or permissions, you'd like to grant this token. To use your token to access repositories from the command line, select repo.

Click Generate token.

Click to copy the token to your clipboard. For security reasons, after you navigate off the page, you will not be able to see the token again.

Warning: Treat your tokens like passwords and keep them secret. When working with the API, use tokens as environment variables instead of hardcoding them into your programs.
To use your token to authenticate to an organization that uses SAML SSO, authorize the token for use with a SAML single-sign-on organization.
Using a token on the command line
Once you have a token, you can enter it instead of your password when performing Git operations over HTTPS.
For example, on the command line you would enter the following:
$ git clone https://github.com/username/repo.git
Username: your_username
Password: your_token
Personal access tokens can only be used for HTTPS Git operations. If your repository uses an SSH remote URL, you will need to switch the remote from SSH to HTTPS.
If you are not prompted for your username and password, your credentials may be cached on your computer. You can update your credentials in the Keychain to replace your old password with the token.
安装 travis
安装rvm: curl -L get.rvm.io | bash -s stable 安装ruby: rvm install ruby-2.4.1 安装travis: gem install travis 查看版本: travis --version
开始使用 Travis CI
1.travis.yml文件
创建文件
travis init
编写travis文件
language: node_js
node_js:
- 10.16.0
branches:
only:
- master
install:
- npm install
script:
- npm run build
addons:
ssh_known_hosts:
- $IP
after_success:
- chmod 600 ~/.ssh/id_rsa
- ssh root@$IP -o StrictHostKeyChecking=no 'cd /root/blogs/ && git pull
&& npm install && npm run build'
2.ssh免密登入
1.生成密钥
ssh-keygen
2.上传到自己的服务器
ssh-copy-id -i ~/.ssh/id_rsa.pub root@47.106.210.183
3.测试免密登入
ssh root@47.106.210.183
3.对私钥加密
1.登入
travis login --org
2.对私钥加密(记得将id_rsa.enc加入git)
travis encrypt-file ~/.ssh/id_rsa --add --org
mikey@localhost:~/DATA/MIKEY/WebStromWorkSpace/innovate-admin-vue$ travis login --help
Authenticates against the API and stores the token.
Usage: travis login [OPTIONS]
-h, --help Display help
-i, --[no-]interactive be interactive and colorful
-E, --[no-]explode don't rescue exceptions
--skip-version-check don't check if travis client is up to date
--skip-completion-check don't check if auto-completion is set up
-e, --api-endpoint URL Travis API server to talk to
-I, --[no-]insecure do not verify SSL certificate of API endpoint
--pro short-cut for --api-endpoint 'https://api.travis-ci.com/'
--com short-cut for --api-endpoint 'https://api.travis-ci.com/'
--org short-cut for --api-endpoint 'https://api.travis-ci.org/'
--staging talks to staging system
-t, --token [ACCESS_TOKEN] access token to use
--debug show API requests
--debug-http show HTTP(S) exchange
-X, --enterprise [NAME] use enterprise setup (optionally takes name for multiple setups)
--adapter ADAPTER Faraday adapter to use for HTTP requests
-g, --github-token TOKEN identify by GitHub token
-T, --auto-token try to figure out who you are automatically (might send another apps token to Travis, token will not be stored)
-p, --auto-password try to load password from OSX keychain (will not be stored)
-a, --auto shorthand for --auto-token --auto-password
-u, --user LOGIN user to log in as
-M, --no-manual do not use interactive login
--list-github-token instead of actually logging in, list found GitHub tokens
--skip-token-check don't verify the token with github
其在travis生成如下内容(如果出现~\/.ssh/id_rs就将反斜杠去掉)
before_install:
- openssl aes-256-cbc -K $encrypted_bd16792238cd_key -iv $encrypted_bd16792238cd_iv
-in id_rsa.enc -out ~/.ssh/id_rsa -d
4.配置变量

5.完整的travis文件
language: node_js
node_js:
- 10.16.0
branches:
only:
- master
install:
- npm install
script:
- npm run build
addons:
ssh_known_hosts:
- $IP
before_install:
- openssl aes-256-cbc -K $encrypted_bd16792238cd_key -iv $encrypted_bd16792238cd_iv
-in id_rsa.enc -out ~/.ssh/id_rsa -d
- chmod 600 ~/.ssh/id_rsa
- echo -e "Host $IP\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
after_success:
- chmod 600 ~/.ssh/id_rsa
- rsync -az --delete ./dist/* root@$IP:/usr/share/nginx/html/blogs
6.推代码到仓库
CI成功:

搭建Docker项目
1.dockerfile + docker-compose 来构建docker容器
2.travis-ci + github 来hook repo 的变动
3.travis-ci 调用 dockerfile 打包 docker image 并push到 dockerhub
4.travis-ci ssh 登录到目标机器,copy docker-compose 并执行来完成部署 服务器提前装好docker-compose
1.Dockerfile (结合自己项目修改)
#基础镜像
FROM tomcat
#Message
MAINTAINER mikey<biaogejiushibiao@outlook.com>
#构建日期
ENV REFRESHED_AT 2019-11-11
#切换镜像目录,进入/usr目录
WORKDIR /usr/local/tomcat/webapps
#复制编译后的文件
COPY target/Attendance.war /usr/local/tomcat/webapps/Attendance_war.war
#公开端口
EXPOSE 8080
#设置启动命令
ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]
2.docker-compose.yml
version: '2'
services:
attendance:
build: .
image: mikeyboom/attendance
ports:
- "8004:8080"
container_name: attendance
3.travis.yml
#环境语言
language: java
#所选服务
services:
- docker
#权限
sudo: required
#分支
branches:
only:
- master
#执行脚本
script:
- mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
- docker build . -t "mikeyboom/attendance:latest"
#安装之前执行脚本
before_install:
- openssl aes-256-cbc -K $encrypted_71a1e6d76d99_key -iv $encrypted_71a1e6d76d99_iv
-in id_rsa.enc -out ~/.ssh/id_rsa -d
- chmod 600 ~/.ssh/id_rsa
- echo -e "Host $ip\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
#成功之后执行的脚本
after_success:
- docker login -u=$dockername -p="$dockerpassword"
- docker push mikeyboom/attendance:latest
- chmod 600 id_rsa
- rsync -az --delete ./docker-compose.yml root@$ip:/home/mikey/Attendance/
- ssh -o "StrictHostKeyChecking no" -i id_rsa root@$ip "cd /home/mikey/Attendance/;sudo
docker-compose -f docker-compose.yml pull;sudo docker-compose -f docker-compose.yml
up -d;exit"
4.记得对私钥进行加密
travis encrypt-file ~/.ssh/id_rsa --add
5.配置好变量

成功

参考资料
官方文档:
https://github.com/travis-ci/docs-travis-ci-com
相关博文:
https://www.cnblogs.com/homehtml/p/11796836.html
https://www.jianshu.com/p/631fc28202dc
https://docs.travis-ci.com/user/tutorial/
http://www.ruanyifeng.com/blog/2017/12/travis_ci_tutorial.html
Travis CI Build Continuous Integration的更多相关文章
- JUnit之持续集成(CI,Continuous Integration)
序,测试驱动开发告诉我们,要尽早测试,经常测试.如果我们进行一点小改动时,都把所有的单元测试.集成测试和功能测试执行一遍,这就会非常浪费时间.为了避免这一点,在开发期间我们只执行单元测试,那么集成测试 ...
- 利用Travis CI 让你的github项目持续构建
Travis CI 是目前新兴的开源持续集成构建项目,它与jenkins,GO的很明显的特别在于采用yaml格式,简洁清新独树一帜.目前大多数的github项目都已经移入到Travis CI的构建队列 ...
- TeamCity vs Jenkins: Which is the Better Continuous Integration (CI) Server for .NET Software Development?
原文:http://www.excella.com/insights/teamcity-vs-jenkins-better-continuous-integration-server So, you’ ...
- [转]GitLab Continuous Integration (GitLab CI/CD)
本文转自:https://docs.gitlab.com/ee/ci/README.html GitLab Continuous Integration (GitLab CI/CD) The bene ...
- 持续集成(CI – Continuous Integration)
持续集成(CI – Continuous Integration) 在传统的软件开发中,整合过程通常在每个人完成工作之后.在项目结束阶段进行.整合过程通常需要数周乃至数月的时间,可能会非常痛苦.持续集 ...
- 关于CI/CD/CD (Continuous Integration/Continuous Delivery/Continuous Deployment)
Continuous Integration (CI) Continuous integration (CI) is the process that ensures the stability of ...
- Jenkins实现CI(Continuous Integration)到CD(Continuous Delivery)
Pipeline as Code是2.0的精髓所在,是帮助Jenkins实现CI(Continuous Integration)到CD(Continuous Delivery)华丽转身的关键推手.所谓 ...
- 使用 Travis CI 实现项目的持续测试反馈
[篇幅较长,10.15前补充完毕,如希望探索可直接移步Github仓库:https://github.com/SivilTaram/CITest] 在编程课中,我们可以使用成熟的在线评测系统来测试某个 ...
- GitHub搭配使用Travis CI 进行自动构建服务
Travis CI (Continuous Integration)持续集成服务 用处:自动监控软件仓库,可以在代码提交后立刻执行自动测试或构建 1.在Github自己的仓库根目录里添加.travis ...
随机推荐
- [CodeIgniter4]故障排除和本地开发服务器
故障排除 以下是一些常见的安装问题,以及建议的解决方法. 我必须在我的URL中包含index.php 如果``/mypage/find/apple``类似的URL``/index.php/mypage ...
- SQL With As的用法
WITH AS,也叫子查询部分(subquery factoring),可以定义一个SQL片断,该SQL片断会被整个SQL语句用到.可以使SQL语句的可读性更高,也可以在UNION ALL的不同部分, ...
- DoS攻击种类
DoS攻击有许多种类,主要有Land攻击.死亡之ping.泪滴.Smurf攻击及SYN洪水等. 据统计,在所有黑客攻击事件中,syn洪水攻击是最常见又最容易被利用的一种DoS攻击手法. 1.攻击原理 ...
- Nginx之反向代理
所谓,反向代理就是,客户端向A服务器地址发送请求,A服务器接收到客户端请求后又将请求转发给了B服务器,最后又将B服务响应的数据响应给了客户端. 通过配置文件,可以实现Nginx的反向代理. 代码: s ...
- ftp的相关配置
参考 https://www.cnblogs.com/hexige/p/7809481.html 访问参数
- How to make mail more effectively?
1.What does your reader need to know? 2.What does your reader know already? 3.Will your reader be ab ...
- Lingo简单入门,以及对线性规划做敏感性分析设置
Lingo中用!表示注释,注释结束用;表示,lingo不区分大小写,运行时会自动统一装换成大写 编程步骤: 1.推算出正确的模型 2.确定描述集,定义集合 3.确定变量 4.正确写出每个式子 常用函数 ...
- 素问 - REITs
摘自<小韭的学习圈> Q 一直以来对REITs感兴趣,看过您微信公众号对REITs的分析,年化8-10%,长期收益稳定,且与其他投资品种关键性低,是很不错的分散配置选择. 您推荐的广发美国 ...
- Go网络编程TCP
1. 服务端 package main import ( "bufio" "fmt" "net" "os" " ...
- HTML学习(1)简介
HTML---HyperText Markup Language,超文本标记语言,是一种用于创建网页的标准标记语言. 注意:对于中文网页需要使用 <meta charset="utf- ...