クイックスタート: Git リポジトリから Cloud Run にデプロイする

このページでは、git リポジトリから Cloud Run への継続的デプロイの方法を説明します。


このタスクを Google Cloud コンソールで直接行う際の順を追ったガイダンスについては、「ガイドを表示」をクリックしてください。

ガイドを表示


このプロセスの詳しい手順については、Cloud Build を使用した Git からの継続的デプロイをご覧ください。

始める前に

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Make sure that you have the following role or roles on the project: Cloud Run Admin, Cloud Run Source Developer, Logs Viewer

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      IAM に移動
    2. プロジェクトを選択します。
    3. [ アクセスを許可] をクリックします。
    4. [新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。

    5. [ロールを選択] リストでロールを選択します。
    6. 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
    7. [保存] をクリックします。
  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Make sure that you have the following role or roles on the project: Cloud Run Admin, Cloud Run Source Developer, Logs Viewer

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      IAM に移動
    2. プロジェクトを選択します。
    3. [ アクセスを許可] をクリックします。
    4. [新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。

    5. [ロールを選択] リストでロールを選択します。
    6. 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
    7. [保存] をクリックします。
  8. Enable the Cloud Build API.

    Enable the API

  9. ドメイン制限の組織のポリシーでプロジェクトの未認証呼び出しが制限されている場合は、限定公開サービスのテストの説明に従って、デプロイされたサービスにアクセスする必要があります。

  10. サービス ID にサービス アカウント ユーザーのロールが付与されていることを確認します。デフォルトでは、サービス ID は Compute Engine のデフォルトのサービス アカウントです。

    ロールを付与する

    サービス ID リソースに対するアクセス権を付与するには、gcloud iam service-accounts add-iam-policy-binding コマンドを使用します。ハイライト表示された変数は、適切な値に置き換えてください。

          gcloud iam service-accounts add-iam-policy-binding SERVICE_ACCOUNT_EMAIL \
              --member="PRINCIPAL" \
              --role="roles/iam.serviceAccountUser"
          

    次のように置き換えます。

    • SERVICE_ACCOUNT_EMAIL: サービス ID として使用しているサービス アカウントのメールアドレス。たとえば次のようなものです。
      • Compute Engine のデフォルトのサービス アカウント: PROJECT_NUMBER-compute@developer.gserviceaccount.com
      • 作成したサービス アカウント: SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
    • PRINCIPAL: ユーザー ID。通常は、Google アカウントのメールアドレスです。
  11. Cloud Run の料金を確認するか、料金計算ツールで費用を見積もります。
  12. テンプレートからリポジトリを作成する

    リポジトリを作成するには:

    1. テンプレート リポジトリに移動します。

    2. [Use this template] をクリックします。

    3. [Create a new repository] を選択します。

    4. フォームにリポジトリ名を入力します。

    5. [Create repository from template] をクリックします。

    リポジトリからデプロイする

    リポジトリからデプロイする手順は次のとおりです。

    1. Google Cloud コンソールで、[Cloud Run] ページに移動します。

      [Cloud Run] に移動

    2. メニューから [サービス] を選択し、[コンテナをデプロイ] をクリックします。

    3. [サービスを作成] で、[リポジトリから継続的にデプロイする] オプションが選択されていることを確認します。

    4. フォームで [Cloud Build の設定] をクリックします。

    5. 右側のパネルで

      1. [リポジトリ] で、新しく作成したリポジトリを選択します。まだ認証していない場合は、認証が必要になることがあります。[リポジトリ プロバイダ] フィールドの [認証] リンクを使用します。

      2. GitHub と Google Cloud インタラクティビティに関する確認同意書を確認します。

      3. [次へ] をクリックします。

      4. [ビルドタイプ] で [Google Cloud Buildpacks] を選択します。

      5. [保存] をクリックします。

    6. [サービスの作成] フォームで、次の操作を行います。

      1. サービスの名前を確認します。リポジトリ名が自動的に入力されます。

      2. [リージョン] プルダウン メニューで、サービスを配置するリージョンを選択します。

      3. [認証] で、[公開アクセスを許可する] を選択します。これを選択する権限(Cloud Run 管理者ロール)がない場合は、サービスがデプロイされた後で認証が必要になります。

      4. [作成] をクリックしてサンプル リポジトリを Cloud Run にデプロイし、デプロイの完了を待ちます。

    7. 表示された URL リンクをクリックして、デプロイされたコンテナを実行します。

    Cloud Run のロケーション

    Cloud Run はリージョナルです。つまり、Cloud Run サービスを実行するインフラストラクチャは特定のリージョンに配置され、そのリージョン内のすべてのゾーンで冗長的に利用できるように Google によって管理されます。

    レイテンシ、可用性、耐久性の要件を満たしていることが、Cloud Run サービスを実行するリージョンを選択する際の主な判断材料になります。一般的には、ユーザーに最も近いリージョンを選択できますが、Cloud Run サービスで使用されている他の Google Cloudプロダクトのロケーションも考慮する必要があります。 Google Cloud プロダクトを複数のロケーションで使用すると、サービスのレイテンシだけでなく、コストにも影響を及ぼす可能性があります。

    Cloud Run は、次のリージョンで利用できます。

    ティア 1 料金を適用

    • asia-east1(台湾)
    • asia-northeast1(東京)
    • asia-northeast2(大阪)
    • asia-south1(ムンバイ、インド)
    • europe-north1(フィンランド) リーフアイコン 低 CO2
    • europe-north2(ストックホルム) リーフアイコン 低 CO2
    • europe-southwest1(マドリッド) リーフアイコン 低 CO2
    • europe-west1(ベルギー) リーフアイコン 低 CO2
    • europe-west4(オランダ) リーフアイコン 低 CO2
    • europe-west8(ミラノ)
    • europe-west9(パリ) リーフアイコン 低 CO2
    • me-west1(テルアビブ)
    • northamerica-south1(メキシコ)
    • us-central1(アイオワ) リーフアイコン 低 CO2
    • us-east1(サウスカロライナ)
    • us-east4(北バージニア)
    • us-east5(コロンバス)
    • us-south1(ダラス) リーフアイコン 低 CO2
    • us-west1(オレゴン) リーフアイコン 低 CO2

    ティア 2 料金を適用

    • africa-south1(ヨハネスブルグ)
    • asia-east2(香港)
    • asia-northeast3(ソウル、韓国)
    • asia-southeast1(シンガポール)
    • asia-southeast2 (ジャカルタ)
    • asia-south2(デリー、インド)
    • australia-southeast1(シドニー)
    • australia-southeast2(メルボルン)
    • europe-central2(ワルシャワ、ポーランド)
    • europe-west10(ベルリン) リーフアイコン 低 CO2
    • europe-west12(トリノ)
    • europe-west2(ロンドン、イギリス) リーフアイコン 低 CO2
    • europe-west3(フランクフルト、ドイツ)
    • europe-west6(チューリッヒ、スイス) リーフアイコン 低 CO2
    • me-central1(ドーハ)
    • me-central2(ダンマーム)
    • northamerica-northeast1(モントリオール) リーフアイコン 低 CO2
    • northamerica-northeast2(トロント) リーフアイコン 低 CO2
    • southamerica-east1(サンパウロ、ブラジル) リーフアイコン 低 CO2
    • southamerica-west1(サンティアゴ、チリ) リーフアイコン 低 CO2
    • us-west2(ロサンゼルス)
    • us-west3(ソルトレイクシティ)
    • us-west4(ラスベガス)

    Cloud Run サービスをすでに作成している場合は、Google Cloud コンソールの Cloud Run ダッシュボードにリージョンが表示されます。

    サービスを更新する

    サービスに変更を加えるには:

    1. commit をリポジトリのメインブランチに push します。

    2. Cloud Run サービスを更新して、更新のステータスを確認します。

    Cloud Run では需要に応じてコンテナ インスタンスが自動的にスケーリングされます。料金は、リクエストの処理中に使用した CPU、メモリ、ネットワークに対してのみ発生します。

    クリーンアップ

    Google Cloud アカウントに追加料金が課されるのを回避するには、このクイックスタートでデプロイしたリソースをすべて削除します。

    リポジトリを削除する

    デプロイされたサービスが使用されていない場合、Cloud Run の料金は発生しません。ただし、コンテナ イメージを Artifact Registry に保存するための料金が発生する可能性があります。Artifact Registry リポジトリを削除するには、Artifact Registry ドキュメントのリポジトリを削除するの手順を行います。

    サービスを削除する

    Cloud Run サービスの費用は、リクエストを受け取るまでは発生しません。Cloud Run サービスを削除するには、次のいずれかの操作を行います。

    コンソール

    サービスを削除するには:

    1. Google Cloud コンソールで Cloud Run に移動します。

      Cloud Run に移動

    2. 削除するサービスをサービスリストで探し、そのチェックボックスをクリックして選択します。

    3. [削除] をクリックします。これにより、サービスのすべてのリビジョンが削除されます。

    gcloud

    サービスを削除するには、次のコマンドを実行します。

    gcloud run services delete SERVICE --region REGION

    次のように置き換えます。

    • SERVICE: サービスの名前。
    • REGION: サービスの Google Cloud リージョン。

    テスト プロジェクトを削除する

    Google Cloud プロジェクトを削除すると、そのプロジェクト内のすべてのリソースに対する課金が停止します。プロジェクト内のすべての Google Cloud リソースを解放する手順は次のとおりです。

    1. In the Google Cloud console, go to the Manage resources page.

      Go to Manage resources

    2. In the project list, select the project that you want to delete, and then click Delete.
    3. In the dialog, type the project ID, and then click Shut down to delete the project.

    次のステップ