設定 CI/CD,以儲存 Terraform 程式碼即設定

本教學課程說明如何使用熱門的 GitOps 方法,透過 TerraformCloud Build 管理基礎架構式程式碼。「GitOps」GitOps一詞是由 Weaveworks 首先提出,其主要概念是使用 Git 存放區來儲存您所要的環境狀態。Terraform 是 HashiCorp 開發的開放原始碼工具,可讓您透過程式碼,以可預測的方式建立、變更及改善雲端基礎架構。在本教學課程中,您將使用 Cloud Build 這個 Google Cloud 持續整合服務,將 Terraform 資訊清單自動套用至您的環境。

本教學課程適用於正在尋找一流的策略,以便以可預測的方式變更基礎架構的開發人員和操作人員。本文假設您已熟悉 Google Cloud和 Linux。

開發運作現狀報告指出,有幾項能力可提升軟體推送成效。本教學課程將說明下列功能:

架構

本教學課程會將 GitOps 做法應用於管理 Terraform 的執行作業。請注意,其中使用了 Secure Source Manager 分支版本 devprod 來代表實際環境。這些環境分別由虛擬私有雲 (VPC) 網路 devprod 定義至Google Cloud 專案中。

將 Terraform 程式碼推送至 devprod 分支版本時,這個程序便會開始。在這種情況下,Cloud Build 會觸發並套用 Terraform 資訊清單,以在個別環境中實現您所要的狀態。另一方面,當您將 Terraform 程式碼推送至任何其他分支版本 (例如功能分支版本) 時,Cloud Build 會運作以執行 terraform plan,但不會將任何內容套用至任何環境。

在理想情況下,開發人員或操作人員必須向開發或功能分支版本提出基礎架構建議,然後透過提取要求提交那些建議。如此一來,您就可以在系統將變更合併至基本分支版本之前先和協作者討論及審查可能的變更,以及加入後續修訂版本。

如果沒有任何疑慮,您必須先將變更合併至 dev 分支版本。這項合併作業會觸發將基礎架構部署至 dev 環境,讓您能夠測試這個環境。在經過測試並對所部署的內容有信心後,您必須將 dev 分支版本合併至 prod 分支版本,以觸發將基礎架構安裝至實際工作環境。

目標

  • 設定 Secure Source Manager 執行個體和存放區。
  • 設定 Terraform 以將狀態儲存在 Cloud Storage 值區中。
  • 將權限授予您的 Cloud Build 服務帳戶。
  • 將 Cloud Build 連結至 Secure Source Manager 存放區。
  • 在功能分支版本中變更您的環境設定。
  • 將變更推行至開發環境。
  • 將變更推行至實際工作環境。

費用

在本文件中,您會使用 Google Cloud的下列計費元件:

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 Google Cloud 的使用者可能符合免費試用資格。

完成本文所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱清除所用資源一節。

