werckerでCI

最近社内でwerckerを使ったプロジェクトが増えている様なので触ってみました。

werckerとは

DockerベースのCIサービス

devcenter.wercker.com

検証環境

リポジトリなど設定

Createよりリポジトリやアクセス設定など登録

f:id:yamamaijp:20160324184756p:plain

wercker.ymlを登録

今回はGo用のサンプルを利用 設定したリポジトリの直下にwercker.ymlを作成

box: golang
dev:
  steps:
    - internal/watch:
        code: |
          go build ./...
          ./source
        reload: true
# Build definition
build:
  # The steps that will be executed on build
  steps:

    # golint step!
    - wercker/golint

    # Build the project
    - script:
        name: go build
        code: |
          go build ./...

    # Test the project
    - script:
        name: go test
        code: |
          go test ./...

まだ何もないので失敗する

f:id:yamamaijp:20160324190119p:plain

ソースを追加してmasterにpushしたら今度は成功

f:id:yamamaijp:20160324201555p:plain

登録したサンプルソース

main.go

package main

import (
    "encoding/json"
    "log"
    "net/http"
)

func CityHandler(res http.ResponseWriter, req *http.Request) {
    data, _ := json.Marshal("{'cities':'San Francisco, Amsterdam, Berlin, New York','Tokyo'}")
    res.Header().Set("Content-Type", "application/json; charset=utf-8")
    res.Write(data)
}

func main() {
    http.HandleFunc("/cities.json", CityHandler)
    err := http.ListenAndServe(":5000", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

main_test.go

package main

import (
    "net/http"
    "net/http/httptest"
    "testing"
)

func TestHandleIndexReturnsWithStatusOK(t *testing.T) {
    request, _ := http.NewRequest("GET", "/", nil)
    response := httptest.NewRecorder()

    CityHandler(response, request)

    if response.Code != http.StatusOK {
        t.Fatalf("Non-expected status code%v:\n\tbody: %v", "200", response.Code)
    }
}

statusをGitHubで表示するように

werckerの →Settigns →Sharing →MarkdownをコピーしてGitHubのREADME.mdに追記してmasterにpush

ステータスが表示されるようになりました。 f:id:yamamaijp:20160324202033p:plain

まとめ

お手軽にCI環境を整えるのに便利だなと思いました。