自動重建容器映像檔,以便同步處理基本映像檔更新


Cloud Workstations 可讓您建立及使用工作站的自訂映像檔。使用自訂映像檔後,自動重建自訂映像檔會很有幫助,因為這樣可以從基本映像檔中提取修正檔和更新。

在本教學課程中,您將瞭解如何建構自動化管道,確保自訂工作站映像檔包含安全性更新和修補程式。

目標

按照本教學課程的步驟,為基本映像檔建構自動化管道:

  1. 建立 Artifact Registry 存放區,用於儲存及掃描自訂映像檔。
  2. 設定 GitHub Google Cloud 來儲存圖片設定。
  3. 建立 Cloud Build 觸發條件,自動建立自訂映像檔並部署至 Artifact Registry。
  4. 設定 Cloud Scheduler,定期啟動建構作業。
  5. 查看自動化程序的結果。

費用

在本文件中,您會使用 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. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Artifact Registry, Container Scanning API, Cloud Build, and Cloud Scheduler APIs.

    Enable the APIs

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  10. Enable the Artifact Registry, Container Scanning API, Cloud Build, and Cloud Scheduler APIs.

    Enable the APIs

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. To initialize the gcloud CLI, run the following command:

    gcloud init

準備環境

請務必先設定下列環境變數,再繼續操作。

  1. 設定要使用的雲端專案的專案 ID:

    PROJECT_ID=$PROJECT_ID
    
  2. 設定要儲存存放區的 GitHub 使用者名稱:

    GITHUB_USER=$GITHUB_ID
    
  3. 設定要在程序中使用的 PROJECT_NUMBERREGION 變數:

    PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID \
        --format='value(projectNumber)')
    
    REGION=$REGION
    

    在先前的範例中,請將 $REGION 替換為您打算使用的區域名稱,例如 us-central1

    如要進一步瞭解可用區域,請參閱「Cloud Workstations 服務據點」。

建立 Artifact Registry 存放區

在本教學課程中,您將使用 Artifact Registry 儲存及掃描映像檔。

  1. 使用下列指令建立存放區:

    gcloud artifacts repositories create custom-images \
          --repository-format=docker \
          --location=$REGION \
          --description="Docker repository"
    

    $REGION 替換成您打算使用的區域名稱。

  2. 將 Docker 設定為存取 Artifact Registry 時使用 gcloud CLI 憑證。

    gcloud auth configure-docker $REGION-docker.pkg.dev
    

    如要關閉構件分析功能,請執行下列指令:

    gcloud services disable containerscanning.googleapis.com
    

設定 GitHub 存放區

在實務上,您會將自訂映像檔的 Dockerfile 保存在 Git 存放區中。自動化程序會在建構程序期間存取該存放區,以提取相關設定和 Dockerfile。

為範例存放區建立分支

如要分叉提供容器定義的範例存放區,請按照下列步驟操作:

  1. 按一下這個連結,即可建立 software-delivery-workshop 存放區的新分支
  2. 如果系統提示,請登入 GitHub。
  3. 選取您的 GitHub 使用者名稱做為擁有者。存放區名稱會顯示為「software-delivery-workshop」。
  4. 按一下「建立分叉」,然後等待幾秒鐘,程序就會完成。

將 Cloud Build 連線至 GitHub

接著,使用內建的 GitHub 連線功能,將該存放區連結至 Cloud Build。按一下 GitHub 存放區的連結,然後按照操作說明完成程序。您不需要在精靈的最後一個步驟中建立觸發程序,而且可以略過最後幾個步驟,因為您稍後可以從指令列執行這項操作。

如果您使用其他 Git 存放區解決方案,也可以按照將 Cloud Build 連結至 GitLabBitbucket 的操作說明進行。

建立 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)")

這個範例會設定下列項目:

  • gcloud CLI 指令會在 Cloud Build 中建立名為 custom-image-trigger手動觸發程序,如第二行的 name 標記所示。
  • 接下來的三行包含與來源 GitHub 存放區相關的標記:
  • 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 觸發程序。在本教學課程中,這項工作每天都會執行。實務上,請根據貴機構的需求設定頻率,確保一律納入最新更新。

  1. 將必要角色授予預設服務帳戶,以叫用 Cloud Build 觸發條件:

    gcloud projects add-iam-policy-binding $PROJECT_ID \
        --member="serviceAccount:$PROJECT_NUMBER-compute@developer.gserviceaccount.com" \
        --role="roles/cloudbuild.builds.editor"
    
  2. 將必要角色授予 Cloud Build 服務帳戶,以便將映像檔上傳至 Artifact Registry:

    gcloud projects add-iam-policy-binding $PROJECT_ID \
        --member=serviceAccount:$PROJECT_NUMBER@cloudbuild.gserviceaccount.com \
        --role="roles/artifactregistry.admin"
    
  3. 使用下列指令建立 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
    
  4. 這項工作設定為每天執行一次,但如要立即測試這項功能,請從 Cloud Scheduler 手動執行工作:

    前往 Cloud Scheduler

    1. 在「Cloud Scheduler」頁面中,找出您剛建立的項目,名為「run-build」
    2. 在「動作」欄中,按一下該列的「更多」more_vert選項選單。
    3. 按一下「強制執行工作」即可手動測試系統。
    4. 指令順利執行後,請切換至 Cloud Build 歷史記錄頁面,查看進度:

      前往 Cloud Build 的「History」(記錄) 頁面

查看結果

由於您在設定過程中啟用了 Container Scanning API,Artifact Registry 會自動掃描映像檔,找出安全漏洞。

如要查看安全漏洞,請按照下列步驟操作:

  1. 開啟 Artifact Registry 存放區頁面:

    前往 Artifact Registry 的「Repositories」(存放區) 頁面

  2. 在存放區清單中,按一下存放區。

  3. 按一下映像檔名稱。每個映像檔摘要的安全漏洞總數會顯示在「Vulnerabilities」(安全漏洞) 資料欄中。

    Artifact Registry 存放區頁面,顯示範例映像檔名稱

  4. 如要查看映像檔的安全漏洞清單,請按一下「Vulnerabilities」(安全漏洞) 資料欄中的連結。安全漏洞清單會顯示嚴重性、修正可用性以及內含安全漏洞的套件名稱。

    Artifact Registry「安全漏洞」頁面,顯示安全漏洞的範例清單

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。

如要避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請務必記得刪除不再需要的資源。

如要從 Google Cloud 控制台或 gcloudCLI 刪除 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.

gcloud

    Delete a Google Cloud project:

    gcloud projects delete PROJECT_ID

如要進一步瞭解如何刪除其他資源 (例如工作站叢集、工作站設定和工作站),請參閱「刪除資源」。

後續步驟