LoginSignup
6
5

More than 5 years have passed since last update.

GCPでビルド環境としてGitLab+Gitlab CI使おうとしてハマったポイント

Posted at

対象バージョン

  • docker-machine : 0.7.0
  • gitlab-runner : 1.2.0

何があったか

GCP上に立てたGitLabにあわせてせっかくクラウドでやるんだからオートスケールするビルド環境がいいぜ!って思いgitlab-ciの導入に踏み切りました。
そして動かしたら、待てど暮らせどインスタンスは作成されずビルドが始まりません。

gitlab-ci-multi-runnerの設定でexecutorをdocker+machineにします。

ログを確認しました。

Jun 10 17:20:32 gitlab-runner1 gitlab-ci-multi-runner[28551]: (runner-8d80da44-auto-scale-runners-1465546832-960a1434) Generating SSH Key
Jun 10 17:20:33 gitlab-runner1 gitlab-ci-multi-runner[28551]: (runner-8d80da44-auto-scale-runners-1465546832-960a1434) Creating host...
Jun 10 17:20:33 gitlab-runner1 gitlab-ci-multi-runner[28551]: (runner-8d80da44-auto-scale-runners-1465546832-960a1434) Opening firewall ports
Jun 10 17:20:33 gitlab-runner1 gitlab-ci-multi-runner[28551]: (runner-8d80da44-auto-scale-runners-1465546832-960a1434) Creating instance
Jun 10 17:20:35 gitlab-runner1 gitlab-ci-multi-runner[28551]: Error creating machine: Error in driver during machine creation: googleapi: Error 400: Invalid value for field 'resource.disks[0].initializeParams.sourceImage': '/de
bian-cloud/debian-8-jessie-v20160606'. The URL is malformed., invalid
Jun 10 17:20:35 gitlab-runner1 gitlab-ci-multi-runner[28551]: The default lines below are for a sh/bash shell, you can specify the shell you're using, with the --shell flag.
Jun 10 17:20:36 gitlab-runner1 gitlab-runner[28551]: Machine creation failed, trying to provision exit status 1

かなC。
じゃあちょっと調べましょうか。

なんでそうなったのか

gitlab-ci-multi-runnerの設定でexecutorをdocker+machineにするのですが、この時のパラメータに注意が必要です。

google-machine-imageこいつが鬼門でした。

なぜなら、ちょっと前まではGCPのデベロッパーコンソールでもインスタンス作成→コマンドラインで表示したらフルのイメージのURLが出ていたんですが、今は省略可能になっています。

gcloud sdkでインスタンス作るなら、これで問題ないはずです。(他のパラメータについては省略。)

gcloud compute instances create hoge --image debian-8-jessie-v20160606

インスタンス作成でdebian-8-jessie-v20160606を指定すれば普通にDebianのインスタンスできます。
これと同じノリでgitlab-runnerの設定もします。

MachineOptions = [
  "google-machine-image=debian-8-jessie-v20160606"
]

これで行きます。

The URL is malformed., invalid

お、おう…。
プロジェクト名と名前だけで指定できるようになって久しいので、フルなURLなんてそらで言えないよ…。

どうしたか

そういえばdocker-machineでイメージ指定しなかったら普通に作れたな。
てことはどっかに書いてあるな。

docker-machineのソースコード読んだらありました。

defaultImageName   = "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1510-wily-v20151114"

やったぜ。

こういうことですね。

https://www.googleapis.com/compute/v1/projects/ + プロジェクトID + /global/images/ + イメージ名

これでOK。

MachineOptions = [
  "google-machine-image=https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20160606",
  "google-scopes=https://www.googleapis.com/auth/cloud-platform",

ちなみに、スコープもURLで指定しないと怒られるので注意してください。
こっちはgcloud compute instances --helpとかすれば内容確認できます。

最後に

そのうちdocker-machineがバージョンアップしてできるようになるかもしれないですね。
てか、image-family使わせてください。

6
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5