Enable services, create an artifact registry and the GKE cluster

Check my progress

/ 20

Create a container image with Cloud Build

Check my progress

/ 20

Create the Continuous Integration (CI) Pipeline

Check my progress

/ 20

Store SSH key secret in Secret Manager

Check my progress

/ 20

Create the Test Environment and CD Pipeline

Check my progress

/ 20

This lab may incorporate AI tools to support your learning.
This content is not yet optimized for mobile devices.
For the best experience, please visit us on a desktop computer using a link sent by email.

GSP1077

Google Cloud 自學實驗室

總覽

Cloud Build 是在 Google Cloud 執行您建構內容的服務,可匯入原始碼、執行建構作業等。

在本實驗室,您將建立持續整合/持續部署 (CI/CD) 管道,從已修訂的程式碼自動建構容器映像檔、將該映像檔儲存到 Artifact Registry、更新 Git 存放區中的 Kubernetes 資訊清單,並使用該清單將應用程式部署至 Google Kubernetes Engine。

CI/CD 流程

在本實驗室,您將使用 2 個託管於專屬自行託管式 Gitea Git 伺服器 (git-server) 的 Git 存放區:

  • app:包含應用程式原始碼 (hello-cloudbuild-app)
  • env:包含 Kubernetes 部署作業資訊清單 (hello-cloudbuild-env)

當您將變更推送至 app 存放區時,Cloud Build 管道會執行測試、建構容器映像檔,並將該變更推送至 Artifact Registry。推送映像檔後,Cloud Build 會更新部署作業資訊清單,再將該清單推送至 env 存放區。這會觸發另一個 Cloud Build 管道,將資訊清單套用至 GKE 叢集,並在套用成功後,將清單儲存到 env 存放區的其他分支版本。

之所以區隔 app 和 env,是因為這兩個存放區有不同的生命週期和用途。app 存放區專用於特定應用程式,主要由真人使用。env 存放區可能由多個應用程式共用,並由 Cloud Build 等自動化系統使用。env 存放區可以有多個分支版本,每個都會對應到特定環境並參照特定的容器映像檔,而 app 存放區則沒有。

完成本實驗室後,您的系統將能輕鬆執行下列工作:

  • 查看 Cloud Build 記錄來區分失敗和成功的部署作業。
  • 查看 env 存放區的 production 分支版本,存取目前使用的資訊清單。
  • 重新執行對應的 Cloud Build 建構作業,復原到先前的任意版本。

Git 作業流程

目標

在本實驗室,您將瞭解如何執行下列工作:

  • 建立 Kubernetes Engine 叢集。
  • 連至專屬 Git 伺服器,並初始化 Git 存放區。
  • 從原始碼存放區執行 Cloud Build 管道。
  • 透過 Cloud Build 自動測試並發布可部署的容器映像檔。
  • 透過 Cloud Build 管理 Kubernetes Engine 叢集中部署的資源。

設定和需求

瞭解以下事項後,再點選「Start Lab」按鈕

請詳閱以下操作說明。實驗室活動會計時,且中途無法暫停。點選「Start Lab」後就會開始計時,顯示可使用 Google Cloud 資源的時間。

您將在真正的雲端環境完成實作實驗室活動,而不是模擬或示範環境。為此,我們會提供新的暫時憑證,供您在實驗室活動期間登入及存取 Google Cloud。

為了順利完成這個實驗室,請先確認:

  • 可以使用標準的網際網路瀏覽器 (Chrome 瀏覽器為佳)。
注意事項:請使用無痕模式 (建議選項) 或私密瀏覽視窗執行此實驗室,這可以防止個人帳戶和學員帳戶之間的衝突,避免個人帳戶產生額外費用。
  • 是時候完成實驗室活動了!別忘了,活動一旦開始將無法暫停。
注意事項:務必使用實驗室專用的學員帳戶。如果使用其他 Google Cloud 帳戶,可能會產生額外費用。

