GoReleaser と Travis CI を連携してみる #golang


前回の記事（[goreleaser を使って Github Releases へ簡単デプロイ](https://qiita.com/ynozue/items/f939cff562ec782b33f0)）で紹介した GoReleaser が便利だったので、Git の tag を push したタイミングで、GitHub Release に自動（Travis CI）でデプロイされるようにしてみました。

# GoReleaser と Travis CI を連携
## イメージ
![goreleaser.png](https://qiita-image-store.s3.amazonaws.com/0/203864/208322ab-6694-5d11-1ed3-c39e6a06ef26.png "goreleaser.png")
※ GitHub と Travis CI がどういう仕組みで動いてるかあまり詳しくなかったので適当にイメージして描いてます

## Travis CI の設定に GoReleaser を追加
以下、変更点になります。

* install する際に goreleaser を追加
* script が成功した場合に goreleaser を実行

```yml:.travis.yml
language: go
  
install:
 - go get -u golang.org/x/tools/cmd/goimports
 - go get -u github.com/golang/lint/golint
 - go get -u github.com/goreleaser/goreleaser

script:
 - go vet ./...
 - diff <(goimports -d .) <(printf "")
 - diff <(golint ./...) <(printf "")
 - go test .

after_success:
 - goreleaser
```

## tag を push
```sh:
$ git tag v1.0.2
$ git push --tags
Total 0 (delta 0), reused 0 (delta 0)
To github.com:ynozue/apichecker.git
 * [new tag]         v1.0.2 -> v1.0.2
```

https://travis-ci.org/ の実行ログを見るといい感じで実行されていました。

```sh:
$ go get -u golang.org/x/tools/cmd/goimports
$ go get -u github.com/golang/lint/golint
$ go get -u github.com/goreleaser/goreleaser
$ go vet ./...
The command "go vet ./..." exited with 0.
0.01s$ diff <(goimports -d .) <(printf "")
The command "diff <(goimports -d .) <(printf "")" exited with 0.
0.03s$ diff <(golint ./...) <(printf "")
The command "diff <(golint ./...) <(printf "")" exited with 0.
3.62s$ go test .
ok  	github.com/ynozue/apichecker	3.186s
The command "go test ." exited with 0.
after_success
$ goreleaser
   • running goreleaser dev   
   • loading config file       file=.goreleaser.yml
   • SETTING DEFAULTS 
   • GETTING AND VALIDATING GIT STATE
   • releasing v1.0.2, commit dc485a54a76189a106a154a85d8b5999cfdb61e5
   • GENERATING CHANGELOG
   • LOADING ENVIRONMENT VARIABLES
   • CHECKING ./DIST  
   • BUILDING BINARIES
   • building                  binary=dist/apichecker_1.0.2_linux_amd64/apichecker
   • building                  binary=dist/apichecker_1.0.2_darwin_amd64/apichecker
   • building                  binary=dist/apichecker_1.0.2_windows_amd64/apichecker.exe
   • CREATING ARCHIVES
   • creating                  archive=dist/apichecker_1.0.2_windows_amd64.tar.gz
   • creating                  archive=dist/apichecker_1.0.2_linux_amd64.tar.gz
   • creating                  archive=dist/apichecker_1.0.2_darwin_amd64.tar.gz
   • new release artifact      artifact=apichecker_1.0.2_windows_amd64.tar.gz
   • new release artifact      artifact=apichecker_1.0.2_linux_amd64.tar.gz
   • new release artifact      artifact=apichecker_1.0.2_darwin_amd64.tar.gz
   • CREATING LINUX PACKAGES WITH FPM
   • skipped                   reason=no output formats configured
   • CREATING LINUX PACKAGES WITH SNAPCRAFT
   • skipped                   reason=no summary nor description were provided
   • CALCULATING CHECKSUMS
   • checksumming              file=apichecker_1.0.2_darwin_amd64.tar.gz
   • checksumming              file=apichecker_1.0.2_windows_amd64.tar.gz
   • checksumming              file=apichecker_1.0.2_linux_amd64.tar.gz
   • new release artifact      artifact=apichecker_1.0.2_checksums.txt
   • CREATING DOCKER IMAGES
   • skipped                   reason=docker section is not configured
   • RELEASING TO GITHUB
   • creating or updating release repo=ynozue/apichecker tag=v1.0.2
   • release updated           url=https://github.com/ynozue/apichecker/releases/tag/v1.0.2
   • uploading to release      file=dist/apichecker_1.0.2_checksums.txt name=apichecker_1.0.2_checksums.txt
   • uploading to release      file=dist/apichecker_1.0.2_linux_amd64.tar.gz name=apichecker_1.0.2_linux_amd64.tar.gz
   • uploading to release      file=dist/apichecker_1.0.2_windows_amd64.tar.gz name=apichecker_1.0.2_windows_amd64.tar.gz
   • uploading to release      file=dist/apichecker_1.0.2_darwin_amd64.tar.gz name=apichecker_1.0.2_darwin_amd64.tar.gz
   • CREATING HOMEBREW FORMULA
   • skipped                   reason=brew section is not configured
   • SUCCESS!         
Done. Your build exited with 0.
```

Git Releases にも問題なくデプロイされました。
![Kobito.reIkQg.png](https://qiita-image-store.s3.amazonaws.com/0/203864/c7ba4965-b430-1a10-e8ba-5c532a1350a4.png "Kobito.reIkQg.png")

バイナリをタグを打つごとに自動的に作って Git Releases にデプロイされるのは便利ですね。

# Appendix
* [ソースコード](https://github.com/ynozue/apichecker/tree/goreleaserci)