LoginSignup
2

More than 5 years have passed since last update.

posted at

updated at

Organization

Travis CI + Docker + go test

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さんの「性能試験おコンテナ」です。

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
What you can do with signing up
2