如何開始實驗室及登入 Google Cloud 控制台

  1. 按一下「Start Lab」按鈕。如果實驗室會產生費用,請在開啟的對話方塊中選取付款方式。右側的「Lab setup and access」面板會顯示下列項目:

    • 「Open Google Cloud console」按鈕
    • 此實驗室所需的臨時憑證 (使用者名稱和密碼)
    • 完成此實驗室所需的其他資訊 (如有)

    請注意,實驗室計時器位於頁面頂端附近,會顯示剩餘時間。

  2. 按一下「Open Google Cloud console」。如果使用 Chrome 瀏覽器,也可以按一下滑鼠右鍵,選取「在無痕視窗中開啟連結」。

    接著,實驗室會啟動相關資源,並開啟另一個分頁,顯示「登入」頁面。

    提示:您可以在不同的視窗並排開啟分頁。

    注意:頁面顯示「選擇帳戶」對話方塊時,請點選「使用其他帳戶」。
  3. 如有必要,請將下方的 Username 貼到「登入」對話方塊。

    {{{user_0.username | "Username"}}}

    您也可以在「Lab setup and access」面板找到「Username」。

  4. 點選「下一步」。

  5. 複製下方的 Password,並貼到「歡迎使用」對話方塊。

    {{{user_0.password | "Password"}}}

    您也可以在「Lab setup and access」面板找到「Password」。

  6. 點選「下一步」。

    重要事項:請務必使用實驗室提供的憑證,而非自己的 Google Cloud 帳戶憑證。 注意:如果使用自己的 Google Cloud 帳戶來進行這個實驗室,可能會產生額外費用。
  7. 繼續點按後續頁面:

    • 接受條款及細則。
    • 由於這是臨時帳戶,請勿新增救援選項或雙重驗證機制。
    • 請勿申請免費試用。

Google Cloud 控制台稍後會在這個分頁開啟。

注意:如要使用 Google Cloud 產品和服務,請按一下「導覽選單」,或在「搜尋」欄位輸入服務或產品名稱。「導覽選單」圖示和搜尋欄位

啟動 Cloud Shell

Cloud Shell 是搭載多項開發工具的虛擬機器,提供永久的 5 GB 主目錄,而且在 Google Cloud 中運作。Cloud Shell 提供指令列存取權,方便您使用 Google Cloud 資源。

  1. 點按 Google Cloud 控制台頂端的「啟用 Cloud Shell」圖示 「啟動 Cloud Shell」圖示。

  2. 系統顯示視窗時,請按照下列步驟操作:

    • 繼續操作 Cloud Shell 視窗。
    • 授權 Cloud Shell 使用您的憑證發出 Google Cloud API 呼叫。

連線建立完成即代表已通過驗證,而且專案已設為您的 Project_ID:。輸出內容中有一行文字,宣告本工作階段的 Project_ID:

Your Cloud Platform project in this session is set to {{{project_0.project_id | "PROJECT_ID"}}}

gcloud 是 Google Cloud 的指令列工具,已預先安裝於 Cloud Shell,並支援 Tab 鍵自動完成功能。

  1. (選用) 您可以執行下列指令來列出使用中的帳戶:
gcloud auth list
  1. 點按「授權」。

輸出內容:

ACTIVE: * ACCOUNT: {{{user_0.username | "ACCOUNT"}}} To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (選用) 您可以使用下列指令來列出專案 ID:
gcloud config list project

輸出內容:

[core] project = {{{project_0.project_id | "PROJECT_ID"}}} 注意:如需 gcloud 的完整說明,請前往 Google Cloud 參閱 gcloud CLI 總覽指南。

工作 1:初始化實驗室

