Cloud Workstations 可讓您為工作站建立及使用自訂映像檔。自訂映像檔投入使用後,建議您自動重新建構自訂映像檔,以便提取基礎映像檔中的修正和更新。
在本教學課程中,您將瞭解如何建構自動化管道,確保自訂工作站映像檔包含安全性更新和修補程式。
目標
按照本教學課程的指示,您可以透過下列步驟為基礎映像檔建構自動化管道:
- 建立 Artifact Registry 存放區,用於儲存及掃描自訂映像檔。
- 使用 Google Cloud 設定 GitHub,儲存映像檔設定。
- 建立 Cloud Build 自動建構觸發條件,自動建立自訂映像檔並部署至 Artifact Registry。
- 設定 Cloud Scheduler,定期啟動建構作業。
- 查看自動化程序的結果。
費用
在本文件中,您會使用下列 Google Cloud計費元件:
您可以使用 Pricing Calculator,根據預測用量估算費用。
完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」。
事前準備
- 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎 建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry, Container Scanning API, Cloud Build, and Cloud Scheduler APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the
gcloudCLI with your federated identity. -
To initialize the
gcloudCLI, run the following command:gcloud init -
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry, Container Scanning API, Cloud Build, and Cloud Scheduler APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the
gcloudCLI with your federated identity. -
To initialize the
gcloudCLI, run the following command:gcloud init
準備環境
繼續操作前,請務必設定下列環境變數。
設定要使用的雲端專案 ID:
PROJECT_ID=$PROJECT_ID設定要儲存存放區的 GitHub 使用者名稱:
GITHUB_USER=$GITHUB_ID設定要在程序中使用的
PROJECT_NUMBER和REGION變數:PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID \ --format='value(projectNumber)') REGION=$REGION在上述範例中,請將 $REGION 替換為您要使用的區域名稱,例如
us-central1。如要進一步瞭解可用的區域,請參閱「Cloud Workstations 位置」。
建立 Artifact Registry 存放區
在本教學課程中,您將使用 Artifact Registry 儲存及掃描映像檔。
使用下列指令建立存放區:
gcloud artifacts repositories create custom-images \ --repository-format=docker \ --location=$REGION \ --description="Docker repository"將 $REGION 替換為您要使用的區域名稱。
設定 Docker,在存取 Artifact Registry 時使用
gcloudCLI 憑證。gcloud auth configure-docker $REGION-docker.pkg.dev如要關閉 Artifact Analysis,請執行下列指令:
gcloud services disable containerscanning.googleapis.com
設定 GitHub 存放區
在實際操作中,您會將自訂映像檔的 Dockerfile 儲存在 Git 存放區中。自動化程序會在建構程序期間存取該存放區,提取相關設定和 Dockerfile。
為範例存放區建立分支
如要建立提供容器定義的範例存放區分支版本,請按照下列步驟操作:
- 按一下這個連結,建立
software-delivery-workshop存放區的新分支版本。 - 如果出現提示,請登入 GitHub。
- 選取您的 GitHub 使用者名稱做為擁有者。存放區名稱會顯示為
software-delivery-workshop。 - 按一下「Create fork」,稍等幾秒鐘,讓程序完成。
將 Cloud Build 連結至 GitHub
接著,使用內建的 GitHub 連線功能,將該存放區連結至 Cloud Build。按一下 GitHub 存放區連結,並按照操作說明完成程序。您不需要在精靈的最後一個步驟中建立觸發條件,也可以略過最後一個步驟,因為您稍後可以透過指令列執行這項操作。
如果您使用其他 Git 存放區解決方案,也可以按照操作說明,將 Cloud Build 連結至 GitLab 或 Bitbucket。
建立 Cloud Build 自動建構觸發條件
範例存放區包含容器定義和 Cloud Build 設定,用於建構容器映像檔。在這個步驟中,您將建立 Cloud Build 自動建構觸發條件,執行 labs/cloudbuild-scheduled-jobs/code-oss-java 資料夾中 cloudbuild.yaml 檔案的指令。
gcloud builds triggers create manual \
--name=custom-image-trigger \
--repo=$GITHUB_USER/software-delivery-workshop \
--repo-type=GITHUB \
--branch=main \
--build-config=labs/cloudbuild-scheduled-jobs/code-oss-java/cloudbuild.yaml \
--substitutions=_REGION=$REGION,_AR_REPO_NAME=custom-images,_AR_IMAGE_NAME=code-oss-java,_IMAGE_DIR=labs/cloudbuild-scheduled-jobs/code-oss-java
TRIGGER_ID=$(gcloud builds triggers list \
--filter=name="custom-image-trigger" --format="value(id)")
這個範例會設定下列項目:
gcloudCLI 指令會在 Cloud Build 中建立名為custom-image-trigger的手動觸發條件,如第二行的name旗標所示。- 接下來的三行包含與來源 GitHub 存放區相關的旗標:
- 存放區路徑
- 存放區類型
- 要建構的 Git 分支版本
build-config旗標代表 Git 存放區中 Cloud Build 檔案的路徑。如要讓工作變成動態,請使用
substitutions旗標。在這個工作中,指令會傳入下列變數:- 區域:
$_REGION - Artifact Registry 存放區名稱:
$_AR_REPO_NAME - 容器映像檔名稱:
$_AR_IMAGE_NAME - 要建構的 Dockerfile 位置,
$_IMAGE_DIR
查看 cloudbuild.yaml 檔案,瞭解這些變數在程序中的使用方式。
- 區域:
觸發條件建立後,系統會擷取觸發條件的不重複名稱,並儲存在
$TRIGGER_ID環境變數中,供日後使用。
設定 Cloud Scheduler
為確保映像檔已套用最新更新和修補程式,請使用 Cloud Scheduler,按照設定的頻率執行 Cloud Build 觸發條件。在本教學課程中,工作每天都會執行。在實際操作中,請根據組織需求設定頻率,確保一律納入最新更新。
將必要角色授予預設服務帳戶,以叫用 Cloud Build 自動建構觸發條件:
gcloud projects add-iam-policy-binding $PROJECT_ID \ --member="serviceAccount:$PROJECT_NUMBER-compute@developer.gserviceaccount.com" \ --role="roles/cloudbuild.builds.editor"將必要角色授予 Cloud Build 服務帳戶,以便將映像檔上傳至 Artifact Registry:
gcloud projects add-iam-policy-binding $PROJECT_ID \ --member=serviceAccount:$PROJECT_NUMBER@cloudbuild.gserviceaccount.com \ --role="roles/artifactregistry.admin"使用下列指令建立 Cloud Scheduler 工作:
gcloud scheduler jobs create http run-build \ --schedule='0 1 * * *' \ --uri=https://cloudbuild.googleapis.com/v1/projects/$PROJECT_ID/locations/global/triggers/$TRIGGER_ID:run \ --location=us-central1 \ --oauth-service-account-email=$PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --oauth-token-scope=https://www.googleapis.com/auth/cloud-platform這項工作設定為每天執行一次,但如要立即測試功能,請從 Cloud Scheduler 手動執行工作:
- 在「Cloud Scheduler」頁面中,找到您剛建立的 run-build 項目。
- 在「Actions」(動作) 欄中,按一下該列的「more_vert」more_vert「More」(更多) 選項選單。
- 按一下「Force a job run」,手動測試系統。
指令執行成功後,請切換至 Cloud Build 記錄頁面查看進度:
查看結果
由於您在設定過程中啟用了容器掃描 API,Artifact Registry 會自動掃描映像檔,找出安全漏洞。
如要查看安全漏洞,請按照下列步驟操作:
開啟 Artifact Registry 的「Repositories」(存放區) 頁面:
在存放區清單中,按一下存放區。
按一下映像檔名稱。「Vulnerabilities」(安全漏洞) 欄會顯示各個映像檔摘要的安全漏洞總數。

如要查看映像檔的安全漏洞清單,請按一下「Vulnerabilities」(安全漏洞) 欄中的連結。安全漏洞清單會顯示嚴重性、是否有修正程式,以及含有安全漏洞的套件名稱。

清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 帳戶收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
為避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請務必刪除不再需要的資源。
如要透過 Google Cloud 控制台或gcloud CLI 刪除 Google Cloud 專案,請按照下列步驟操作:
控制台
- 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。
- 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
- 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。
gcloud
刪除 Google Cloud 專案:
gcloud projects delete PROJECT_ID
後續步驟
- 查看可用的預先設定基本映像檔清單。
- 自訂容器映像檔。
- 查看可用的機型。
- 設定安全性最佳做法。
- 查看 Google Cloud 的參考架構、圖表和最佳做法。歡迎瀏覽我們的 Cloud Architecture Center。