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

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

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

始める前に

  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.

    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. このガイドで既存のプロジェクトを使用する場合は、このガイドを完了するために必要な権限があることを確認します。新しいプロジェクトを作成した場合は、必要な権限がすでに付与されています。

  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. このガイドで既存のプロジェクトを使用する場合は、このガイドを完了するために必要な権限があることを確認します。新しいプロジェクトを作成した場合は、必要な権限がすでに付与されています。

  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 プラグインをインストールします(まだインストールしていない場合)。
  13. 必要なロール

    このクイックスタートを完了するために必要な権限を取得するには、プロジェクトに対する次の 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 を入力して確定し、[シャットダウン] をクリックします。

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

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

次のステップ