這項工作會引導您設定環境:

  • 將專案 ID、專案編號和 Git 伺服器 IP 做為變數匯入
  • 啟用 GKE、Cloud Build、Secret Manager 和 Artifact Analysis 所需的 API
  • 建立 Artifact Registry Docker 存放區
  • 建立 GKE 叢集,用於部署本實驗室的範例應用程式
  1. 在 Cloud Shell 執行下列指令,設定專案 ID、專案編號和區域:
export PROJECT_ID=$(gcloud config get-value project) export PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)') export REGION={{{ project_0.default_region }}} gcloud config set compute/region $REGION export GIT_SERVER_IP=$(gcloud compute instances describe git-server --zone={{{ project_0.default_zone }}} --format='get(networkInterfaces[0].accessConfigs[0].natIP)') echo "Git Server IP is: ${GIT_SERVER_IP}"
  1. 執行下列指令,啟用 GKE、Cloud Build、Secret Manager 和 Artifact Analysis 所需的 API:
gcloud services enable container.googleapis.com \ cloudbuild.googleapis.com \ secretmanager.googleapis.com \ containeranalysis.googleapis.com
  1. 在 區域建立 Artifact Registry Docker 存放區 my-repository,用於儲存容器映像檔:
gcloud artifacts repositories create my-repository \ --repository-format=docker \ --location=$REGION
  1. 建立 GKE 叢集,用於部署本研究室的範例應用程式:
gcloud container clusters create hello-cloudbuild --num-nodes 1 --region $REGION
  1. 在 Cloud Shell 執行下列指令,設定 Git: git config --global user.name "giteaadmin" git config --global user.email "student@qwiklabs.net"

點選「Check my progress」,確認目標已達成。 啟用服務、建立 Artifact Registry 和 GKE 叢集

工作 2:連至 Git 伺服器上的 Git 存放區

在這項工作,您將在專屬 Git 伺服器 (git-server) 初始化兩個 Git 存放區 (hello-cloudbuild-app 和 hello-cloudbuild-env),並推送初始程式碼範例。

  1. 從 Cloud Storage 下載程式碼範例:
cd ~ git clone http://${GIT_SERVER_IP}:3000/giteaadmin/hello-cloudbuild-app.git

您複製的程式碼包含一個簡易的「Hello World」應用程式:

from flask import Flask app = Flask('hello-cloudbuild') @app.route('/') def hello(): return "Hello World!\n" if __name__ == '__main__': app.run(host = '0.0.0.0', port = 8080)

工作 3:使用 Cloud Build 建立容器映像檔

在這項工作,您將使用現有的 Dockerfile,透過 Cloud Build 建立及儲存容器映像檔。

您先前複製的程式碼即包含此 Docker 檔案:

FROM python:3.11-slim RUN pip install flask WORKDIR /app COPY app.py /app/app.py ENTRYPOINT ["python"] CMD ["/app/app.py"]

有了這個 Dockerfile,您就能使用 Cloud Build 建立容器映像檔,並儲存到 Artifact Registry。

  1. 在 Cloud Shell 執行下列指令,根據最新的修訂版本建立 Cloud Build 建構作業:
cd ~/hello-cloudbuild-app COMMIT_ID="$(git rev-parse --short=7 HEAD)" gcloud builds submit --tag="${REGION}-docker.pkg.dev/${PROJECT_ID}/my-repository/hello-cloudbuild:${COMMIT_ID}" .

執行此指令時,Cloud Build 會將建立容器映像檔時產生的記錄串流到終端機。

  1. 建構作業完成後,在 Google Cloud 控制台標題列的「搜尋」欄位輸入 Artifact Registry,然後點選結果中的「Artifact Registry」,確認當中有新的容器映像檔。點選「my-repository」,即可在「映像檔」清單中看到 hello-cloudbuild。

點選「Check my progress」,確認目標已達成。 使用 Cloud Build 建立容器映像檔

工作 4:建立並執行持續整合 (CI) 管道

在這項工作,您會設定並執行 Cloud Build,自動執行單元測試、建構容器映像檔,然後推送至 Artifact Registry。

