Use Travis CI + GitHub achieve continuous integration and automated deployment

travis+github.png

Foreword

If you manually deployed the project, we will be deeply necessity of continuous integration, because it is manual deployment and time-consuming and cumbersome, although the deployment process basically fixed, still prone to error.

If you are familiar with continuous integration, we will agree with this view: "It has become a standard to use."

What is continuous integration
the Continuous Integration (CI) IS A Development Practice The requires that Developers to integrate code INTO A Shared Repository 'several' Times A Day. Each the Check-in Verified by the then IS AN Automated Build, Allowing Teams is to Detect Problems Early.
--- ThoughtWorks
translation: continuous integration is a development activity, it requires developers to integrate the code multiple times a day to a shared repository, each submission will be checked automatically builds, teams can therefore detect problems in advance.

Continuous integration tool is very large, for example with java language development Jenkins, due to its characteristics can be distributed build and load testing on multiple machines, many large companies are using it.

But Jenkins' without modification interface interface so I have some dislike ...

With the development of GitHub, there has been growing support for GitHub of CI / CD products. On GitHub market, we can see that already supports continuous integration service provider has more than more than 300 ( more details ).

github continuous integration.jpg

Choose Travis CI, is recommended because a lot of friends around.

Here I share with you how to use the Travis CI + GitHub achieve continuous integration and automated deployment through some of my research and practical experience, the desire to help a friend in need.

What is Travis CI

Travis CI is a continuous integration services with an open source Ruby language development of distributed, used to automatically build and test project hosted on GitHub. Including support Javascript, Node.js, Ruby and other 20 kinds of programming languages. For open source projects with free CI services. You can also buy his paid version, enjoy more services.

Travis CI currently has two official website, are https://travis-ci.org and https://travis-ci.com.
https://travis-ci.org old platform to the new platform has been gradually migrated https://travis-ci.com. For private warehouse of free automated build, Travis CI on the new platform has supported.

travis-CI-0.jpg

First, access to GitHub Access Token

Travis CI when the automatic deployment of the need to push the contents of a branch warehouses, and access GitHub repository require user authorization, authorized way to provide users with Access Token to Travis CI.

Get token GitHub->Settings->Developer Settings->Personal access tokensposition: .

Check repoall the items, and userunder the user:emailrear, generates a token, token value replication.

Note: This token is only now can see, will not see re-enter, but never see, forget it can only regenerate, so remember to take good care of.

personal-access-token-variable.jpg

Second, the use GitHub account login Travis

Enter Travis's official website , log in with GitHub account. (I am currently using its old platform)
travis one-1.jpg

After logging in, you will see all public open source repo under their own account in GitHub Travis Lane.

Third, open the monitoring of projects

Selecting a target item, the right switch is opened.
travis-CI-4.jpg

Fourth, configure travis

  • Click the switch on the right Settings, enter travis configuration page of the project
  • Check the trigger condition
    travis-CI-7.jpg
  • Set global variables
    travis-CI-8.jpg
    Note: The first step in obtaining the access token, you must set
    -set variables can be $ {name} variable to reference in the configuration file.
    travis-CI-9.jpg

Fifth, add the project root directory .travis.ymlprofile

Note that the file name to .begin with.

Travis CI once constructed in two steps:

  1. install, the installation of any required dependencies
  2. The script is run build scripts

Travis CI offers some build lifecycle "hook"

Travis CI build a complete life cycle:

  1. OPTIONAL Install apt addons
  2. OPTIONAL Install cache components
  3. before_install
  4. install
  5. before_script
  6. script
  7. OPTIONAL before_cache(for cleaning up cache)
  8. after_success or after_failure
  9. OPTIONAL before_deploy
  10. OPTIONAL deploy
  11. OPTIONAL after_deploy
  12. after_script

In before_install, before_scriptbefore, or after_scriptafter, you can run custom commands while the details may refer to the official documentation: the Job Lifecycle

I footprintproject .travis.ymla complete configuration:

language: node_js #设置语言

node_js: "10.16.3" #设置语言版本

cache:
  directories:
    - node_modules #缓存依赖

# S: Build Lifecycle
install:
  - npm i

script:
  - npm run build

#after_script前5句是把部署分支的.git文件夹保护起来,用于保留历史部署的commit日志,否则部署分支永远只有一条commit记录。
#命令里面的变量都是在Travis CI里配置过的。
after_script:
  - git clone https://${GH_REF} .temp
  - cd .temp
  - git checkout gh-pages
  - cd ../
  - mv .temp/.git dist
  - cd dist
  - git config user.name "${U_NAME}"
  - git config user.email "${U_EMAIL}"
  - git add .
  - git commit -m ":construction_worker:- Build & Deploy by Travis CI"
  - git push --force --quiet "https://${Travis_Token}@${GH_REF}" gh-pages:${D_BRANCH}
# E: Build LifeCycle

# 只有指定的分支提交时才会运行脚本
branches:
  only:
    - master

Done!

The .travis.ymlpush to the remote, you can see travis start building a compiler. And after each push of code, travis automatically execute .travis.ymlscript in the configuration of the task.

  • Automatic compilation:
    travis-CI-6.jpg
  • Building completion, travis based on my configuration, automatically deployed to GitHub:
    travis-CI-10.jpg

And One More Thing

After building a successful, we can add in your own GitHub project in the buildbadge.
Methods: Travis, click on the badge on the right side of the project, you can get a small badge address, the address in README.md document can be.
travis-CI-12.jpg
effect:
travis-CI-11.jpg

--
GOODLUCK!

Welcome to reprint, please indicate the source: https: //champyin.com/2019/09/27/%E5%88%A9%E7%94%A8Travis-CI-GitHub%E5%AE%9E%E7%8E% B0% E6% 8C% 81% E7% BB% AD% E9% 9B% 86% E6% 88% 90% E5% 92% 8C% E8% 87% AA% E5% 8A% A8% E9% 83% A8% E7% BD% B2 / # more

This article was published in sync:
the use of Travis CI + GitHub achieve continuous integration and deployment of automatic | Denver

Guess you like

Origin www.cnblogs.com/champyin/p/11621898.html