Go製ソフトウェアのCIやリリースをGithubActionsで行う


Go で書いた CLI ツールのリリースは GoReleaser と GitHub Actions で個人的には決まり を読んで、自分もどうやっているのか書いておこうと思ったので書く。

CIでテストを実行する

これはまあ、素朴にやる。テスト自体はMakefileに隠蔽しておくと手元でもCIと同じテストを実行できて便利。

.github/workflows/ci.yaml
--- name: CI on: [push] jobs: run_test: name: Go Test runs-on: ubuntu-latest steps: - name: Set up Go 1.13 uses: actions/setup-go@v1 with: go-version: 1.13 id: go - name: Check out code into the Go module directory uses: actions/checkout@v1 - name: Run Test run: | make test

タグを打ってリリースする

masterブランチに対してタグを作成すると、Actionsで自動的にリリースされるようにしている。

.github/workflows/release.yaml
--- name: Release on: create: tags: - v*.*.* branches: - master jobs: release: name: Release on GitHub runs-on: ubuntu-latest steps: - name: Check out code uses: actions/checkout@v1 - name: Validates GO releaser config uses: docker://goreleaser/goreleaser:latest with: args: check - name: Create release on GitHub uses: docker://goreleaser/goreleaser:latest with: args: release env: GITHUB_TOKEN: ${{secrets.GORELEASER_TOKEN}}

リリースにはGoReleaserを使っている。これはtakaishi/kelmの.goreleaser.yaml。バージョンの埋め込みなども行なっている。

.goreleaser.yaml
# This is an example goreleaser.yaml file with some sane defaults. # Make sure to check the documentation at http://goreleaser.com before: hooks: # you may remove this if you don't use vgo - go mod download # you may remove this if you don't need go generate - go generate ./... builds: - env: - CGO_ENABLED=0 ldflags: -X github.com/takaishi/kelm/config.Version={{.Version}} archive: replacements: darwin: Darwin linux: Linux windows: Windows amd64: x86_64 checksum: name_template: 'checksums.txt' snapshot: name_template: "{{ .Tag }}-next" changelog: sort: asc filters: exclude: - '^docs:' - '^test:' brew: github: owner: takaishi name: homebrew-fomulas commit_author: name: goreleaserbot email: goreleaser@carlosbecker.com description: "CLI tool to Generate and insert markdown's table of contents" homepage: "https://github.com/takaishi/kelm" install: | bin.install Dir['kelm'] test: | system "#{bin}/kelm"

このように、環境ごとのアーカイブファイルやチェックサム、チェンジログを作ってくれる。Homebrew用のfomulasも更新する機能があって便利。

コンテナイメージをビルドする

ソフトウェアによってはコンテナイメージをビルドしたいことがある。この場合も、ActionsでビルドしてDockerHubにプッシュしている。これはtakaishi/openstack-sg-controller の例。

.github/workflows/dockerimage.yaml
--- name: Docker Image CI on: push: tags: - v*.*.* env: DOCKER_HUB_TOKEN: ${{ secrets.DOCKER_HUB_TOKEN}} jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - name: Build the Docker image run: | tag=$(echo ${{ github.ref }} | sed 's/refs\/tags\///g' | tr -d '\n') docker login -u rtakaishi -p $DOCKER_HUB_TOKEN docker build . --file Dockerfile --tag rtakaishi/openstack-sg-controller:${tag} docker push rtakaishi/openstack-sg-controller:${tag}

タグの作成はlinyows/git-semvを使っている

linyows/git-semv というgit pluginがあって、これを使ってタグを作っている。major/minor/patchのタグ作成以外にもpre-releaseタグの作成ができたりして便利。

課題:リリース時にChangeLogをどう作るか

今の方法だとコミットログが羅列してあるだけなので、ちょっと読みにくいかなあと思っている。また、PullRequestがある場合はそちらの情報を表示したい。CHANGELOGファイルに自動追記されると変更を追いかけやすくて嬉しいと思うが、どうやるのがいいのか悩み中である。

Leave a Reply

Your email address will not be published. Required fields are marked *