架構

程式碼中的 cloudbuild.yaml 檔案是管道的設定。

  1. 提交變更並推送至 Git 伺服器:
cd ~/hello-cloudbuild-app git add . git commit -m "Trigger CI pipeline" git push http://giteaadmin:GiteaPassword123@${GIT_SERVER_IP}:3000/giteaadmin/hello-cloudbuild-app.git main
  1. 使用下列指令,將建構作業提交至 Cloud Build:
gcloud builds submit --config=cloudbuild.yaml --substitutions=SHORT_SHA=$(git rev-parse --short=7 HEAD) .
  1. 在 Google Cloud 控制台搜尋 Cloud Build,然後點選左側窗格中的「記錄」,即可查看 CI 建構作業的執行記錄。

點選「Check my progress」,確認目標已達成。 建立持續整合 (CI) 管道

工作 5:將 SSH 私密金鑰儲存在 Secret Manager

在這個步驟中,您會將私密金鑰儲存在 Secret Manager,並授予 Cloud Build 存取 Secret Manager 的權限。

建立 SSH 金鑰並儲存在 Secret Manager

  1. 在 Cloud Shell 建立 SSH 金鑰:

    mkdir -p ~/workingdir && cd ~/workingdir ssh-keygen -t rsa -b 4096 -N '' -f id_rsa -C "student@qwiklabs.net"
  2. 在 Cloud Shell 執行下列指令,將 SSH 私密金鑰儲存在 Secret Manager:

gcloud secrets create ssh_key_secret --data-file=$HOME/workingdir/id_rsa

或者,您也能在 Google Cloud 控制台執行下列操作: a. 首先,將 id_rsa 下載到電腦:在 Cloud Shell,依序點選「顯示更多項目」圖示 「更多」圖示 >「Download file」,輸入 workingdir/id_rsa,然後點選「下載」。 b. 搜尋 Secret Manager,然後點選「Secret Manager」。 c. 點選「建立密鑰」。 d. 將「名稱」設為 ssh_key_secret。 e. 在「密鑰值」下方,選取「上傳」,然後上傳下載的 id_rsa 檔案。 f. 點選「建立密鑰」。

授予服務帳戶 Secret Manager 的存取權

在 Cloud Shell 執行下列指令,授予 Compute Engine 預設服務帳戶 Secret Manager 的存取權:

PROJECT_NUMBER=$(gcloud projects describe ${PROJECT_ID} --format='value(projectNumber)') gcloud projects add-iam-policy-binding ${PROJECT_NUMBER} \ --member=serviceAccount:${PROJECT_NUMBER}-compute@developer.gserviceaccount.com \ --role=roles/secretmanager.secretAccessor

點選「Check my progress」,確認目標已達成。 將 SSH 私密金鑰儲存在 Secret Manager

工作 6:建立測試環境和 CD 管道

Cloud Build 也可用於持續推送軟體更新管道。每次將修訂版本推送至 hello-cloudbuild-env 存放區的 candidate 分支版本時,管道都會開始運作。管道會將新版資訊清單套用至 Kubernetes 叢集,並在套用成功後,將清單複製到 production 分支版本。這項程序具有下列特性:

  • candidate 分支版本是嘗試部署記錄。
  • production 分支版本是成功部署記錄。
  • 您可以查看 Cloud Build 中成功和失敗的部署作業。
  • 您可以復原到先前部署的任何版本,只要重新執行對應的 Cloud Build 建構作業即可。復原作業也會更新 production 分支版本,這是為了真實反映部署記錄。

接下來,您將修改持續整合管道,更新 hello-cloudbuild-env 存放區的 candidate 分支版本,觸發持續推送軟體更新管道。

將 GKE 存取權授予 Cloud Build

為了在 Kubernetes 叢集部署應用程式,Cloud Build 需要 Kubernetes Engine 開發人員角色。

  1. 在 Cloud Shell 執行下列指令:
PROJECT_NUMBER="$(gcloud projects describe ${PROJECT_ID} --format='get(projectNumber)')" gcloud projects add-iam-policy-binding ${PROJECT_NUMBER} \ --member=serviceAccount:${PROJECT_NUMBER}@cloudbuild.gserviceaccount.com \ --role=roles/container.developer

您需要使用兩個分支版本 (production 和 candidate) 和描述部署程序的 Cloud Build 設定檔,將 hello-cloudbuild-env 存放區初始化。

  1. 在 Cloud Shell,從 Cloud Storage 下載程式碼範例:
cd ~ git clone http://${GIT_SERVER_IP}:3000/giteaadmin/hello-cloudbuild-env.git
  1. 在 Cloud Shell 執行下列指令,替換 hello-cloudbuild-env 存放區中的 cloudbuild.yaml 內容:
cd ~/hello-cloudbuild-env cat <<'EOF' > cloudbuild.yaml # Copyright 2018 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. substitutions: _COMMIT_SHA: 'v1.0' steps: # This step deploys the new version of our container image # in the hello-cloudbuild Kubernetes Engine cluster. - name: 'gcr.io/cloud-builders/kubectl' id: Deploy args: - 'apply' - '-f' - 'kubernetes.yaml' env: - 'CLOUDSDK_COMPUTE_REGION={{{ project_0.default_region }}}' - 'CLOUDSDK_CONTAINER_CLUSTER=hello-cloudbuild' # This step copies the applied manifest to the production branch - name: 'gcr.io/cloud-builders/gcloud' id: Copy to production branch entrypoint: /bin/sh args: - '-c' - | set -x && \ git clone -b production http://giteaadmin:GiteaPassword123@${GIT_SERVER_IP}:3000/giteaadmin/hello-cloudbuild-env.git prod_repo && \ cd prod_repo && \ git config user.email "student@qwiklabs.net" && \ git config user.name "Cloud Build" && \ cp ../kubernetes.yaml kubernetes.yaml && \ git add kubernetes.yaml && \ git commit -m "Deployed manifest from commit $_COMMIT_SHA" && \ git push origin production options: logging: CLOUD_LOGGING_ONLY EOF sed -i "s/\${GIT_SERVER_IP}/$GIT_SERVER_IP/g" cloudbuild.yaml

提交更新並推送至 hello-cloudbuild-env:

cd ~/hello-cloudbuild-env git checkout candidate git add cloudbuild.yaml git commit -m "Create cloudbuild.yaml for deployment" git push http://giteaadmin:GiteaPassword123@${GIT_SERVER_IP}:3000/giteaadmin/hello-cloudbuild-env.git candidate

修改持續整合管道,觸發持續推送軟體更新管道

接著,更新 hello-cloudbuild-app 中的持續整合管道,產生新版 Kubernetes 資訊清單,並推送至 hello-cloudbuild-env 來觸發 CD 管道。

  1. 在 Cloud Shell 執行下列指令,更換 hello-cloudbuild-app 中的 cloudbuild.yaml,並填入您的 Git 伺服器 IP:
