使用 Cloud Code for VS Code 建立及部署函式

本頁面說明如何使用 Cloud Code 搭配 Cloud Functions 建立、下載、部署及叫用第 2 代 HTTP 函式。

事前準備

  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. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, Logging, and Pub/Sub APIs.

    Enable the APIs

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

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, Logging, and Pub/Sub APIs.

    Enable the APIs

  8. 安裝 Git。您必須使用 Git,才能將範例複製到電腦。
  9. 如果尚未安裝 Cloud Code 外掛程式,請先安裝。
  10. 建立函式

    如果您之前已建立函式,可以將現有函式下載至新的工作區,並在本機編輯該函式。

    如要使用範例建立新函式,請按照下列步驟操作:

    1. 按一下 「Cloud Code」,然後展開「Cloud Functions」專區。

    2. 按一下「新增」「建立函式」,然後選取範本。

    3. 前���要建立新函式的路���,���入函式名稱,然後選取「建立新應用程式」

      如果應用程式資料夾未自動顯示在「Explorer」中,請按一下「Refresh」

      launch.json 檔案包含函式所需的設定:

      {
        "configurations": [
          {
            "name": "Deploy Cloud Function",
            "type": "cloudcode.cloudfunctions",
            "request": "launch",
            "functionName": "function-hello-world",
            "gen": "GEN_2",
            "entryPoint": "helloHttp",
          }
        ]
      }
      

    如要進一步瞭解如何在單一工作區中建立多個函式,請參閱「在 VS Code 工作區中管理函式」。

    將現有函式下載至新的工作區

    如要將函式下載至新的工作區,請按照下列步驟操作:

    1. 按一下 「Cloud Code」,然後展開「Cloud Functions」專區。

    2. 展開 Google Cloud 專案程式碼片段,在要下載的函式上按一下滑鼠右鍵,然後選取「Download to new workspace」(下載至新的工作區)

    3. 出現提示時,請輸入新應用程式的名稱,然後點選「Create New Application」(建立新應用程式)

      如此一來,系統便會將函式下載到本機電腦,並在 IDE 的新工作區中開啟該函式。

    部署函式

    如要部署函式,請按照下列步驟操作:

    1. 在函式上按一下滑鼠右鍵,然後選取「Deploy function」(部署函式)

    2. 在快捷選單,選取要部署函式的 Google Cloud 專案。

    3. 選取要部署函式的區域。

    4. 選取執行階段。

      函式部署作業可能需要幾分鐘的時間才能完成。在進行中,系統會顯示 progress_activity 進度指標。完成後,畫面上會顯示 check_circle 勾號。如果失敗,系統會顯示錯誤標記。

      如果部署失敗,請查看「Output」分頁中的錯誤訊息。點選連結後,系統會將您導向 Google Cloud 控制台的建構作業記錄,並提供該錯誤的詳細資訊。

      錯誤訊息輸出

    5. 選用:函式部署完成後,您可以執行下列動作:

      • 如要查看已部署函式的唯讀檔案,請展開函式,然後展開「Source Code」資料夾。您可在此查看 launch.json (在 .vscode 底下)、已部署的程式碼和 package.json

      • 如要在 Google Cloud 控制台中查看函式,請在函式上按一下滑鼠右鍵,然後選取「Open in Cloud Console」(在 Cloud 控制台中開啟),如果出現系統提示,請選取「Open」(開啟)

      • 如要編輯函式的中繼資料,請在函式上按一下滑鼠右鍵,然後選取「Edit metadata in Cloud Console」(在 Cloud 控制台中編輯中繼資料)

      • 如要查看已部署函式的記錄檔,請在函式上按一下滑鼠右鍵,然後選取「View logs」(查看記錄檔)

    叫用函式

    只要叫用函式,即可在Google Cloud 控制台中,透過 IDE 內建的終端機執行先前部署的函式。如要叫用函式,請按照下列步驟操作:

    1. 在函式上按一下滑鼠右鍵,選取「Invoke function via HTTP」(透過 HTTP 叫用函式)

    2. 在顯示的「Trigger function over HTTP」(透過 HTTP 觸發函式) 快捷選單中,選取下列兩個選項之一:

      • 預設:終端機會在 IDE 中開啟並執行指令,以便在 Google Cloud中執行函式。

      • 從 JSON 檔案匯入引數:這個選項會擷取所選 JSON 檔案的內容,然後將其轉換為字串並使用 -d '{ 傳送該字串。當您有函式輸入內容且想儲存時,這項功能就很實用,可讓您執行輸入內容,而無須重複輸入。

    後續步驟

    取得支援

    如要提供意見回饋,請在 GitHub 上回報問題,或在 Stack Overflow 上提問。