GitHub ActionsでGo言語のテストを動かしてみた

はじめに

GitHub Actionsのβテストに選ばれたので、正月休みを利用して試してみました。何をさせようかちょっと考えたのですが、ちょうど『Go言語でつくるインタプリタ』を読みすすめているので、そのテストコードを実行するようにしてみました。

Go言語でつくるインタプリタ

Go言語でつくるインタプリタ

はじめてのGitHub Actions

ドキュメントが公開されているので、それをもとに実行してみます。

developer.github.com

今回はGoコマンドのバージョンを出力するようにしてみます。

.github/main.workflowに以下の内容を記してGitHubリポジトリにpushします。

workflow "Test" {
  on = "push"
  resolves = ["test"]
}

action "test" {
  uses = "docker://golang:latest"
  runs = "go version"
}

すると、GitHubリポジトリにあるActionsタブに以下のような画面が表示されます。

f:id:miyohide:20190101193353p:plain

「Log」をクリックすると以下のようなログが出力されます。

### STARTED test 07:11:08Z

Pulling image: gcr.io/github-actions-images/action-runner:latest
latest: Pulling from github-actions-images/action-runner
4fe2ade4980c: Already exists
86eb2eb12b87: Pulling fs layer
3c0e0e9cbe16: Pulling fs layer
0dff81d7fe33: Pulling fs layer
3c0e0e9cbe16: Verifying Checksum
3c0e0e9cbe16: Download complete
0dff81d7fe33: Verifying Checksum
0dff81d7fe33: Download complete
86eb2eb12b87: Verifying Checksum
86eb2eb12b87: Download complete
86eb2eb12b87: Pull complete
3c0e0e9cbe16: Pull complete
0dff81d7fe33: Pull complete
Digest: sha256:2b563779b8eb5c34114dce5f547ef0e8506a5701123819159a3ae1b319f4f80e
Status: Downloaded newer image for gcr.io/github-actions-images/action-runner:latest
Unable to find image 'golang:latest' locally
latest: Pulling from library/golang
Digest: sha256:7866a3428b229f9bb8aeaa437ed5cc3a4e5bf0b7c2ac534598fb6e40b6c6088d
Status: Downloaded newer image for golang:latest
go version go1.11.4 linux/amd64

### COMPLETED test 07:11:13Z (5.365s)

見にくいですが、

go version go1.11.4 linux/amd64

が表示され、Goコマンドのバージョンが出力されていることがわかります。

テストを実行する

さて、当初の目的であるテストの実行をしてみます。DevelopersIOの以下の記事を参考にDockerfileなどを作って実行してみました。

dev.classmethod.jp

最終的な設定は以下の通り。

まずは.github/main.workflowusesの部分を自分が作成したDockerfileなどをおいているパスに変更しています。

workflow "Test" {
  on = "push"
  resolves = ["test"]
}

action "test" {
  uses = "./.github/actions/golang"
}

.github/actions/golang/Dockerfileは以下の通り。

FROM golang:latest
 
COPY entrypoint.sh /entrypoint.sh
 
ENTRYPOINT ["/entrypoint.sh"]

最後に.github/actions/golang/entrypoint.shリポジトリ名をmiyohide-monkeyにしていたのにソースの中のpackage名はmonkeyにしていたので当初うまく動かずに悩みましたが、APP_DIRを直接指定することで動かすことができました。

#!/bin/bash
 
APP_DIR="/go/src/github.com/miyohide/monkey/"
 
mkdir -p ${APP_DIR} && cp -r ./ ${APP_DIR} && cd ${APP_DIR}
 
echo "#######################"
echo "# Running Test"
go test ./...

実行ログは以下の通り。

### STARTED test 13:13:05Z

Already have image (with digest): gcr.io/cloud-builders/docker
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855: Pulling from gct-12-whr4wtzjyryvx9olfja8cwy/e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855/8cf18bd4dc69e0f0e1f4b7a018b1b229495786905f46383be02cfeda9cc0c33b
cd8eada9c7bb: Already exists
c2677faec825: Already exists
fcce419a96b1: Already exists
045b51e26e75: Already exists
70a7003e9fe9: Already exists
751b6f060321: Already exists
462a907acb44: Already exists
6592a1601888: Pulling fs layer
6592a1601888: Verifying Checksum
6592a1601888: Download complete
6592a1601888: Pull complete
Digest: sha256:ab4db72476723a12a47a5c13c618ed8284b1444840b54e74a156ab01ab2da208
Status: Downloaded newer image for gcr.io/gct-12-whr4wtzjyryvx9olfja8cwy/e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855/8cf18bd4dc69e0f0e1f4b7a018b1b229495786905f46383be02cfeda9cc0c33b:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
Already have image (with digest): gcr.io/cloud-builders/docker
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM golang:latest
latest: Pulling from library/golang
Digest: sha256:7866a3428b229f9bb8aeaa437ed5cc3a4e5bf0b7c2ac534598fb6e40b6c6088d
Status: Downloaded newer image for golang:latest
 ---> 343df9d12b7b
Step 2/3 : COPY entrypoint.sh /entrypoint.sh
 ---> e8e67d9de9a7
Step 3/3 : ENTRYPOINT ["/entrypoint.sh"]
 ---> Running in f3333c74336c
Removing intermediate container f3333c74336c
 ---> e38fd33cb0bf
Successfully built e38fd33cb0bf
Successfully tagged gcr.io/gct-12-whr4wtzjyryvx9olfja8cwy/e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855/8cf18bd4dc69e0f0e1f4b7a018b1b229495786905f46383be02cfeda9cc0c33b:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
Pulling image: gcr.io/github-actions-images/action-runner:latest
latest: Pulling from github-actions-images/action-runner
4fe2ade4980c: Already exists
86eb2eb12b87: Pulling fs layer
3c0e0e9cbe16: Pulling fs layer
0dff81d7fe33: Pulling fs layer
86eb2eb12b87: Verifying Checksum
86eb2eb12b87: Download complete
0dff81d7fe33: Verifying Checksum
0dff81d7fe33: Download complete
3c0e0e9cbe16: Verifying Checksum
3c0e0e9cbe16: Download complete
86eb2eb12b87: Pull complete
3c0e0e9cbe16: Pull complete
0dff81d7fe33: Pull complete
Digest: sha256:2b563779b8eb5c34114dce5f547ef0e8506a5701123819159a3ae1b319f4f80e
Status: Downloaded newer image for gcr.io/github-actions-images/action-runner:latest
#######################
# Running Test
?       github.com/miyohide/monkey  [no test files]
ok      github.com/miyohide/monkey/ast  0.002s
ok      github.com/miyohide/monkey/evaluator    0.003s
ok      github.com/miyohide/monkey/lexer    0.002s
ok      github.com/miyohide/monkey/object   0.002s
ok      github.com/miyohide/monkey/parser   0.002s
?       github.com/miyohide/monkey/repl [no test files]
?       github.com/miyohide/monkey/token    [no test files]

### COMPLETED test 13:13:26Z (21.068s)

Running Test以下にGo言語のテストが実行されていることがわかります。

まとめ

取り急ぎ動かしてみたっていう感じでブログエントリを書いてみました。

ここまではTravisやCircleCIとやっていることは一緒なので、もっとGitHub Actionsならではのことをやってみたいです。