cd ~/hello-cloudbuild-app cat <<'EOF' > cloudbuild.yaml # Copyright 2018 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. substitutions: _SHORT_SHA: 'v1.0' _COMMIT_SHA: 'v1.0' steps: # This step runs the unit tests on the app - name: 'python:3.11-slim' id: Test entrypoint: /bin/sh args: - -c - 'pip install flask && python test_app.py -v' # This step builds the container image. - name: 'gcr.io/cloud-builders/docker' id: Build args: - 'build' - '-t' - '{{{ project_0.default_region }}}-docker.pkg.dev/$PROJECT_ID/my-repository/hello-cloudbuild:$_SHORT_SHA' - '.' # This step pushes the image to Artifact Registry - name: 'gcr.io/cloud-builders/docker' id: Push args: - 'push' - '{{{ project_0.default_region }}}-docker.pkg.dev/$PROJECT_ID/my-repository/hello-cloudbuild:$_SHORT_SHA' # This step clones the hello-cloudbuild-env repository - name: 'gcr.io/cloud-builders/gcloud' id: Clone env repo entrypoint: /bin/sh args: - '-c' - | git clone http://giteaadmin:GiteaPassword123@${GIT_SERVER_IP}:3000/giteaadmin/hello-cloudbuild-env.git && \ cd hello-cloudbuild-env && \ git checkout candidate && \ git config user.email "student@qwiklabs.net" && \ git config user.name "Cloud Build" # This step generates the new manifest - name: 'gcr.io/cloud-builders/gcloud' id: Generate manifest entrypoint: /bin/sh args: - '-c' - | sed "s/GOOGLE_CLOUD_PROJECT/${PROJECT_ID}/g" kubernetes.yaml.tpl | \ sed "s/COMMIT_SHA/${_SHORT_SHA}/g" > hello-cloudbuild-env/kubernetes.yaml # This step pushes the manifest back to hello-cloudbuild-env candidate branch - name: 'gcr.io/cloud-builders/gcloud' id: Push manifest entrypoint: /bin/sh args: - '-c' - | set -x && \ cd hello-cloudbuild-env && \ git add kubernetes.yaml && \ git commit -m "Deploying image {{{ project_0.default_region }}}-docker.pkg.dev/$PROJECT_ID/my-repository/hello-cloudbuild:${_SHORT_SHA} Built from commit ${_COMMIT_SHA} of repository hello-cloudbuild-app Author: $(git log --format='%an <%ae>' -n 1 HEAD)" && \ git push origin candidate options: logging: CLOUD_LOGGING_ONLY EOF sed -i "s/\${GIT_SERVER_IP}/$GIT_SERVER_IP/g" cloudbuild.yaml
  1. 提交變更並推送至 Git 伺服器,然後提交管道建構作業:
cd ~/hello-cloudbuild-app git add cloudbuild.yaml git commit -m "Trigger CD pipeline" git push http://giteaadmin:GiteaPassword123@${GIT_SERVER_IP}:3000/giteaadmin/hello-cloudbuild-app.git main gcloud builds submit --config=cloudbuild.yaml --substitutions=_SHORT_SHA=$(git rev-parse --short=7 HEAD),_COMMIT_SHA=$(git rev-parse HEAD) .

這會觸發 Cloud Build 中的持續整合管道,建構應用程式映像檔,並將新的 kubernetes.yaml 資訊清單推送至 hello-cloudbuild-env 存放區。

  1. 提取 hello-cloudbuild-env 中產生的資訊清單,並觸發持續推送軟體更新 (CD) 部署建構作業:
cd ~/hello-cloudbuild-env git pull http://giteaadmin:GiteaPassword123@${GIT_SERVER_IP}:3000/giteaadmin/hello-cloudbuild-env.git candidate gcloud builds submit --config=cloudbuild.yaml --substitutions=_COMMIT_SHA=$(git rev-parse HEAD) .

點選「Check my progress」,確認目標已達成。 建立測試環境和 CD 管道

工作 7:查看 Cloud Build 管道

在這項工作,您會前往控制台查看 Cloud Build 管道。

  1. 在控制台依序前往「Cloud Build」>「記錄」。
  2. 點選最近的建構作業,追蹤執行情況並查看記錄。
  3. 確認建構管道已成功執行所有步驟。

工作 8:測試完整管道

您已設定完整的 CI/CD 管道。在這項工作,您將執行端對端測試。

  1. 在控制台依序點選「導覽選單」圖示 「導覽選單」圖示>「Kubernetes Engine」>「閘道、Service 與 Ingress」>「服務」。

