使用 Cloud Build 自動執行建構作業
本頁說明如何使用 Cloud Build 自動化建構作業。
Cloud Build 會使用建構觸發條件啟用 CI/CD 自動化功能。您可以設定觸發條件,以便監聽傳入的事件,例如新的提交內容推送至存放區或發起拉取要求時,然後在收到新事件時自動叫用版本。
您將建立觸發條件,並設定該觸發條件,以便在您將變更推送至 GitHub 存放區時叫用建構作業。
如要直接在 Cloud Shell 編輯器中按照逐步指南操作,請按一下「Guide me」:
事前準備
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build API.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build API.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
如果您尚未建立帳戶,請前往 GitHub 建立帳戶。
如果您已在 GitHub 中設定雙重驗證功能,請透過指令列建立個人存取權杖來取代 GitHub 密碼。
建立範例 GitHub 存放區分支並加以複製
您需要有一些範例原始碼才能建構,在本節中,您將複製現有的來源存放區,其中包含 Go 中的 Hello World 範例,並複製到自己的 GitHub 使用者帳戶。如果您尚未建立帳戶,請前往 GitHub 建立帳戶。
如果您已在 GitHub 中設定雙重驗證功能,請透過指令列建立個人存取權杖來取代 GitHub 密碼。
建立
cloud-build-samples
存放區分支,並新增至您的 GitHub 帳戶:前往 Cloud Build 中的 cloud-build-samples 存放區。
按一下頁面右上角的「分支」圖示。
按一下要建立存放區分支的目標 GitHub 使用者帳戶。
系統會自動將您重新導向至含有
cloud-build-samples
存放區分支版本的頁面。
在本機環境中開啟終端機。
執行下列指令來複製存放區的分支版本,其中 GITHUB_USERNAME 為您的 GitHub 帳戶使用者名稱:
git clone https://github.com/GITHUB_USERNAME/cloud-build-samples.git
將 Cloud Build 連結至存放區
如要使用觸發條件在 GitHub 上建構原始碼,您必須先將 Cloud Build 連結至 GitHub 存放區。在本節中,您將將cloud-build-samples
存放區連結至 Cloud Build。在 Google Cloud 控制台導覽選單中,依序點選「Cloud Build」>「Triggers」(觸發條件)。
選取您的專案,然後按一下「Open」。
按一下
「Connect repository」(連結存放區) 。選取「Select source」(選取來源) 下方的
「GitHub (Cloud Build GitHub App)」(GitHub (Cloud Build GitHub 應用程式)) 。按一下
「Continue」(繼續) 。驗證 GitHub 帳戶。
在「Select repostory」(選取存放區) 專區中,選取
「GitHub account」(GitHub 帳戶) 的使用者名稱。如果清單中未顯示您的使用者名稱,請按照下列指示操作:按一下「GitHub account」(GitHub 帳戶) 清單中的「+Add」(+新增)。
畫面中會顯示彈出式視窗,方便您安裝 Cloud Build GitHub 應用程式。
按一下您的 GitHub 使用者名稱。
按一下「Only certain repositories」(僅限特定存放區),在特定存放區中安裝 Cloud Build GitHub 應用程式。
在下拉式選單中選取「GITHUB_USERNAME/cloud-build-samples」,其中 GITHUB_USERNAME 是您的 GitHub 帳戶使用者名稱。
按一下 [安裝]。
在繼續操作之前,系統可能會要求您輸入與 GitHub 帳戶相關聯的密碼。
安裝 Cloud Build GitHub 應用程式之後,您就能在「GitHub account」(GitHub 帳戶) 欄位的下拉式選單中看見自己的使用者名稱。選取您的使用者名稱。
選取
「Repository」(存放區) 下方的「GITHUB_USERNAME/cloud-build-samples」這個存放區。按一下勾號來同意觸發條件連線的條款及細則。
按一下
「Connect」(連結) 。按一下
「Done」(完成) 。
建立觸發條件
在 Google Cloud 控制台中開啟「Triggers」頁面:
在頁面頂端的專案選取器下拉式選單中選取專案。
按一下「開啟」。
按一下「Triggers」(觸發條件) 頁面中的
「Create trigger」(建立觸發條件) 。在「Create trigger」(建立觸發條件) 頁面中輸入下列設定:
「Name」(名稱):輸入
hello-world-trigger
來當做觸發條件的名稱。「Event」(事件):選取
「Push to a branch」(推送至分支版本) 來當做叫用觸發條件的存放區事件。「Source」(來源) :選取「cloud-build-samples
」存放區來當做來源,當中包含原始碼和建構設定檔。「Build Configuration」(建構設定):選取
「Cloud Build configuration file」(Cloud Build 設定檔) 來當做建構設定檔。「Cloud Build configuration file location」(Cloud Build 設定檔位置) :將 Cloud Build 設定檔的路徑指定為quickstart-automate/cloudbuild.yaml
。
按一下
「Create」(建立) 來儲存自動建構觸發條件。
修訂變更
在本節中,您將在自己的 GitHub 帳戶中,將變更提交至複製的 cloud-build-samples
存放區。
在終端機中前往
quickstart-automate
目錄:cd cloud-build-samples/quickstart-automate
開啟
main.go
檔案,並更新包含「Hello, world!」的程式碼行。改為「Hello, universe!」package main import ( "fmt" ) func main() { fmt.Println("Hello, universe!") }
查看
,這是 Cloud Build 使用的建構設定檔。透過觸發條件叫用建構作業時,這個檔案中的建構步驟會指示 Cloud Build 使用 Docker Hub 中的cloudbuild.yaml
golang
映像檔來建立及執行main.go
檔案。steps: - name: golang script: go run quickstart-automate/main.go
返回存放區的根目錄:
cd ..
執行下列指令來修訂 GitHub 變更:
git add quickstart-automate/main.go git commit -m "update text" git push
將程式碼推送至存放區時,系統可能會提示您輸入憑證。如果系統顯示提示訊息,請輸入您的使用者名稱和密碼或驗證權杖。
您已成功將變更推送至存放區,接著觸發條件會自動執行建構作業。
查看建構作業詳細資料
在本節中,您將查看變更提交後與叫用建構相關聯的建構詳細資料。在 Google Cloud 控制台導覽選單中,依序點選「Cloud Build」>「History」(記錄)。
選取您的專案並按一下 [Open] (開啟)。
畫面會顯示「Build history」(建構歷史記錄) 頁面:
在
「Build」(建構作業) 欄中,按一下建構作業的名稱。按一下「Build details」(建構作業詳細資料) 頁面中的
「Build Artifacts」(建構作業構件) 。畫面會顯示類似以下的輸出:
如要查看建構作業的記錄檔,請點選
「下載」圖示 並瀏覽下載的檔案。
您已成功使用觸發條件叫用 Cloud Build 建構作業,並查看建構作業的詳細資料。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取您在本頁所用資源的費用,請按照下列步驟操作。
在 Google Cloud 控制台導覽選單中,依序點選「Cloud Build」>「Triggers」(觸發條件)。
選取您的專案並按一下「Open」。
在
hello-world-trigger
列中,按一下資料列最右側的動作選單 (垂直刪節號)。選取 [刪除]。
您已刪除與存放區副本相關聯的觸發條件。
後續步驟
- 瞭解如何建立基本的建構設定檔。
- 瞭解如何建立及管理自動建構觸發條件。