LoginSignup
9

More than 5 years have passed since last update.

Google Cloud Build覚え書き

Last updated at Posted at 2018-11-28

Screenshot from 2018-11-27 16-41-25.png

Google Cloud Buildとは、

  • 1日120分まで無料
  • 1ステップ1Docker(コンテナ)

、、、いきなり1ステップ1Dockerといわれると:thinking::thinking::thinking:となってしまうのですが、設定ファイルをみると

cloudbuild.yaml
steps:
- name: 'gcr.io/cloud-builders/npm'
  args: ['install']
- name: 'gcr.io/cloud-builders/npm'
  args: ['run', 'build']

どうやらTravisCircleCIのようにOSイメージやシェルスクリプトを書くのではなく、ビルドの1ステップ毎にnameでコンテナを指定していくとのこと。

gcr.io/cloud-builders/npmでnpmを使用し、他にこちらの一覧にはすぐに使えるOfficialコマンドや、自分のプロジェクトで作成するCommunityコマンドが用意されてます。

⚡クイックスタート⚡

  1. GCPの適当なプロジェクトでCloud BuildのAPIを有効化
  2. Cloud Shellにアクセスして、
  3. 設定ファイルを作成し、ビルドコマンドを実行するだけ
# (必要であれば) 使用プロジェクトの設定
gcloud projects list
gcloud config set project [PROJECT_ID]

# 作業ディレクトリの作成
mkdir ~/gcb-test && cd $_

# 設定ファイルの作成
cat << EOF > cloudbuild.yaml
steps:
- name: 'gcr.io/cloud-builders/npm'
  args: ['--version']
EOF

# ビルドコマンドの実行
gcloud builds submit

:desktop: 出力結果 >

Creating temporary tarball archive of 1 file(s) totalling 65 bytes before compression.
Uploading tarball of [.] to [gs://***.tgz]
Created [https://cloudbuild.googleapis.com/v1/projects/***].
Logs are available at [https://console.cloud.google.com/gcr/builds/***].
---------------- REMOTE BUILD OUTPUT ------------------
starting build "***-***-***"

FETCHSOURCE
Fetching storage object: gs://***...
Copying gs://***...
/ [1 files][  195.0 B/  195.0 B]
Operation completed over 1 objects/195.0 B.
BUILD
Already have image (with digest): gcr.io/cloud-builders/npm
6.4.1
PUSH
DONE
--------------------------------------------------------

ID   CREATE_TIME                DURATION  SOURCE        IMAGES  STATUS
***  2018-11-27T07:16:04+00:00  7S        gs://***.tgz  -       SUCCESS

:desktop: コンソールに表示される履歴 >

Screenshot from 2018-11-27 17-06-51.png

手軽に使えて便利そうです!:star2::star2::star2:

独自コマンドの作成

基本npm buildでなんでもできそうですが、例えばpolymer-cliを直接使いたい時は、

# 作業ディレクトリの作成
mkdir ~/gcb-polymer-cli && cd $_

# Dockerの定義ファイル作成
cat << EOF > Dockerfile
FROM gcr.io/cloud-builders/npm

RUN npm install polymer-cli -g --unsafe-perm

ENTRYPOINT ["polymer"]
EOF

# Cloud Buildの設定ファイルの作成
cat << EOF > cloudbuild.yaml
steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/\$PROJECT_ID/polymer', '.']
images:
- 'gcr.io/\$PROJECT_ID/polymer'
EOF

# ビルドの実行
gcloud builds submit

でコマンドがプロジェクトに登録されるので、

cloudbuild.yaml
steps:
- name: 'gcr.io/$PROJECT_ID/polymer'
  args: ['--version']

として使うことができます。

参考URL

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
9