5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Travis CI + Docker + go test

この記事はmediba Advent Calendar 2016の10日目です。

@mediba-moritake です。auスマートパスのサーバーサイド開発をしています。
今回はTravis CIのDockerを使って、go testを実行したいと思います。

docker-compose.yml
version: '2'
services:
  golang:
    container_name: golang_container
    build: ./docker/golang
    volumes:
      - ./:/go/src/github.com/mediba-moritake/docker-golang
Dockerfile
FROM golang:1.7.4-alpine

RUN apk update

WORKDIR /go/src/github.com/mediba-moritake/docker-golang

CMD ["/sbin/init"]
main.go
package main

import (
	"fmt"
	"github.com/mediba-moritake/docker-golang/hello/lib"
)

func main() {
	hello := lib.Hello{Language: "Golang"}
	fmt.Println(hello.Hello())
}
hello.go
package lib

type Hello struct {
	Language string
}

func (f *Hello) Hello() string {
	return "Hello, " + f.Language

}
hello_test.go
package lib

import "testing"

func TestHello(t *testing.T) {
	hello := Hello{Language: "Golang"}

	if hello.Hello() != "Hello, Golang" {
		t.Fatal("Hello() doesn't match")
	}
}
.travis.yml
sudo: required

services:
  - docker

before_install:
  - docker-compose up -d

install:

before_script:

script:
  - docker-compose exec golang go test -v ./...

after_script:

notifications:

最後に

  • go testはDockerを使わなくても実行可能です。
  • 1つのGitHubリポジトリに複数言語コミットしている場合(Golang 1.7.4とRuby 2.3.3など、複数言語のバージョン指定)に便利です。
  • Docker for Macなどでローカル開発環境を構築すると、そのままCI環境で使えるのが便利ですね。

今回使用したソースコードはこちらです。

明日の11日目は@kitatubaさんの「性能試験おコンテナ」です。

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?