GCP CI/CD
CI/CD 簡單說明
CI/CD Workflow
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
CI 持續整合
(1)專案程式碼提交 (commit → push / merge)
(2)Build、單元測試、整合測試、程式碼規範檢查
CD 持續交付
(3)完成程式碼部署、營運、監控
優點
避免版本衝突
降低人為操作失誤
減少人工時間花費
及早發現(bug)及早修正(fix)
專注開發提高生產力
加速產品迭代
GCP CI/CD
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Cloud Build
是一項在 Google Cloud 基礎架構上執行建置的服務。
可從 Cloud Storage、Cloud Source Repositories、GitLab、GitHub 或 Bitbucket 匯入原始程式碼。
根據您的規範執行構建,並產生 Docker 容器或 Java 存檔等工件。
Artifact Registry
用於管理私有套件和 Docker 容器映像的存放區
Container Registry 容器註冊表
2024/5/15後,已棄用,新專案無法再推送
2025/3/18後,將關閉
Cloud Deploy
可依照定義的升級順序自動將應用程式交付到一系列目標環境。
達到持續交付。
Source Repositories
託管在 Google Cloud 的 Git儲存庫
Cloud Build
實作-1
這邊會使用 Cloud Build 構建 Docker image 並將 image 推送到 GAR。
首先新增以下兩個檔案
1.quickstart.sh
2.Dockerfile
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
在 GCP Artifact Registry 建立存放區
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
執行CloudBuild
有兩種方式1.直接透過指令執行、2.透過cloudbuild.yaml檔案執行
這邊使用第2種,所以需建立cloudbuild.yaml
開始創建 docker image
成功訊息
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
實作-2
使用觸發條件連結 github 自動 CI
環境準備
github建立新的repo,git clone到本地端,放入自己的專案
並建立Dockerfile、cloudbuild.yaml 兩份檔案
Dockerfile
cloudbuild.yaml
CloudBuild 設定觸發條件、連結存放區
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
推送版本到指定的 github 存放區,觸發 CloudBuild
版本紀錄可查看觸發條件名稱
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
成功建置 image 到 GAR
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
Cloud Deploy
將應用程式部署至GKE
準備 skaffold.yaml、k8s-pod.yaml、clouddeploy.yaml
skaffold.yaml
k8s-pod.yaml
clouddeploy.yaml
使用 Cloud Deploy 服務註冊管道與目標
指令
成功訊息



建立版本
使用ui


使用指令
Deploy成功會變成綠色

一個以上目標示意圖

補充:
Q1.
觸發條件連接Google Source Repositories
A. 先在Source Repositories建立存放區,再建立觸發條件時,存放區版本第1代中可直接選擇Cloud Source Repositories,不用連結新的存放區

Q2.
Cloud Build 自動帶版號
A.
方法一、cloudbuild.yaml image的tag改代變數 $SHORT_SHA'
會自動帶入 COMMIT_SHA 前7個字符

( 還有其他方法會再補充上來 )
Q3.
目標環境同一個 GKE Cluster
A.使用 skaffold.yaml 設定變數可以區分環境
( 待補充設定方式 )
參考文件
Cloud Build
https://cloud.google.com/build/docs/build-push-docker-image
Cloud Deploy
https://cloud.google.com/deploy/docs/overview
build trigger
https://cloud.google.com/build/docs/automating-builds/create-manage-triggers#build_trigger
Build and Deploy apps with CI/CD
https://cloud.google.com/build/docs