把golang项目接入Travis CI,从而获得持续集成的能力。这里拿正在写的一个iris的项目举例。https://github.com/liguoqinjim/iris_template

1.单元测试和travis的badge

新建.travis.yml

在项目中新建.travis.yml,内容为

language: go
go:
  - 1.13.x
  - 1.14.x
env:
  - GO111MODULE=on
script:
  - go test ./... -v

travis的badge

在travis里面可以拿到一个build状态的badge,我们可以加到项目的README中。 在travis的build界面拿到badge,如: golang_travis_01

2.在travis中使用mysql和redis

language: go
go:
  - 1.13.x
  - 1.14.x
env:
  - GO111MODULE=on
services:
  - mysql
  - redis-server
before_install:
  - mysql -e 'CREATE DATABASE temp_db DEFAULT CHARACTER SET utf8;'

在services里面添加mysql和redis-server,运行环境就会有这两个服务了。before_install里面可以加上数据库的初始化脚本。 mysql的用户名为root,密码为空。redis没有密码

3.测试覆盖率和codecov的badge

测试覆盖率脚本

.travis.yml

language: go
go:
  - 1.13.x
  - 1.14.x
env:
  - GO111MODULE=on
services:
  - mysql
  - redis-server
before_install:
  - mysql -e 'CREATE DATABASE temp_db DEFAULT CHARACTER SET utf8;'
script:
  - "./test.sh"
after_success:
  - bash <(curl -s https://codecov.io/bash)
before_deploy: "./build.sh"

把测试命令移到脚本里面,新建test.sh

#!/bin/bash

set -e
echo "" > coverage.txt

for d in $(go list ./...); do
	echo $d
    go test -v -race -coverprofile=profile.out -covermode=atomic $d
    if [ -f profile.out ]; then
        cat profile.out >> coverage.txt
        rm profile.out
    fi
done

test.sh脚本把测试覆盖率的输出存到了coverage.txt里面,要是测试成功,.travis.yml里面的after_success就会被调用,会把测试覆盖率信息发送到codecov

codecov的badge

golang_travis_03

4.发布到release

.travis.yml

language: go
go:
  - 1.13.x
  - 1.14.x
env:
  - GO111MODULE=on
services:
  - mysql
  - redis-server
before_install:
  - mysql -e 'CREATE DATABASE temp_db DEFAULT CHARACTER SET utf8;'
script:
  - "./test.sh"
after_success:
  - bash <(curl -s https://codecov.io/bash)
before_deploy: "./build.sh"

deploy:
  provider: releases
  api_key:
    secure: ...
  file_glob: true
  file: releases/*
  skip_cleanup: true
  on:
    repo: liguoqinjim/iris_template
    tags: true

新建打包的脚本,build.sh

#!/bin/bash

set -e

SOURCE_FILE_NAME=main
TARGET_FILE_NAME=iris_template
RELEASE_PATH=releases/

mkdir releases
rm -fr ${TARGET_FILE_NAME}*
rm -rf ${RELEASE_PATH}*

build(){
    echo $GOOS $GOARCH

    tname=${TARGET_FILE_NAME}_${GOOS}_${GOARCH}${EXT}
    echo 'tname='$tname

    env GOOS=$GOOS GOARCH=$GOARCH \
    go build -o ${tname} \
    -v *.go

    chmod +x ${tname}
    mv ${tname} ${TARGET_FILE_NAME}${EXT}

    if [ ${GOOS} == "windows" ];then
        #zip ${tname}.zip ${TARGET_FILE_NAME}${EXT} config.ini ../public/
        filename=${tname}.zip
        zip $filename ${TARGET_FILE_NAME}${EXT} app.toml
        mv ${filename} ${RELEASE_PATH}${filename}
    else
        filename=${tname}.tar.gz
        tar --exclude=*.gz  --exclude=*.zip  --exclude=*.git -czvf ${filename} ${TARGET_FILE_NAME}${EXT} app.toml *.sh -C ./ .
        mv ${filename} ${RELEASE_PATH}${filename}
    fi
    echo "target:"${TARGET_FILE_NAME}${EXT}
    mv ${TARGET_FILE_NAME}${EXT} ${RELEASE_PATH}${tname}
}

CGO_ENABLED=0
#mac os 64
GOOS=darwin
GOARCH=amd64
build

#linux 64
GOOS=linux
GOARCH=amd64
build

#windows
#64
GOOS=windows
GOARCH=amd64
build

#32
GOARCH=386
build

安装Travis CI Client

Travis CI Client的主页:https://github.com/travis-ci/travis.rb,里面有安装方法。 MAC下可以直接用brew安装:brew install travis

获得github的api key

在项目路径下运行travis setup releases,跟着提示操作就可以获得上传到github的权限了。

触发

脚本里面设置了tags: true,所以只能有tag的build才能触发deploy的操作。

release效果

golang_travis_04

5.badge效果

golang_travis_02

6.参考资料