使用 Ruby (第 1 代) 建立及部署 HTTP Cloud Run 函式

本指南將逐步說明如何使用 Ruby 執行階段編寫 Cloud Run 函式。Cloud Run 函式分為兩種類型:

  • HTTP 函式,可從標準 HTTP 要求叫用。
  • 事件驅動函式,用於處理 Cloud 基礎架構中的事件,例如 Pub/Sub 主題上的訊息,或 Cloud Storage 值區中的變更。

這個範例說明如何建立簡單的 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. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Functions and Cloud Build 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. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Functions and Cloud Build APIs.

    Enable the APIs

  8. 安裝並初始化 gcloud CLI
  9. 更新並安裝 gcloud 元件:
    gcloud components update
  10. 準備開發環境。

    前往 Ruby 設定指南

  11. 建立函式

    1. 在本機系統上為函式程式碼建立目錄:

      Linux 或 Mac OS X

      mkdir ~/helloworld
      cd ~/helloworld
      

      Windows

      mkdir %HOMEPATH%\helloworld
      cd %HOMEPATH%\helloworld
      
    2. helloworld 目錄中建立 app.rb 檔案,並加入以下內容:

      require "functions_framework"
      require "cgi"
      require "json"
      
      FunctionsFramework.http "hello_http" do |request|
        # The request parameter is a Rack::Request object.
        # See https://www.rubydoc.info/gems/rack/Rack/Request
        name = request.params["name"] ||
               (request.body.rewind && JSON.parse(request.body.read)["name"] rescue nil) ||
               "World"
        # Return the response body as a string.
        # You can also return a Rack::Response object, a Rack response array, or
        # a hash which will be JSON-encoded into a response.
        "Hello #{CGI.escape_html name}!"
      end

      此範例函式採用在 HTTP 要求中提供的名稱,並傳回問候語,如未提供任何名稱,系統會傳回「Hello World!」。

    指定依附元件

    Ruby 中的依附元件由 bundler 代管,並以名為 Gemfile 的檔案表示。

    部署函式時,Cloud Run 函式會使用 bundler 下載並安裝 GemfileGemfile.lock 中宣告的依附元件。

    Gemfile 會列出函式所需的套件,以及任何選用的版本限制。如果是 Cloud Run 函式,其中一個套件必須是 functions_framework Gem。

    在本練習中,請在與包含函式程式碼的 app.rb 檔案相同的目錄中,建立名為 Gemfile 的檔案,並加入下列內容:

    source "https://rubygems.org"
    
    gem "functions_framework", "~> 0.7"
    

    執行下列指令,安裝 functions_framework Gem 和其他依附元件:

    bundle install
    

    在本機建構及測試

    部署函式前,您可以在本機建構及測試函式。執行下列指令,使用 functions-framework-ruby 可執行檔啟動本機網路伺服器,執行 hello_http 函式:

    bundle exec functions-framework-ruby --target hello_http
    # ...starts the web server in the foreground
    

    如果函式建構成功,系統會顯示網址,您可以在網頁瀏覽器中前往該網址,查看函式運作情形: http://localhost:8080/。畫面上應會顯示 Hello World! 訊息。

    或者,您也可以透過其他終端機視窗使用 curl,傳送要求至此函式:

    curl localhost:8080
    # Output: Hello World!
    

    請參閱 Ruby Functions Framework 說明文件中的「測試函式」。

    部署函式

    如要使用 HTTP 觸發條件部署函式,請在 helloworld 目錄中執行下列指令:

    gcloud functions deploy hello_http --no-gen2 --runtime ruby33 --trigger-http --allow-unauthenticated
    

    --allow-unauthenticated 旗標可讓您不必驗證就能使用函式。如要要求驗證,請略過該旗標。

    測試已部署的函式

    1. 函式完成部署後,請記下 httpsTrigger.url 屬性,或使用下列指令找到這個屬性:

      gcloud functions describe hello_http
      

      內容應該會類似這樣:

      https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_http
    2. 透過您的瀏覽器造訪這個網址。您應該會看到「Hello World!」訊息。

      請嘗試透過 HTTP 要求傳送名稱,例如使用下列網址:

      https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_http?name=NAME

      您應該會看到「Hello NAME!」訊息

    查看記錄

    您可以在 Cloud Logging UI 或透過 Google Cloud CLI 查看 Cloud Run functions 記錄。

    使用指令列工具查看記錄

    如要透過 gcloud CLI 查看函式的記錄檔,請使用 logs read 指令加上函式的名稱:

    gcloud functions logs read hello_http

    輸出應會如下所示:

    LEVEL  NAME       EXECUTION_ID  TIME_UTC                 LOG
    D      helloHttp  rvb9j0axfclb  2019-09-18 22:06:25.983  Function execution started
    D      helloHttp  rvb9j0axfclb  2019-09-18 22:06:26.001  Function execution took 19 ms, finished with status code: 200

    在 Logging 資訊主頁中查看記錄

    您也可以從Google Cloud 控制台查看 Cloud Run functions 的記錄。