您的位置 首页 golang

在 Go 语言项目中使用 Travis CI

原文链接:在 Go 语言项目中使用 Travis CI

Travis CI 是一种免费的持续集成服务,而 持续集成(CI, Continuous integration) 是一种软件工程流程,概括来讲就是多提交小的 Commit 来更快的发现软件的 Bug,从而提高软件质量。

本文会详细介绍如何在 Go 语言项目中使用 Travis CI。

Travis CI logo
Travis CI logo

准备工作

  • GitHub 账号:用于保存项目。
  • Travis CI 账号:点击右上角的 Sign in with GitHub 即可通过 GitHub 创建关联账号。
  • 示例项目 hello,它包含三个文件:

hello.go:

package hellofunc Hello() string { return "Hello, World!" }复制代码

hello_test.go:

package helloimport (	"testing")func TestHello(t *testing.T) {	if got, want := Hello(), "Hello, World!"; got != want {		t.Errorf("got %v, want %v", got, want)	}}复制代码

go.mod:

module github.com/linehk/hellogo 1.14复制代码

该文件通过命令:go mod init github.com/linehk/hello 生成。

建立 GitHub 仓库

建立一个 GitHub 仓库,并将上面三个文件组成的 hello 项目推送到仓库。

同步仓库到 Travis CI

Travis CI 仓库页面左上角点击 Sync account 来将 GitHub 新创建的仓库同步到 Travis CI。

并在左边的 Legacy Services Integration 下找到 hello 项目,将其勾选上。如下图所示:

勾选 hello 项目
勾选 hello 项目

编写 .travis.yml 文件

受益于在 Go 1.13 版本后,Go Modules 是默认开启的情况下,以下就是最简单的 .travis.yml 文件,.yml 后缀表示使用的是 YAML 格式

# 使用 Go 语言(# 号开头的为注释)language: goscript:  - go test -v ./...复制代码

开始集成测试

将上一步编写的 .travis.yml 文件加入仓库并推送至 GitHub。Travis CI 检测到该文件就会根据里面的内容开始测试。之后每次推送都会触发测试。

dashboard 中可以找到 hello 项目,点击进入就可以看到详细的测试信息。如下图所示:

详细测试信息
详细测试信息

Travis CI 徽章

点击绿色的 build passing 就能得到徽章的地址,如:https://travis-ci.org/linehk/hello.svg?branch=master

可以将它贴在你的 README 文件里,如果项目在某次推送中测试失败,徽章就会变成红色。

参考链接

Building a Go Project


文章来源:智云一二三科技

文章标题:在 Go 语言项目中使用 Travis CI

文章地址:https://www.zhihuclub.com/1590.shtml

关于作者: 智云科技

热门文章

网站地图