ハードウェアの気になるあれこれ

技術的に興味のあることを調べて書いてくブログ。主にハードウェアがネタ。

GitLab Runnerを使ったChiselの自動テスト環境の構築

スポンサーリンク

技書博向けの作業において、サンプルコードの自動テスト環境を作りたいなーと考えていて、それを構築するにはGitLabとGitLab Runnerを使用するのが良さそうに見えたので試してみた。

GitLab と GitLab Runnerを試す

幾つかインストールの方法はあるけど、今回は以下の構成で進めます。

  • GitLab : 普通にGitLabを使う(https://gitlab.com/
  • GitLab Runner : 自分のPC上にDockerを使って構築
    • Dockerは既にインストール済み

Dockerを使ってGitLab Runnerをインストール

これは公式のページでしっかりを説明してくれているので、この説明に従って進めた。

Run GitLab Runner in a container

GitLab Runnerはdocker hubに既にイメージがあるので、それを使えばOKなのだがその際に幾つか設定が必要で以下のようなコマンドになる。

docker run -d --name gitlab-runner --restart always \
  -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest

実行時には勝手にdocker hubのイメージが引っ張られて起動する。

$ docker run -d --name gitlab-runner --restart always \
>   -v /srv/gitlab-runner/config:/etc/gitlab-runner \
>   -v /var/run/docker.sock:/var/run/docker.sock \
>   gitlab/gitlab-runner:latest
Unable to find image 'gitlab/gitlab-runner:latest' locally
latest: Pulling from gitlab/gitlab-runner
5667fdb72017: Pull complete
d83811f270d5: Pull complete
ee671aafb583: Pull complete
7fc152dfb3a6: Pull complete
961bd0a4566b: Pull complete
90dcb1002cb9: Pull complete
c967f5522245: Pull complete
09fe988779de: Pull complete
e63696361b2c: Pull complete
Digest: sha256:c421610d4a591604c1d211b1edc19d1b57a56ce9f70de27d6e71e66fa6f1cf24
Status: Downloaded newer image for gitlab/gitlab-runner:latest
b3e339452f2cdf1a8c72bce75bbd1bce0e499e4b337caeadfd47e6d9d2a92f41

起動していることを確認。

$ docker ps
CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS              PORTS               NAMES
b3e339452f2c        gitlab/gitlab-runner:latest   "/usr/bin/dumb-init …"   2 hours ago         Up 2 hours                              gitlab-runner

適当なプロジェクトで動作を確認

Docker上でのGitLab Runnerの起動が確認できたら、適当なリポジトリをGitLabに作成する。 このプロジェクトに自分のPC上で動作しているGitLab Runnerを登録すると自動テストが可能になる。

作成したらプロジェクトのページの左側のメニューの"Settings"から"CI/CDを選択し、表示されたページの真ん中あたりにある"Runner"の項目を展開する。

開くと以下の画像のようになっているはずだ。

f:id:diningyo-kpuku-jougeki:20191012195604p:plain

ここにある"Set up a specific Runner manually"に書いてあるURLとトークンが登録に必要になる。

また実行時に意図しないRunnerで実行されることがあったので、この時についでに"Shared Runners"の設定を"Disable"にしておこう。

f:id:diningyo-kpuku-jougeki:20191012195615p:plain

作成したDocker上でgitlab-runnerコマンドを使用して設定が行える。引数とかをちゃんと把握してればdockerコマンドにgitlab-runnerコマンドを渡して実行して登録することも可能なのだが、あまりきちんと調べていないのでGitLab Runnerが動作しているコンテナに入って直接gitlab-runnerを実行した。

$ docker exec -it gitlab-runner bash
root@b3e339452f2c:/#

上記のようにコンテナ内に入りbashが起動したのを確認したら以下のようにgitlab-runnerregisterというサブコマンドを付けて実行する。

root@b3e339452f2c:/# gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=54 revision=a8a019e0 version=12.3.0
Running in system-mode.

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
https://gitlab.com/ <- ここに連携したいGitLabのURLを指定(今回はそのままgitlab.com)
Please enter the gitlab-ci token for this runner:
xxxxxxxxxxxxxxxxxxxx <- ここにトークンを指定
Please enter the gitlab-ci description for this runner:
[b3e339452f2c]: diningyo-test-shell-runner
Please enter the gitlab-ci tags for this runner (comma separated):
<ここにタグを登録すると、指定したタグにのみ反応するように出来る(らしいのだが、よくわかってない)>
Registering runner... succeeded                     runner=gAq5Dams
Please enter the executor: docker-ssh, shell, ssh, docker-ssh+machine, kubernetes, custom, docker, docker+machine, parallels, virtualbox:
<ここは実行する際の環境を指定する>
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

executorの部分は自動テストをどの環境で実行するかを選択できるようだ。shellにするとgitlab-runnerがインストールされている環境をそのまま使用する形になる。今回のケースだとGitLab Runner用のDocker上で自動実行される感じ。 dockerを指定しておくと自動テストの実行時に指定したDockerイメージを構築し、そのコンテナ上で実行されるようになる。

とりあえずお試しということでBashのサンプルを実行。プロジェクトのページにある"Set up CI/CD"(画像では違うけど)を押すと、そのままWeb IDEが開き自動テスト用のYAMLファイルを作成&編集することが出来る。

f:id:diningyo-kpuku-jougeki:20191012195629p:plain

f:id:diningyo-kpuku-jougeki:20191012195705p:plain

"Apply a GitLab CI Yaml template"を開くと"Bash"があるのでそれを選択して"Commit changes"を押す。GitLab Runnerとの連携がうまくできていればこれで自動実行が開始されるはずだ。

CIの状況を確認するには左側のメニューから"CI/CD"の開けばOK。以下の画像のように"running"の表示があれば自動実行がうまく動いている。

f:id:diningyo-kpuku-jougeki:20191012195718p:plain

GitLab と GitLab RunnerでChiselのCI環境

続いて本題のChiselのCI環境を構築しよう。

こちらはGitLab Runnerが動いている環境上でChiselが実行できればそれでOKになる。GitLab Runner自体がDockerで切り離されている状態なので、コンテナ内にChiselの環境を構築してもいいしDockerのイメージを実行に使うことも可能だ。

GitLab Runnerが動作するコンテナ内にChisel環境を構築

まずはGitLab Runnerのコンテナ内にChisel環境を構築してみる。ということで再度コンテナ内に入る。

docker exec -it gitlab-runner bash

コンテナ内にはGitLab Runnerを動かすためのものしか入っていないので、色々インストールしていく。必要になるのは以下。

  • Java
  • scala
  • sbt
  • verilator : 使用する場合は以下も必要になる。

GitLab Runnerのイメージ上ではaptコマンドが使えるので大体はそれで入る。

apt update
apt install openjdk-8-jdk scala verilator make gcc g++

sbtのみapt updateを実行しても見つからなかったので、これのみ手動で設定した。

cd /usr/local/src
mkdir sbt
cd sbt
wget https://piccolo.link/sbt-1.3.2.tgz
tar xf sbt-1.3.2.tgz
cd /usr/local/bin
ln -s /usr/local/src/sbt/sbt/bin/sbt
ln -s /usr/local/src/sbt/sbt/bin/sbt-launch.jar

ここまで設定すればsbtが動くはずなのでコンテナから抜けてOK。

2019/10/13追記

Chiselの公式の環境構築を見ていたらsbtは設定すればaptで入ると書いてあったので、それも追加。

echo "deb https://dl.bintray.com/sbt/debian /" | sudo tee -a /etc/apt/sources.list.d/sbt.list
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 642AC823
sudo apt update
sudo apt install sbt

Chiselの自動テストを実行してみる

Github Actionsの時と同様に適当なリポジトリにchisel-templateを展開して実行する。そのへんの手順は一緒なので前回の記事をどうぞ。

chisel-tempalateの展開が終わったら、CIの設定ファイルを作成する。 以下のような内容のファイルを先ほどのお試しの時と同様に".gitlab-ci.yml"に記載しcommit&pushする。

  • /.gitlab-ci.yml
# This file is a template, and might need editing before it works on your project.
# see https://docs.gitlab.com/ce/ci/yaml/README.html for all available options

# you can delete this line if you're not using Docker
# image: busybox:latest

test:
  stage: test
  script:
    - sbt "test"

設定がうまく行けば以下のように"CI/CD"の"Pipelines"のページで以下のように自動テストが実行されるはずだ。

f:id:diningyo-kpuku-jougeki:20191012195733p:plain

f:id:diningyo-kpuku-jougeki:20191012195746p:plain

結果は以下のようにChiselのテストが実行され"passed"となった。

f:id:diningyo-kpuku-jougeki:20191012195756p:plain

chisel3-toolsを使ってみる

ツイッター教えてもらった"chisel3-tools"を使った実行についても試してみた。

まずはGitLab Runnerの設定で"executor"を"docker"にして再度登録を行う。

gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=6188 revision=a8a019e0 version=12.3.0
Running in system-mode.

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
https://gitlab.com/
Please enter the gitlab-ci token for this runner:
qrwD9K5tU-sY35kMCpUE
Please enter the gitlab-ci description for this runner:
[b3e339452f2c]: diningyo-chisel-runner
Please enter the gitlab-ci tags for this runner (comma separated):

Registering runner... succeeded                     runner=qrwD9K5t
Please enter the executor: virtualbox, docker+machine, docker-ssh, parallels, ssh, docker-ssh+machine, kubernetes, custom, docker, shell:
docker # ここをdockerにして登録
Please enter the default Docker image (e.g. ruby:2.6):
ucbbar/chisel3-tools # デフォルトのdockerイメージにdocker hubのchisel3-toolsを指定

うまく設定が出来ていれば以下の画面のように最初のDockerイメージのpullが行われて、その後に実行が行われるようになる。

f:id:diningyo-kpuku-jougeki:20191012195805p:plain

こっちはDockerのイメージを使いやすくて楽だな。