gitlab
統一管理 gitlab-ci.yml
Table Of Content
- 統一管理 gitlab-ci.yml
- 執行步驟
- 單元測試
- Integration Test for SR team trigger
- Build Image to Artifacts Registry & Deploy To Cloud Run Development Env
- Build Image to Artifacts Registry & Deploy To Cloud Run [SR] Project Env
- Build Image to Artifacts Registry & Deploy To Cloud Run Demo Env
- Build Image to Artifacts Registry & Deploy To Cloud Run Production Env
- 相關資源
統一管理 gitlab-ci.yml
參考朋友提供的 .gitlab-ci.yml 做法,將自己的所有 .gitlab-ci.yml 使用 CI/CD Repo 統一管理建立一份獨立的 CI/CD 專案用於管理所有專案的 .gitlab-ci.yml 檔案其他專案可以直接引用,如下:
include:
- project: "devops821128/cicd"
ref: main
file:
- "/project/<PROJECT_NAME>.yml"
在 CICD 專案中可以使用專案名稱做為 yaml 檔案的名稱,單元測試的部分需要請 RD 們提供執行單元測試或是整合測試的順序和指令。
以下是在 CICD 專案內的其中一個範例
# 執行步驟
stages:
- "unit_test"
- "build"
# 單元測試
unit-test:
stage: "unit_test"
image: ${NODE_V18_IMAGE}
tags:
- shared-javascript
script: # 填寫:需要執行的指令
- cp $ENV_FILE .env
- yarn
- yarn vitest
- yarn vitest:coverage
# Integration Test for SR team trigger
integration-test:
stage: unit_test
image: ${NODE_V18_IMAGE}
tags:
- shared
script: # 填寫:需要執行的指令
- cp $ENV_FILE .env
- yarn
- yarn vitest
- yarn vitest:coverage
rules: # 需要加上這行規則,加上了才可以讓 SR team 觸發
- if: $CI_PIPELINE_SOURCE == "pipeline"
when: never
# Build Image to Artifacts Registry & Deploy To Cloud Run Development Env
dev-build-deploy:
stage: build
image: ${GOOGLE_IMAGE}
needs: ["unit-test"]
tags:
- shared-gcloud
script:
- cp $ENV_FILE .env
- echo 'VITE_ENV=dev' >> .env
- cp ${LOCAL_RPA_SA} serviceAccount.json
- gcloud auth activate-service-account --key-file=serviceAccount.json
- gcloud config set project ${RPA_PROJECT_ID}
- gcloud builds submit --region=asia-northeast1 --config=.cloudbuild/cloudbuild.yaml . --substitutions=_CLOUD_IMAGE_URL="asia-northeast1-docker.pkg.dev/${RPA_PROJECT_ID}/rpa-frontend/dev-promotion-recurl-adv:dev",_CLOUD_RUN_SVC_NAME="dev-promotion-recurl-adv",_REGION="asia-northeast1"
rules:
- if: $CI_COMMIT_BRANCH == "feature"
# Build Image to Artifacts Registry & Deploy To Cloud Run [SR] Project Env
it-build-deploy:
stage: build
image: ${GOOGLE_IMAGE}
needs: ["unit-test"]
tags:
- shared-gcloud
script:
- cp $ENV_FILE .env
- echo 'VITE_ENV=it' >> .env
- cp ${IT_BI_RPA_CICD} serviceAccount.json
- gcloud auth activate-service-account --key-file=serviceAccount.json
- gcloud config set project ${IT_PROJECT_ID}
- gcloud builds submit --region=asia-northeast1 --config=.cloudbuild/cloudbuild.yaml . --substitutions=_CLOUD_IMAGE_URL="asia-northeast1-docker.pkg.dev/${IT_PROJECT_ID}/bi-rpa/bi-rpa-redirect-url:it",_CLOUD_RUN_SVC_NAME="bi-rpa-redirect-url",_REGION="asia-northeast1"
rules:
- if: $CI_COMMIT_BRANCH == "Integration-Test"
# Build Image to Artifacts Registry & Deploy To Cloud Run Demo Env
demo-build-deploy:
stage: build
image: ${GOOGLE_IMAGE}
needs: ["unit-test"]
tags:
- shared-gcloud
script:
- cp $ENV_FILE .env
- echo 'VITE_ENV=demo' >> .env
- cp ${LOCAL_RPA_SA} serviceAccount.json
- gcloud auth activate-service-account --key-file=serviceAccount.json
- gcloud config set project ${RPA_PROJECT_ID}
- gcloud builds submit --region=asia-northeast1 --config=.cloudbuild/cloudbuild.yaml . --substitutions=_CLOUD_IMAGE_URL="asia-northeast1-docker.pkg.dev/${RPA_PROJECT_ID}/rpa-frontend/dev-promotion-recurl-adv:demo",_CLOUD_RUN_SVC_NAME="dev-promotion-recurl-adv",_REGION="asia-northeast1"
rules:
- if: $CI_COMMIT_BRANCH == "qa"
# Build Image to Artifacts Registry & Deploy To Cloud Run Production Env
production-build-deploy:
stage: build
image: ${GOOGLE_IMAGE}
needs: ["unit-test"]
tags:
- shared-gcloud
script:
- cp $ENV_FILE .env
- echo 'VITE_ENV=prod' >> .env
- cp ${LOCAL_RPA_SA} serviceAccount.json
- gcloud auth activate-service-account --key-file=serviceAccount.json
- gcloud config set project ${RPA_PROJECT_ID}
- gcloud builds submit --region=asia-northeast1 --config=.cloudbuild/cloudbuild.yaml . --substitutions=_CLOUD_IMAGE_URL="asia-northeast1-docker.pkg.dev/${RPA_PROJECT_ID}/rpa-frontend/prd-promotion-recurl-adv:prod",_CLOUD_RUN_SVC_NAME="prd-promotion-recurl-adv",_REGION="asia-northeast1"
rules:
- if: $CI_COMMIT_BRANCH == "prod"
我們可以將比較常使用的值統一放在 variable.yml 中,統一管理的好處如下
- 工程師不需要學習 gitlab-ci.yml 的做法,全都會由 SRE 或架構師管理
- 工程師可以更專注在開發上
- 可以統一管理比較好管理資訊,如:GCP 專案 ID、Service Account、環境部署、區域和 Images 版本等