charles
Charles
gitlab

Stages: unit-test (Python)

Stages: unit-test (Python)
0 views
2 min read
#gitlab

開始需注意:

  • Group Runner 已經建立成功
  • 在專案的根目錄上新增 .gitlab-ci.yml 檔案
  • 如果還沒有建立 Group Runner 這有

Q:什麼是 .gitlab-ci.yml

A:Gitlab CI/CD Pipeline 是 Gitlab 平台上的一個自動化流程,其中包括了多個階段,如建置、測試、封裝、部署等。 開發人員可以通過在

目前在 gitlab-ci.yaml 只有做 test 和 build stage。

deploy 階段會在 cloudbuild.yaml 中執行。

Stage 是控制要執行的循序 unit test, coverage, build 和 deploy

  • stages: 階段可以包含零個、一個或多個要執行的作業
  • test stages: 做 Unit-test 單元測試
  • build stages: 使用 docker ci build images
  • deploy stages:是 deploy 至到 GCP Cloud Run / GKE 上
# This is a sample .gitlab-ci.yml file
image: python:3.9
variables:
  ARTIFACT_REGISTRY_AREA: "aus-central1-docker.pkg.dev/<GCP_PROJECT_ID>/web-push-repository/"
  ARTIFACT_REGISTRY_PROJECT_NAME: "api-test"
  VERSION: "DevCICDTest01"
  MAIN_FOLDER: "app/"
  TEST_FOLDER: "tests/"
  MIN_COVERAGE_RATE: 50
stages:
  - test
  - build
test-coverage:
  stage: test
  tags:
    - shared
  script:
    - pip install --upgrade pip
    - pip install -r requirements.txt
    - python -m pytest --cov="$MAIN_FOLDER" "$TEST_FOLDER" --cov-fail-under="$MIN_COVERAGE_RATE"
deploy-dev:
  stage: build
  image: google/cloud-sdk
  tags:
    - shared
  services:
    - docker:dind
  script:
    - gcloud auth activate-service-account --key-file 123_cloud_build_sa.json
    - gcloud config set project <GCP_PROJECT_ID>
    - gcloud builds submit . --config=cloudbuild.yaml
  rules:
    - if: $CI_COMMIT_BRANCH == "feature"

注意:這裡有一個滿重要的點,就是需要在專案上加上自己專案的 Service Account (如果有跨專案的狀況)在 gitlab-ci.yml 部署到自己家的 GCP 專案上需要使用自己家的 Service Account