Cloud Code for VS Code を使用して Cloud Run サービスをデプロイする

このドキュメントでは、Cloud Code の使用を開始する方法について説明します。

サンプル「Hello, world」アプリケーションからサービスを作成し、Cloud Code を使用してサービスを一般公開し、実際のサービスのリソースのステータスを表示します。

始める前に

  1. アカウントにログインします。 Google Cloud を初めて使用する場合は、 アカウントを作成して、実際のシナリオで Google プロダクトのパフォーマンスを評価してください。 Google Cloud新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $300 分を差し上げます。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  5. Enable the Cloud Run API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

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

  9. Enable the Cloud Run API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  10. Git をインストールします。 サンプルをマシンにコピーするには Git が必要です。
  11. Docker をインストールします。 サンプルコードをビルドするには Docker が必要です。ローカル ユーザー アカウント でコンテナをビルドできることを確認します。
  12. Cloud Code プラグイン をインストールします(まだインストールしていない場合)。

必要なロール

このクイックスタートを完了するために必要な権限を取得するには、プロジェクトに対する次の IAM ロールを付与するよう管理者に依頼してください。

ロールの付与については、プロジェクト、フォルダ、組織へのアクセス権の管理をご覧ください。

必要な権限は、カスタム ロールや他の事前定義 ロールから取得することもできます。

サービスの作成

サンプル サービス アプリケーションを作成する手順は次のとおりです。

  1. Cloud Code ステータスバーで、アクティブなプロジェクト名をクリックします。

    ステータスバーでアクティブなプロジェクト名

  2. 表示されたクイック選択メニューで、[新しいアプリケーション] を選択します。

  3. アプリケーション タイプとして [Cloud Run application] を選択します。

  4. サンプル アプリケーションのリストから、使用する言語のものを選択します。

    たとえば、[Node.js: Cloud Run] を選択すると、Node.js で「Hello, world」サービス アプリケーションが作成されます。

  5. サービスのソースコードのファイル システムの場所を確認し(必要に応じて変更)、[Create New Application] をクリックします。

    選択したサービスの README.md ファイルのプレビューが VS Code で開きます。

Cloud Run へのデプロイ

サービスを Cloud Run にデプロイするには、次の手順に沿って操作します。

  1. コマンド パレットを開きます(Ctrl/Cmd+Shift+P を押下するか、[表示] > [コマンド パレット] をクリック)。その後、[Deploy to Cloud Run] コマンドを実行します。

  2. [Cloud Run にデプロイする] ペインで、 Google Cloud プロジェクトを設定します。

  3. 残りの設定はデフォルト値のままにして、[Deploy] をクリックします。

    Cloud Code がイメージをビルドしてレジストリにプッシュし、サービスを Cloud Run にデプロイします。

  4. 実行中のサービスを表示するには、[Cloud Run へのデプロイ] ダイアログの上部に表示される URL を開きます。

リソースのステータスの表示

Cloud Run サービスのブラウザを使用して、Cloud Run サービスのステータスを表示できます。

  1. VS Code アクティビティ バーで [Cloud Code] をクリックします。
  2. [Cloud Run] セクションを開いて、Cloud Run サービスを表示します。
  3. サービスをクリックして、そのプロパティを表示します。

ログを表示

Cloud Code に付属のログビューアを使用して、デプロイしたサービスのログを表示することもできます。

ログを表示するには、次の手順に沿って操作します。

  1. VS Code アクティビティ バーで [Cloud Code] をクリックします。
  2. [Cloud Run] セクションを開いて、Cloud Run サービスを表示します。
  3. Cloud Run サービスを右クリックし、つづいて [ログを表示] をクリックします。

クリーンアップ

プロジェクトを削除するには、次のようにします。

  1. コンソールのプロジェクト ページに移動します。 Google Cloud

    プロジェクト ページに移動

  2. このクイックスタート用に作成したプロジェクトを選択し、[削除] をクリックします。

  3. プロジェクト ID を入力して確定し、[シャットダウン] をクリックします。

    プロジェクトがシャットダウンし、プロジェクト削除のスケジュールが決定されます。

既存のプロジェクトを使用していて、このクイックスタート用に作成したリソースを削除する場合は、次の方法で削除できます。

  • コンテナ イメージ: サービスが使用されていない場合、Cloud Run の料金は発生しませんが、コンテナ イメージを Artifact Registry に保存することで課金される場合があります。イメージを削除する手順については、 イメージの削除をご覧ください。

  • Cloud Run サービス: Cloud Run サービスを削除するには、ナビゲーション メニューを使用して Google Cloud コンソールで Cloud Run に移動します。削除するサービスを選択して、[削除] をクリックします。

次のステップ