GCPでDocker Imageの自動ビルド

Google Cloud PlatformのSource RepositoryにGitリポジトリを作成し、Container RegistryでDocker Imageの自動ビルドを設定して、ビルドしたイメージをpushするようにします。

ちょうどGitHubとDocker Hubのような関係です。

サンプルプロジェクト作成

今回自動ビルドするサンプルプロジェクトを作成します。

$ mkdir project
$ cd project
$ git init
$ vi Dockerfile
$ cat Dockerfile
FROM nginx:latest

RUN echo "This is my web page." > /usr/share/nginx/html/index.html
$ git config credential.helper gcloud.sh
$ git add Dockerfile
ishikura-mbp:project ishikura$ git commit -m "initial commit"
[master (root-commit) 49b04e5] initial commit
 1 file changed, 3 insertions(+)
 create mode 100644 Dockerfile

Gitリモートリポジトリ作成

GCP上にリポジトリを作成し、pushします。

$ gcloud source repos create test
Created [test].
You may be billed for this repository. See https://cloud.google.com/source-repositories/docs/pricing for details.

URL確認。

$ gcloud source repos list
REPO_NAME  PROJECT_ID  URL
test       uphy-test2  https://source.developers.google.com/p/uphy-test2/r/test

push。

$ git config credential.helper gcloud.sh
$ git push origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 296 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://source.developers.google.com/p/uphy-test2/r/test
 * [new branch]      master -> master

自動ビルド設定

先程作成したGitリポジトリからDocker Imageを自動ビルドするように設定します。 トリガーの作成から、Cloud Source Repositoryを選択して続行。

f:id:uphy:20170722225906p:plain

作成したリポジトリを選択し、続行。

f:id:uphy:20170722225849p:plain

適宜入力して、トリガーを作成。
写真では、タグをlatestに変えてしまいましたが、$COMMIT_SHAのままの方が良いです。latestのような固定文字列だと、Kubernetesの場合イメージの更新ができないようです。 f:id:uphy:20170722225913p:plainf:id:uphy:20170722225916p:plain

トリガーを実行をクリックして、最初のビルド。 f:id:uphy:20170722225919p:plain

以降はpushする度に自動でビルドされるようになります。

コンテナの更新

Container Registryにアクセスし、新しいイメージのタグを調べます。 f:id:uphy:20170722233627p:plain Pullコマンドを表示 f:id:uphy:20170722233631p:plain

gcr.io/uphy-test2/test:d597bf683642188790a6be1593b7947d4e72dd90
だとわかりました。 以下のコマンドで更新できます。

$ kubectl set image deployment/nginx nginx=gcr.io/uphy-test2/test:d597bf683642188790a6be1593b7947d4e72dd90
deployment "nginx" image updated

git pushでコンテナの更新まで行って欲しい。。引き続き調査。