事前準備

  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. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  6. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  7. 在 Cloud Shell 中,取得您剛剛選取的專案的 ID:
    gcloud config get-value project
    如果此指令未傳回專案 ID,請將 Cloud Shell 設定為使用您的專案,然後將 PROJECT_ID 改成您的專案 ID。
    gcloud config set project PROJECT_ID
  8. 啟用所需的 API:
    gcloud services enable cloudbuild.googleapis.com compute.googleapis.com securesourcemanager.googleapis.com
    這個步驟可能需要幾分鐘的時間才能完成。
  9. 如果您從未在 Cloud Shell 中使用過 Git,請使用您的姓名和電子郵件地址進行設定:
    git config --global user.email "YOUR_EMAIL_ADDRESS"
    git config --global user.name "YOUR_NAME"
    
    Git 會使用這項資訊識別您在 Cloud Shell 中建立的修訂版本的作者身分。
  10. 設定 Secure Source Manager 存放區

    在本教學課程中,您將使用單一 Secure Source Manager 存放區來定義雲端基礎架構。您可以讓不同的分支版本對應至不同的環境,藉此來自動化調度管理這個基礎架構:

    • dev 分支版本包含套用至開發環境的最新變更。
    • prod 分支版本包含套用至實際工作環境的最新變更。
    • 類似 feature_x 的功能分支版本可用於進行變更,然後再推送至 devprod 分支版本。

    有了這個基礎架構,您隨時可以參考存放區,以瞭解每個環境中的預期設定,並透過先將變更合併至 dev 環境的方式來提出新的變更。然後,您可以透過將 dev 分支版本合併至後續的 prod 分支版本中來推行變更。

    1. 建立空的 Secure Source Manager 存放區,但請勿初始化存放區。
    2. 執行下列指令,將 Secure Source Manager 驗證輔助程式新增至全域 git config

      git config --global credential.'https://*.*.sourcemanager.dev'.helper gcloud.sh
      

      使用 Secure Source Manager 執行 Git 指令時,驗證輔助程式會使用 gcloud CLI 擷取您的Google Cloud 憑證。

    3. 如要在初始憑證設定後重新驗證,請執行下列 gcloud CLI 指令:

      gcloud auth login
      
    4. solutions-terraform-cloudbuild-gitops 存放區複製到本機殼層或工作環境:

      git clone https://github.com/GoogleCloudPlatform/solutions-terraform-cloudbuild-gitops.git
      
    5. 將 Secure Source Manager 存放區新增為上游。

      git remote add google HTTPS_REPO_URL
      

      其中 HTTPS_REP_URL 是 Secure Source Manager 存放區的 HTTPS 網址。您可以在 Secure Source Manager 網頁介面的存放區頁面頂端找到網址。

    6. 建立並切換至 dev 分支。

      git checkout dev
      
    7. 使用下列指令,將複製的存放區推送至您的存放區:

      git push -u google --all
      
    8. 針對 prod 分支重複前兩個步驟。

    這個存放區中的程式碼結構如下:

    • environments/ 資料夾包含代表環境 (例如 devprod) 的子資料夾,可分別為處於不同成熟度階段 (開發和實際工作) 的工作負載之間提供邏輯隔離。雖然讓這些環境儘量類似是很好的做法,但每個子資料夾應有自己的 Terraform 設定,以確保它們可以視需要具有專屬的設定。

    • modules/ 資料夾包含內嵌 Terraform 模組。這些模組代表相關資源的邏輯群組,可用於跨不同環境共用程式碼。

    • cloudbuild.yaml 檔案是建構設定檔,其中包含 Cloud Build 的操作說明,例如如何根據一組步驟來執行工作。這個檔案會根據 Cloud Build 擷取程式碼的來源分支版本來指定條件式執行作業,例如:

      • 如果是 devprod 分支版本,系統會執行以下步驟:

        1. terraform init
        2. terraform plan
        3. terraform apply
      • 如果是任何其他分支版本,系統會執行以下步驟:

        1. 針對所有 environments 子資料夾執行 terraform init
        2. 針對所有 environments 子資料夾執行 terraform plan

    為確保提議的變更適用於所有環境,系統會針對所有 environments 子資料夾執行 terraform initterraform plan。合併提取要求之前,您可以先查看計畫,確保不會將存取權授予未經授權的實體。

    修改建構設定檔

    如要讓範例建構設定檔與 Secure Source Manager 搭配運作,請進行下列編輯:

    • 新增複製存放區的步驟。
    • 新增步驟以取得分支名稱,並指派給變數。

    dev 分支中編輯建構設定檔:

    1. 切換至 dev 分支版本:

      git checkout dev
      
    2. 開啟 cloudbuild.yaml 檔案,並將內容替換為下列程式碼:

      # Copyright 2019 Google LLC
      #
      # Licensed under the Apache License, Version 2.0 (the "License");
      # you may not use this file except in compliance with the License.
      # You may obtain a copy of the License at
      #
      #     https://www.apache.org/licenses/LICENSE-2.0
      #
      # Unless required by applicable law or agreed to in writing, software
      # distributed under the License is distributed on an "AS IS" BASIS,
      # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      # See the License for the specific language governing permissions and
      # limitations under the License.
      
      
      steps:
      - id: 'clone repository'
        name: 'gcr.io/cloud-builders/git'
        args:
        - clone
        - '${_REPO_URL}'
        - .
      - id: 'branch name'
        name: gcr.io/cloud-builders/git
        entrypoint: 'sh'
        args:
        - '-c'
        - |
            branch=$(basename "$_REF")
            git checkout ${branch}
            echo "***********************"
            git branch --show-current
            echo "***********************"
      
      - id: 'tf init'
        name: 'hashicorp/terraform:1.0.0'
        entrypoint: 'sh'
        args:
        - '-c'
        - |
         branch=$(basename "$_REF")
            if [ -d "environments/${branch}/" ]; then
              cd environments/${branch}
              terraform init
            else
              for dir in environments/*/
              do
                cd ${dir}
                env=${dir%*/}
                env=${env#*/}
                echo ""
                echo "*************** TERRAFORM INIT ******************"
                echo "******* At environment: ${env} ********"
                echo "*************************************************"
                terraform init || exit 1
                cd ../../
              done
            fi
      
      - id: 'tf plan'
        name: 'hashicorp/terraform:1.0.0'
        entrypoint: 'sh'
        args:
        - '-c'
        - |
            branch=$(basename "$_REF")
            if [ -d "environments/${branch}/" ]; then
              cd environments/${branch}
              terraform plan
            else
              for dir in environments/*/
              do
                cd ${dir}
                env=${dir%*/}
                env=${env#*/}
                echo ""
                echo "*************** TERRAFOM PLAN ******************"
                echo "******* At environment: ${env} ********"
                echo "*************************************************"
                terraform plan || exit 1
                cd ../../
              done
            fi
      
      - id: 'tf apply'
        name: 'hashicorp/terraform:1.0.0'
        entrypoint: 'sh'
        args:
        - '-c'
        - |
            branch=$(basename "$_REF")
            if [ -d "environments/${branch}/" ]; then
              cd environments/${branch}
              terraform apply -auto-approve
            else
              echo "***************************** SKIPPING APPLYING *******************************"
              echo "Branch '${branch}' does not represent an official environment."
              echo "*******************************************************************************"
            fi
    3. 確認檔案已修改。

      git status
      
    4. 修訂並推送變更:

      git add --all
      git commit -m "Modify build config file."
      git push google dev
      
    5. 開啟提取要求,快速將變更升級至 prod 分支。

      1. 在 Secure Source Manager 網頁介面中,前往存放區。
      2. 按一下「Pull requests」(提取要求) 分頁標籤。
      3. 按一下 [New pull request] (新的提取要求)
      4. 在「merge into:」(要併入的分支) 欄位中,選取 prod 分支。
      5. 在「pull from:」(從以下位置提取) 欄位中,選取 dev 分支版本。
      6. 查看變更,然後按一下「New pull request」(新提取要求)。
      7. 按一下 [Create pull request]
      8. 按一下「合併提取要求」
      9. 再次按一下「Merge pull request」

        變更會合併至 prod 分支版本。

    設定 Terraform 以將狀態儲存在 Cloud Storage 值區中

    根據預設,Terraform 會將狀態儲存在名為 terraform.tfstate 的本機檔案中。這個預設設定會使得團隊難以使用 Terraform,尤其是當有許多使用者同時執行 Terraform,且每部機器對於目前的基礎架構都有自己的理解時。

    為了協助您避免這類問題,本節會設定指向 Cloud Storage 值區的遠端狀態。遠端狀態是後端的一項功能,在本教學課程中會在 backend.tf 檔案中進行設定,例如:

    # Copyright 2019 Google LLC
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #     https://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    
    
    terraform {
      backend "gcs" {
        bucket = "PROJECT_ID-tfstate"
        prefix = "env/dev"
      }
    }
    

    在以下步驟中,您將建立一個 Cloud Storage bucket,並變更一些檔案以指向您的新 bucket 和 Google Cloud 專案。

    1. 在 Cloud Shell 中,建立 Cloud Storage 值區:

      PROJECT_ID=$(gcloud config get-value project)
      gcloud storage buckets create gs://${PROJECT_ID}-tfstate
      
    2. 啟用物件版本管理功能,以保留部署的記錄:

      gcloud storage buckets update gs://${PROJECT_ID}-tfstate --versioning
      

      不過,啟用這項功能會增加儲存空間成本;您可以設定物件生命週期管理將較早的狀態版本刪除,藉此降低相關費用的支出。

    3. 建立新的 cloud-storage-bucket 分支來進行變更:

      cd ~/solutions-terraform-cloudbuild-gitops
      git checkout -b cloud-storage-bucket
      
    4. terraform.tfvarsbackend.tf 檔案中,將 PROJECT_ID 預留位置改成專案 ID:

      sed -i s/PROJECT_ID/$PROJECT_ID/g environments/*/terraform.tfvars
      sed -i s/PROJECT_ID/$PROJECT_ID/g environments/*/backend.tf
      

      在 OS X 或 macOS 上,你可能需要在 sed -i 後方加上兩個半形雙引號 (""),如下所示:

      sed -i "" s/PROJECT_ID/$PROJECT_ID/g environments/*/terraform.tfvars
      sed -i "" s/PROJECT_ID/$PROJECT_ID/g environments/*/backend.tf
      
    5. 檢查是否已更新所有檔案:

      git status
      

      輸出如下所示:

      On branch cloud-storage-bucket
      Changes not staged for commit:
      (use "git add ..." to update what will be committed)
      (use "git restore ..." to discard changes in working directory)
             modified:   environments/dev/backend.tf
             modified:   environments/dev/terraform.tfvars
             modified:   environments/prod/backend.tf
             modified:   environments/prod/terraform.tfvars
      no changes added to commit (use "git add" and/or "git commit -a")
      
    6. 修訂並推送變更:

      git add --all
      git commit -m "Update project IDs and buckets"
      git push google -u cloud-storage-bucket
      

      新的 cloud-storage-bucket 分支版本會推送至存放區。

    7. 開啟並提交每個分支的合併要求,將 cloud-storage-bucket 變更合併至 devprod 分支。

    將權限授予 Cloud Build 服務帳戶

    如要讓 Cloud Build 服務帳戶以管理 Google Cloud 資源的目標執行 Terraform 指令碼,您必須為其授予對專案的適當存取權。為簡單起見,在本教學課程中會授予專案編輯者存取權。在實際工作環境中,請遵循貴公司的 IT 安全性最佳做法,通常是提供最低存取權限

    1. 如要找出 Cloud Build 服務帳戶電子郵件,請前往 Cloud Build 頁面,然後前往「設定」

      前往 Cloud Build 設定

    2. 複製「服務帳戶電子郵件地址」的值。

    3. 將所需的存取權授予您的 Cloud Build 服務帳戶:

      gcloud projects add-iam-policy-binding PROJECT_ID \
          --member serviceAccount:CLOUDBUILD_SA --role roles/editor
      

      更改下列內容:

      • PROJECT_ID 為您的專案 ID。
      • CLOUDBUILD_SA,並提供 Cloud Build 服務帳戶電子郵件地址。

    連結至 Cloud Build

    如要在推送至任何分支版本時觸發 Cloud Build,請設定 Secure Source Manager Webhook。建構設定檔會檢查分支名稱,判斷是否需要變更 devprod 環境。

    1. 在專案中啟用及設定 Cloud Build

    2. 在 Google Cloud 控制台中開啟「觸發條件」頁面。

      開啟「Triggers」(觸發條件) 頁面

    3. 在頁面頂端的專案選取器下拉式選單中選取專案。

    4. 按一下「開啟」

    5. 按一下「建立觸發條件」

    6. 輸入下列觸發條件設定:

      • Name (名稱):trigger-on-push

      • 「Region」(區域):選取觸發條件的區域。如果與觸發條件相關聯的建構設定檔指定了私人集區,您為觸發條件選取的區域就必須與私人集區的區域相符。

        如果您選取 global 做為區域,Cloud Build 會使用建構設定檔中指定的區域執行建構作業。如果您在建構設定檔中指定私人集區,這可以是私人集區的區域;如果您未指定私人集區,則可以是全域預設集區。

      • 說明 (選填):輸入觸發條件的說明。

      • 「Event」(事件):選取「Webhook event」(Webhook 事件) 來當做叫用觸發條件的存放區事件。

        如果未安裝 Secret Manager,系統會提示您啟用 Secret Manager。

      • Webhook 網址:選取下列其中一個選項:

        • 如要使用 Cloud Build 產生新的密鑰,請使用新的密鑰。按一下「建立密鑰」即可建立密鑰。
        • 如要使用現有密鑰,請使用現有密鑰或自行建立。在下拉式選取方塊中輸入密鑰和版本。

        如果使用現有密鑰,您可能需要手動將 Secret Manager 密鑰存取者角色授予 Cloud Build 服務代理程式 service-PROJECT_NUMBER@gcp-sa-cloudbuild.iam.gserviceaccount.com

        詳情請參閱將角色授予 Cloud Build 服務代理程式

    7. 按一下「顯示網址預覽結果」,並記錄網址。您需要這個網址,才能在 Secure Source Manager 中設定 Webhook。

      • 設定:針對「類型」選取「Cloud Build 設定檔 (YAML 或 JSON)」,然後針對「位置」選取「內嵌」
    8. 按一下「開啟編輯器」按鈕,編輯建構設定檔。

    9. cloudbuild.yaml 檔案的內容複製到編輯器。

      如先前所述,這個管道會根據要擷取的分支版本而有不同的行為。該版本會檢查 ${branch} 變數是否與任何環境資料夾相符。如果相符的話,Cloud Build 會針對該環境執行 terraform plan。否則,Cloud Build 會針對所有環境執行 terraform plan,以確保建議的變更適用於所有環境。如果這些計畫中有任何一個無法執行,則該版本會失敗。

      - id: 'tf plan'
        name: 'hashicorp/terraform:1.0.0'
        entrypoint: 'sh'
        args:
        - '-c'
        - |
            branch=$(basename "$_REF")
            if [ -d "environments/${branch}/" ]; then
              cd environments/${branch}
              terraform plan
            else
              for dir in environments/*/
              do
                cd ${dir}
                env=${dir%*/}
                env=${env#*/}
                echo ""
                echo "*************** TERRAFOM PLAN ******************"
                echo "******* At environment: ${env} ********"
                echo "*************************************************"
                terraform plan || exit 1
                cd ../../
              done
            fi

      terraform apply 指令會針對環境分支版本執行,但在任何其他情況下,都會完全遭到忽略。

    10. 按一下「+ 新增變數」,然後新增下列兩個替代變數:

      • 「Variable」(變數)_REPO_URL、「Value」(值)$(body.repository.clone_url)
      • 「Variable」(變數)_REF、「Value」(值)$(body.ref)
    11. 點選「建立」

    在 Secure Source Manager 中設定 Webhook

    建立 Webhook,在推送至 devprod 分支版本時觸發建構。

    1. 在 Secure Source Manager 網頁介面中,前往要建立 Webhook 的存放區。
    2. 按一下「設定」
    3. 按一下「Webhook」,然後按一下「新增 Webhook」
    4. 在「Hook ID」(Webhook ID) 欄位中,輸入 Webhook 的 ID。

    5. 在「目標網址」欄位中,輸入您在 Cloud Build 中設定 Webhook 觸發條件時複製的 Webhook 網址。

      如要找出 Webhook 網址,請按照下列步驟操作:

      1. 在 Google Cloud 控制台中開啟「觸發條件」頁面。

        開啟「Triggers」(觸發條件) 頁面

      2. 按一下觸發條件。

      3. 在「Webhook URL」(Webhook 網址) 部分中,按一下「Show URL preview」(顯示網址預覽),然後複製網址。

    6. Webhook 網址包含您建立 Cloud Build 觸發程序時輸入的鍵和密鑰值。為避免洩漏這些值,請從目標網址結尾移除這些值,然後複製到「敏感查詢字串」欄位。

      如要在 Webhook 網址中找出金鑰和密鑰,請尋找以 key= 開頭的文字

      舉例來說,假設有以下網址: https://cloudbuild.googleapis.com/v1/projects/my-project/triggers/test-trigger:webhook?key=eitIfKhYnv0LrkdsyHqIros8fbsheKRIslfsdngf&secret=Hello%20Secret%20Manager

      從「目標網址」欄位中,複製並移除以問號開頭的部分 ?key=...。然後移除開頭的問號,將剩餘部分 key=... 移至「敏感查詢字串」欄位。

    7. 按一下 [Add Webhook]

    8. Webhook 會顯示在「Webhooks」頁面。

    在新的功能分支版本中變更您的環境設定

    1. 確認您位於 dev 分支版本中:

      cd ~/solutions-terraform-cloudbuild-gitops
      git checkout dev
      
    2. 提取最新變更:

      git pull
      
    3. 建立 bug-fix 分支版本,以變更環境設定。

      git checkout -b bug-fix
      
    4. 開啟 modules/firewall/main.tf 進行編輯。

    5. 在第 30 行中,修正 target_tags 欄位中的 "http-server2" 錯字。

      此值必須是 "http-server"

    6. 修訂並推送變更:

      git add --all
      git commit -m "Fix typo."
      git push google -u bug-fix
      
    7. 在 Google Cloud 控制台中開啟 Cloud Build 的「History」(記錄) 頁面:

      開啟「記錄」頁面

    8. 按一下「Build」(建構) 即可查看更多資訊,包括 terraform plan 的輸出內容。

    請注意,Cloud Build 工作會執行在 cloudbuild.yaml 檔案中定義的管道。如先前所述,這個管道會根據要擷取的分支版本而有不同的行為。該版本會檢查 ${branch} 變數是否與任何環境資料夾相符。如果相符的話,Cloud Build 會針對該環境執行 terraform plan。否則,Cloud Build 會針對所有環境執行 terraform plan,以確保建議的變更適用於所有環境。如果這些計畫中有任何一個無法執行,則該版本會失敗。

    - id: 'tf plan'
      name: 'hashicorp/terraform:1.0.0'
      entrypoint: 'sh'
      args:
      - '-c'
      - |
          branch=$(basename "$_REF")
          if [ -d "environments/${branch}/" ]; then
            cd environments/${branch}
            terraform plan
          else
            for dir in environments/*/
            do
              cd ${dir}
              env=${dir%*/}
              env=${env#*/}
              echo ""
              echo "*************** TERRAFOM PLAN ******************"
              echo "******* At environment: ${env} ********"
              echo "*************************************************"
              terraform plan || exit 1
              cd ../../
            done
          fi

    同樣地,terraform apply 指令會針對環境分支版本執行,但是在任何其他情況下,它都會完全遭到忽略。在本節中,您已將程式碼變更提交至新的分支版本,因此系統未將任何基礎架構部署套用至您的 Google Cloud 專案。

    - id: 'tf apply' name: 'hashicorp/terraform:1.0.0' entrypoint: 'sh' args: - '-c' - | branch=$(basename "$_REF") if [ -d "environments/${branch}/" ]; then cd environments/${branch} terraform apply -auto-approve else echo "***************************** SKIPPING APPLYING *******************************" echo "Branch '${branch}' does not represent an official environment." echo "*******************************************************************************" fi

    將變更推行至開發環境

    現在可以將您想要的狀態套用至 dev 環境了。

    1. 在 Secure Source Manager 網頁介面中,前往存放區。
    2. 按一下「New pull request」
    3. 在「merge into:」(要併入的分支) 欄位中,選取 dev 分支。
    4. 在「pull from:」(從以下位置提取) 欄位中,選取 bug-fix 分支版本。
    5. 按一下 [New pull request] (新的提取要求)
    6. 按一下 [Create pull request]
    7. 按一下「Merge pull request」(合併提取要求),然後再次按一下「Merge pull request」(合併提取要求)
    8. 檢查是否已觸發新的 Cloud Build:

      前往 Cloud Build 頁面

    9. 開啟版本並檢查記錄檔。

      當該版本完成後,您會看到如下的內容:

      Step #3 - "tf apply": external_ip = EXTERNAL_IP_VALUE
      Step #3 - "tf apply": firewall_rule = dev-allow-http
      Step #3 - "tf apply": instance_name = dev-apache2-instance
      Step #3 - "tf apply": network = dev
      Step #3 - "tf apply": subnet = dev-subnet-01
      
    10. 複製 EXTERNAL_IP_VALUE 並在網路瀏覽器中開啟該網址。

      http://EXTERNAL_IP_VALUE
      

      這項佈建可能需要幾秒鐘的時間來啟動 VM 和傳播防火牆規則。最後,您會在網路瀏覽器中看到「Environment: dev」(環境:dev)

    11. 前往 Cloud Storage:

      前往 Cloud Storage 頁面

    12. 選取專案。

    13. 按一下 Terraform 狀態儲存空間 bucket。值區名稱如下所示:

      PROJECT_ID-tfstate
      
    14. 依序點選「env」和「dev」,即可查看 Terraform 狀態檔案。

    將變更推行至實際工作環境

    現在您的開發環境已經過全面測試,接下來便可以將基礎架構程式碼推行至實際工作環境了。

    1. 在 Secure Source Manager 網頁介面中,前往存放區。
    2. 按一下「Pull requests」(提取要求) 分頁標籤。
    3. 按一下 [New pull request] (新的提取要求)
    4. 在「merge into:」(合併至:) 中,選取存放區的 prod 分支。
    5. 在「pull from」(從以下位置提取) 中,選取存放區 dev 分支版本。
    6. 按一下 [New pull request] (新的提取要求)
    7. 針對「title」(標題) 輸入標題 (例如「Promoting networking changes」),然後按一下「Create pull request」(建立提取要求)
    8. 查看建議的變更,然後按一下「Merge pull request」

      系統會在留言欄位中加入日期和存放區網址。

    9. 再次按一下「Merge pull request」(合併提取要求) 確認。

    10. 在 Google Cloud 控制台中,開啟「Build History」(版本記錄) 頁面,以查看正在套用至實際工作環境的變更:

      前往 Cloud Build 頁面

    11. 等待版本完成,然後檢查記錄檔。

      在記錄檔的末尾,您會看到如下的內容:

      Step #3 - "tf apply": external_ip = EXTERNAL_IP_VALUE
      Step #3 - "tf apply": firewall_rule = prod-allow-http
      Step #3 - "tf apply": instance_name = prod-apache2-instance
      Step #3 - "tf apply": network = prod
      Step #3 - "tf apply": subnet = prod-subnet-01
      
    12. 複製 EXTERNAL_IP_VALUE 並在網路瀏覽器中開啟該網址。

      http://EXTERNAL_IP_VALUE
      

      這項佈建可能需要幾秒鐘的時間來啟動 VM 和傳播防火牆規則。最後,您會在網路瀏覽器中看到「Environment: prod」(環境:prod)

    13. 前往 Cloud Storage:

      前往 Cloud Storage 頁面

    14. 選取專案。

    15. 按一下 Terraform 狀態儲存空間 bucket。值區名稱如下所示:

      PROJECT_ID-tfstate
      
    16. 依序點選「env」和「prod」,即可查看 Terraform 狀態檔案。

    您已成功在 Cloud Build 中設定無伺服器基礎架構即程式碼管道。日後,您可以嘗試以下做法:

    • 針對不同的用途新增部署。
    • 建立其他環境以反映您的需求。
    • 針對每個環境使用一個專案,而不是針對每個環境使用一個虛擬私人雲端。

    清除所用資源

    完成本教學課程後,請清除您在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.

    後續步驟