清單中應該有一個名為 hello-cloudbuild 的服務,這是由剛剛執行的持續推送軟體更新建構作業所建立。

  1. 點選「hello-cloudbuild」服務的端點後,您應該會看到「Hello World!」。如果沒有此端點,或是看到負載平衡器錯誤,可能必須等待幾分鐘,負載平衡器才會完全初始化。請視需要點選「重新整理」來更新頁面。

應用程式 1 Hello World!

  1. 在 Cloud Shell 執行下列指令,將應用程式和單元測試中的「Hello World」換成「Hello Cloud Build」:
cd ~/hello-cloudbuild-app sed -i 's/Hello World/Hello Cloud Build/g' app.py sed -i 's/Hello World/Hello Cloud Build/g' test_app.py
  1. 提交變更並推送至 Git 伺服器,然後提交 CI 建構作業:
git add app.py test_app.py git commit -m "Hello Cloud Build" git push http://giteaadmin:GiteaPassword123@${GIT_SERVER_IP}:3000/giteaadmin/hello-cloudbuild-app.git main gcloud builds submit --config=cloudbuild.yaml --substitutions=_SHORT_SHA=$(git rev-parse --short=7 HEAD),_COMMIT_SHA=$(git rev-parse HEAD) .
  1. 將更新後的資訊清單提取至 hello-cloudbuild-env,並提交部署管道建構作業,部署新的應用程式版本:
cd ~/hello-cloudbuild-env git pull http://giteaadmin:GiteaPassword123@${GIT_SERVER_IP}:3000/giteaadmin/hello-cloudbuild-env.git candidate gcloud builds submit --config=cloudbuild.yaml --substitutions=_COMMIT_SHA=$(git rev-parse HEAD) .

幾分鐘後,在瀏覽器中重新載入應用程式。現在應該會看到「Hello Cloud Build!」。

應用程式 2 Hello Cloud Build!

工作 9:測試復原作業

這項工作會將應用程式復原到顯示「Hello World!」的版本。

  1. 在控制台標題列的「搜尋」欄位輸入 Cloud Build 資訊主頁,然後點選結果中的「Cloud Build」。請確認左側窗格已選取「記錄」。
  2. 在「建構作業記錄」中,點選第二新的可用建構作業。
  3. 點選「重新建構」。

復原成功畫面

建構完成後,在瀏覽器中重新載入應用程式。現在應該會再次看到「Hello World!」。

應用程式 1 Hello World!

恭喜!

您現在可透過 Cloud Build,在 Google Cloud 為 GKE 建立及復原持續整合管道了!

Google Cloud 教育訓練與認證

協助您瞭解如何充分運用 Google Cloud 的技術。我們的課程會介紹專業技能和最佳做法,讓您可以快速掌握要領並持續進修。我們提供從基本到進階等級的訓練課程,並有隨選、線上和虛擬課程等選項,方便您抽空參加。認證可協助您驗證及證明自己在 Google Cloud 技術方面的技能和專業知識。

使用手冊上次更新日期:2026 年 7 月 20 日

實驗室上次測試日期:2026 年 7 月 20 日

Copyright 2026 Google LLC 保留所有權利。Google 和 Google 標誌是 Google LLC 的商標,其他公司和產品名稱則有可能是其關聯公司的商標。

Before you begin

  1. Labs create a Google Cloud project and resources for a fixed time
  2. Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
  3. On the top left of your screen, click Start lab to begin

Use private browsing

  1. Copy the provided Username and Password for the lab
  2. Click Open console in private mode

Sign in to the Console

  1. Sign in using your lab credentials. Using other credentials might cause errors or incur charges.
  2. Accept the terms, and skip the recovery resource page
  3. Don't click End lab unless you've finished the lab or want to restart it, as it will clear your work and remove the project

This content is not currently available

We can notify you via email when it becomes available

Great!

We can contact you via email if it becomes available

One lab at a time

Confirm to end all existing labs and start this one

Use private browsing to run the lab

Using an Incognito or private browser window is the best way to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.