Home Archives Categories Tags

Github 使用 travisci 做自动化测试

发布时间: 更新时间: 总字数:823 阅读时间:2m 作者: 分享

Github 使用 travisci 做自动化测试

travisci简介

Travis CI 是一款 Web 端的持续集成(Continuous Integration)工具。可以实现用户在提交代码、打tag等动作时,触发自动化脚本。官网:

帮助文档:

配置

Travis 要求项目的根目录下面,必须有一个.travis.yml文件。这是配置文件,指定了 Travis 的行为。

.travis.yml采用YAML格式,

sudo: false     # 使用容器进行编译,速度更快

language: go    # 环境

os:
  - linux       # 系统

go:
  - "1.11.4"    # 语言版本

branches:       # 对哪些分支生效
  only:
    - master
    - /^(release-v|v)\d+\.\d+\.\d+$/

env:            # 环境变量
  global:
    - REPO_NAME=bk-cmdb
    - ALIAS_REPO_NAME=configcenter
    - VERSION=`cat VERSION`
    - BRANCH_VERSION=`echo $VERSION | ( IFS=".$IFS" ; read major minor revision && echo $major.$minor.x )`
    - BRANCH=release-v$VERSION
    - GH_REF=github.com/g-robot/cmdb.git

cache:          # 环境变量
  directories:
    - node_modules

install: true   # 跳过 install 阶段

before_script:  # 环境准备
  - sudo apt-get update -qq
  - sudo apt-get install -qq -y make tree
  - REPO_PATH=`pwd`
  - echo $REPO_PATH && tree
  # - chmod +x ./script/publish-to-github.sh

