什么是持续集成

持续集成(Continuous Integration)是经常合并小的代码更改的实践,而不是在开发周期结束时合并大的更改。目的是通过以较小的增量开发和测试来构建更健康的软件。这就是Travis CI出现的地方。
作为一个持续集成平台,Travis CI通过自动构建和测试代码更改来支持您的开发过程,并提供有关更改成功的即时反馈。Travis CI还可以通过管理部署和通知来自动化开发过程的其他部分。 

Travis CI 介绍

官网:https://www.travis-ci.org

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:

As a security precaution, GitHub automatically removes personal access tokens that haven't been used in a year.

Creating a token

  1. Verify your email address, if it hasn't been verified yet.

  2. In the upper-right corner of any page, click your profile photo, then click Settings.

  3. In the left sidebar, click Developer settings.

  4. In the left sidebar, click Personal access tokens.

  5. Click Generate new token.

  6. Give your token a descriptive name.

  7. 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.

  8. Click Generate token.

  9. 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.

  10. 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://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的更多相关文章

  1. JUnit之持续集成(CI,Continuous Integration)

    序,测试驱动开发告诉我们,要尽早测试,经常测试.如果我们进行一点小改动时,都把所有的单元测试.集成测试和功能测试执行一遍,这就会非常浪费时间.为了避免这一点,在开发期间我们只执行单元测试,那么集成测试 ...

  2. 利用Travis CI 让你的github项目持续构建

    Travis CI 是目前新兴的开源持续集成构建项目,它与jenkins,GO的很明显的特别在于采用yaml格式,简洁清新独树一帜.目前大多数的github项目都已经移入到Travis CI的构建队列 ...

  3. 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’ ...

  4. [转]GitLab Continuous Integration (GitLab CI/CD)

    本文转自:https://docs.gitlab.com/ee/ci/README.html GitLab Continuous Integration (GitLab CI/CD) The bene ...

  5. 持续集成(CI – Continuous Integration)

    持续集成(CI – Continuous Integration) 在传统的软件开发中,整合过程通常在每个人完成工作之后.在项目结束阶段进行.整合过程通常需要数周乃至数月的时间,可能会非常痛苦.持续集 ...

  6. 关于CI/CD/CD (Continuous Integration/Continuous Delivery/Continuous Deployment)

    Continuous Integration (CI) Continuous integration (CI) is the process that ensures the stability of ...

  7. Jenkins实现CI(Continuous Integration)到CD(Continuous Delivery)

    Pipeline as Code是2.0的精髓所在,是帮助Jenkins实现CI(Continuous Integration)到CD(Continuous Delivery)华丽转身的关键推手.所谓 ...

  8. 使用 Travis CI 实现项目的持续测试反馈

    [篇幅较长,10.15前补充完毕,如希望探索可直接移步Github仓库:https://github.com/SivilTaram/CITest] 在编程课中,我们可以使用成熟的在线评测系统来测试某个 ...

  9. GitHub搭配使用Travis CI 进行自动构建服务

    Travis CI (Continuous Integration)持续集成服务 用处:自动监控软件仓库,可以在代码提交后立刻执行自动测试或构建 1.在Github自己的仓库根目录里添加.travis ...

随机推荐

  1. JavaDay1(上)

    Java learning_Day1(上) 一切准备工作已经做好,虽然自己之前也零零碎碎学了一些Java的基础知识,貌似现在忘得差不多了,趁寒假契机从头开始学习吧 本人学习视频用的是马士兵的,也在这里 ...

  2. maven scope 的作用

    一: 1.Maven中的依赖作用范围概述 Maven中使用 scope 来指定当前包的依赖范围和依赖的传递性.常见的可选值有:compile, provided, runtime, test, sys ...

  3. [CF705B] Spider Man - 博弈论

    [CF705B] Description ICG 游戏有若干个环,每次操作将一个环断成非空的两部分,节点数总和不变.集合初态为空,每次向集合中添加一个环,询问当前集合用于游戏的胜负. \(n \le ...

  4. python property(不动产)方法

    class Test(object): @property def test(self): return 100 @test.setter def test(self): return "修 ...

  5. SQL中 select count(1) count中的1 到底是什么意思呢?和count(*)的区别

    count(1),其实就是计算一共有多少符合条件的行. 1并不是表示第一个字段,而是表示一个固定值.其实就可以想成表中有这么一个字段,这个字段就是固定值1,count(1),就是计算一共有多少个1.同 ...

  6. [thinkphp] 启用__PUBLIC__

    我真是受够了,,, 为了解决__PUBLIC__不能用的问题 我折腾了好几天了,然后终于被我找到了原因 解决过程 首先必须贴出来帮助我的人 https://my.oschina.net/u/12630 ...

  7. SpringBoot 各层之间的关系

    SpringBoot 各层之间的关系 SpringBoot 分为四层:controller层.service层.dao层.entity层.  entity层:和 model 层一样,存放的是实体类,属 ...

  8. (c#)拼写单词

    题目 解

  9. Nearest Interesting Number

    Polycarp knows that if the sum of the digits of a number is divisible by 33, then the number itself ...

  10. 210. 课程表 II

    Q: 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1] 给定课程总量 ...