script:         # 编译脚本
  - ls $GOPATH && ls $GOPATH/src
  - cd $GOPATH/src
  - git clone https://github.com/Tencent/$REPO_NAME $ALIAS_REPO_NAME
  - cd ${GOPATH}/src/$ALIAS_REPO_NAME/
  - git checkout $BRANCH
  - cd ${GOPATH}/src/$ALIAS_REPO_NAME/src/
  - make
  # - make package
  - make enterprise
  - tree $GOPATH/src/$ALIAS_REPO_NAME/src/bin/
  - cp -rp $GOPATH/src/$ALIAS_REPO_NAME/src/bin/build/$VERSION/web $GOPATH/src/$ALIAS_REPO_NAME/src/bin/enterprise/cmdb/
  - cp -rp $REPO_PATH/templates/v$BRANCH_VERSION/* $GOPATH/src/$ALIAS_REPO_NAME/src/bin/enterprise/cmdb/
  - cd $GOPATH/src/$ALIAS_REPO_NAME/src/bin/enterprise
  - tree
  - if [ -f "./cmdb/support-files/projects.yaml.bak" ]; then cp ./cmdb/support-files/projects.yaml.bak ./cmdb/projects.yaml; sed -i 's#__VERSION__#$BRANCH_VERSION#g' ./cmdb/projects.yaml; sed -i 's#__VERSION_TYPE__#ee#g' ./cmdb/projects.yaml; fi
  - echo $VERSION > cmdb/VERSION
  - touch cmdb/release.md
  - echo "more log at https://github.com/Tencent/bk-cmdb" > cmdb/release.md
  - tar -zcvf cmdb_ee-v$VERSION.tgz cmdb

# after_script:  # 编译后处理
#   - ./script/publish-to-github.sh

deploy:          # 部署
  provider: releases  # repo 的 releases
  api_key:
    secure: NIkdBtUHDjzqDFiYXVSjFbrWkCPcrjBbtKxMzZQ7XQeitxEifEqEr5OXxoGdFWASwy1sbpExFIwY1ErQ/7cHiaMTtD/OPvXlpVxPp1zmnZGIchEiv4pnH4TDSQ3N0HauUpB9pWYPz71zImGWLzR3zkks/M80Z8ckfMt0eMxYWQwCctvgGz9djGTYW+5JmMY3OUTVSe0LmJ3M8QA6nlQeE0sH85Rkp0aZwfBSnPE5UK43/DL829BFEL3oHBM2oS9cQ5E53A92pykw7GfMSthvmRIt2RWkITDpPCIZDbQQJWK0lR0pZK9Eg/6BkcGVdqu7BGWQJ3Tv7+ruurv3F22fLbg7w/Vmcr8nsftLr9EM8esQ0w1RNJ1Z2GBW6hsx0VhO2u8zoWfJ/LOMBjyTh0Al6+Y2KA8/jg7QC8hyMYnEdPaFVYbNZ2nDy/ukhzRX9jA27Bk8d81l5NkJ/xjkLpqrH/Ux/suPPCbP/LnMkliWk/M1zKnd849vHJi3/drj9fjGRp87pZqF91FIvSLO6SgKWK+1L12LvC0kph0iniYI08uDmrp7MOj9QFsDUpXqk2RqRWoRLteKqNqP9BoC8GXYRzjEvotgzRjKJq3WJH1ZtptDekEd9Ujs9PrJAQyfD5WO+ftT8KuurnndAmMz+ilCYJ3igv+cvpALcNihc05BZE4=
  file: "$GOPATH/src/$ALIAS_REPO_NAME/src/bin/enterprise/cmdb_ee-v$VERSION.tgz"
  skip_cleanup: true
  on:
    tags: true
    # repo: G-robot/cmdb

after_success:   # 成功后脚本
  - echo "ok"

notifications:   # 通知
  email:
    recipients:
      - me@xiexianbin.cn
    on_success: always
    on_failure: always

获取 deploy -> api_key

> travis setup releases -r xiexianbin/cmdb-x --force
Username: xiexianbin
Password for xiexianbin: *************
File to Upload: /<path>/abc.tgz
Deploy only from xiexianbin/cmdb-x? |yes|
fatal: not a git repository (or any of the parent directories): .git
Deploy from  branch? |yes|
Encrypt API key? |yes|
>

.travis.yml 格式如下:

deploy:
  provider: releases
  api_key:
    secure: NIkdBtUHDjzqFFiYXVSjFbrWkCPcrjBbtKxMzZQ7XQeitxEifEqEr5OXxoGdFWASwy1sbpExFIwY1ErQ/7cHiaMTtD/OPvXlpVxPp1zmnZGIchEiv4pnH4TDSQ3N0HauUpB9pWYPz71zImGWLzR3zkks/M80Z8ckfMt0eMxYWQwCctvgGz9djGTYW+5JmMY3OUTVSe0LmJ3M8QA6nlQeE0sH85Rkp0aZwfBSnPE5UK43/DL829BFEL3oHBM2oS9cQ5E53A92pykw7GfMSthvmRIt2RWkITDpPCIZDbQQJWK0lR0pZK9Eg/6BkcGVdqu7BGWQJ3Tv7+ruurv3F22fLbg7w/Vmcr8nsftLr9EM8esQ0w1RNJ1Z2GBW6hsx0VhO2u8zoWfJ/LOMBjyTh0Al6+Y2KA8/jg7QC8hyMYnEdPaFVYbNZ2nDy/ukhzRX9jA27Bk8d81l5NkJ/xjkLpqrH/Ux/suPPCbP/LnMkliWk/M1zKnd849vHJi3/drj9fjGRp87pZqF91FIvSLO6SgKWK+1L12LvC0kph0iniYI08uDmrp7MOj9QFsDUpXqk2RqRWoRLteKqNqP9BoC8GXYRzjEvotgzRjKJq3WJH1ZtptDekEd9Ujs9PrJAQyfD5WO+ftT8KuurnndAmMz+ilCYJ3igv+cvpALcNihc05BZE4=
  file: "/<path>/abc.tgz"
  on:
    repo: xiexianbin/cmdb-x
    branch: ''

环境矩阵

dist: xenial


language: python


branches:
  only:
    - master

matrix:
  include:
    - python: 3.7
      env: TOXENV=flake8
    - python: 3.7
      env: TOXENV=mypy

    - python: 2.7
      env: TOXENV=coverage-py27-tw171,codecov-py27
    - python: 2.7
      env: TOXENV=coverage-py27-tw184,codecov-py27
    - python: 2.7
      env: TOXENV=coverage-py27-twcurrent,codecov-py27

    - python: 3.5
      env: TOXENV=coverage-py35-tw171,codecov-py35
    - python: 3.5
      env: TOXENV=coverage-py35-tw184,codecov-py35
    - python: 3.5
      env: TOXENV=coverage-py35-twcurrent,codecov-py35

    - python: 3.6
      env: TOXENV=coverage-py36-tw171,codecov-py36
    - python: 3.6
      env: TOXENV=coverage-py36-tw184,codecov-py36
    - python: 3.6
      env: TOXENV=coverage-py36-twcurrent,codecov-py36

    - python: 3.7
      env: TOXENV=coverage-py37-tw171,codecov-py37
    - python: 3.7
      env: TOXENV=coverage-py37-tw184,codecov-py37
    - python: 3.7
      env: TOXENV=coverage-py37-twcurrent,codecov-py37

    - python: pypy
      env: TOXENV=coverage-pypy2-tw171,codecov-pypy2
    - python: pypy
      env: TOXENV=coverage-pypy2-tw184,codecov-pypy2
    - python: pypy
      env: TOXENV=coverage-pypy2-twcurrent,codecov-pypy2

    - python: pypy3
      env: TOXENV=coverage-pypy3-tw171,codecov-pypy3
    - python: pypy3
      env: TOXENV=coverage-pypy3-tw184,codecov-pypy3
    - python: pypy3
      env: TOXENV=coverage-pypy3-twcurrent,codecov-pypy3

    # Test against Twisted trunk in case something in development breaks us.
    # This is allowed to fail below, since the bug may be in Twisted.
    - python: 2.7
      env: TOXENV=coverage-py27-twtrunk,codecov-py27
    - python: 3.7
      env: TOXENV=coverage-py37-twtrunk,codecov-py37

    - python: 2.7
      env: TOXENV=twistedchecker-diff
    - python: 2.7
      env: TOXENV=docs
    - python: 2.7
      env: TOXENV=docs-linkcheck

  allow_failures:
    # Tests against Twisted trunk are allow to fail, as they are not supported.
    - env: TOXENV=coverage-py27-twtrunk,codecov-py27
    - env: TOXENV=coverage-py37-twtrunk,codecov-py37

    # This is not yet required.
    - env: TOXENV=twistedchecker-diff

    # This depends on external web sites, so it's allowed to fail.
    - env: TOXENV=docs-linkcheck


install:
  - ./.travis/install


script:
  - ./.travis/run tox

触发

提交代码到github就可以触发对应的任务。更多示例参考:https://travis-ci.org/xiexianbin

参考

最新评论
加载中...