用於iGaming項目的GitLab CI/CD
(部分: 技術和基礎設施)
簡短摘要
GitLab CI/CD是iGaming應用程序,分析和ML服務的「輸送機」。它結合了以下內容:存儲庫,作為代碼的pipline,環境和安全管理,自己的容器/數據包註冊表,與Kubernetes和Terraform的集成以及漏洞和許可證掃描。成功的關鍵是相同的吹笛模式、帶有自動滑板的短暫跑步者、嚴格的權利和秘密模式、GitOps流程和成本控制。
1)架構和角色
GitLab(SaaS或Self-Managed):組/項目,受保護的分支機構/標題,Merge請求應用程序。
Runners: Docker/Kubernetes/Virtual Machine executors.K8s中的短暫波達可最大程度地減少介質的漂移。
寄存器:Container/Package/Dependency Proxy-緩存基本映像和依賴項。
Observability: job logs, job artifacts, pipeline insights,將指標導出到監控。
角色:開發人員(MR),主流人員(approve/release),SecOps(掃描策略),Platform/DevOps(跑步者,模板,GitOps)。
2)基礎知識'.gitlab-ci。yml': 階段、規則、依賴性
yaml stages: [lint, test, build, security, package, deploy]
variables:
DOCKER_DRIVER: overlay2
IMAGE: "$CI_REGISTRY_IMAGE/app:$CI_COMMIT_SHA"
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
.default:
image: alpine:3. 20 before_script: [ 'apk add --no-cache bash curl jq' ]
lint:
stage: lint script: [ "make lint" ]
rules: [ { if: '$CI_PIPELINE_SOURCE == "merge_request_event"' } ]
unit:
stage: test script: [ "make test" ]
artifacts:
when: always reports: { junit: "reports/junit. xml" }
needs: [ "lint" ]
build_image:
stage: build image: docker:27 services: [ 'docker:27-dind' ]
variables: { DOCKER_TLS_CERTDIR: "" }
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker build -t $IMAGE.
- docker push $IMAGE cache:
key: "docker-${CI_COMMIT_REF_SLUG}"
paths: [ "/var/lib/docker" ]
policy: pull-push needs: [ "unit" ]
實踐:
- 分支/MR/標簽的「規則」;DAG並發的「needs」;JUnit/coverage的「artifacts: reports」;「工作流」-不運行多余的管道。
3)跑步者和自動滑板
Kubernetes executor(推薦)
短暫的配額,CPU/RAM配額,nodeSelector/taints,保密。
緩存/工件: 對象存儲;dependency proxy для NPM/Maven/PyPI/Docker.
Docker executor
一個簡單的開始;使用DinD或Kaniko/BuildKit進行無權限構建。
提示:- 按負載類型(Build/Test/Security/ML)分開的Runner池;對組/項目的約束限制;排行榜(「k8s」,「gpu」,「security」)。
4)緩存,工件和矩陣
yaml cache:
key: "pip-${CI_COMMIT_REF_SLUG}"
paths: [ "venv/", ".cache/pip/" ]
policy: pull-push
test:py:
stage: test parallel:
matrix:
- PY: ["3. 10", "3. 12"]
image: python:${PY}
script:
- python -m venv venv &&. venv/bin/activate
- pip install -r requirements. txt
- pytest -q
使用全局dependency proxy來節省流量和時間,通過矩陣進行分解測試,artifacts:expire_in用於衛生。
5)安全性和合規性(Shift-Left)
典型的「安全階段」:yaml sast:
stage: security image: registry. gitlab. com/security-products/sast:latest script: [ "analyzer run" ]
artifacts: { reports: { sast: "gl-sast-report. json" } }
rules: [ { if: '$CI_PIPELINE_SOURCE == "merge_request_event"' } ]
secret_detection:
stage: security image: registry. gitlab. com/security-products/secret-detection:latest script: [ "analyzer run" ]
artifacts: { reports: { secret_detection: "gl-secret-report. json" } }
sbom:
stage: security image: alpine:3. 20 script:
- apk add syft cosign
- syft $IMAGE -o cyclonedx-json > sbom. json
- cosign sign --key $COSIGN_KEY $IMAGE artifacts:
reports: { cyclonedx: "sbom. json" }
另外:用於看臺的DAST,Dependency/License Compliance,在關鍵設置中強制執行MR,掩蓋變量。
6)環境,評論Apps和發布
yaml review:
stage: deploy image: bitnami/kubectl environment:
name: review/$CI_COMMIT_REF_SLUG url: https://$CI_COMMIT_REF_SLUG. apps. example. com on_stop: stop_review script:
-./deploy. sh --env=review --image=$IMAGE rules: [ { if: '$CI_PIPELINE_SOURCE == "merge_request_event"' } ]
stop_review:
stage: deploy when: manual environment:
name: review/$CI_COMMIT_REF_SLUG action: stop script: [ "./deploy. sh --env=review --delete" ]
Release/Tag pipline: Helm圖表/工件發布,發行說明生成,映像簽名。
7) Progressive delivery: canary/blue-green
yaml deploy_canary:
stage: deploy script: [ "./helm_upgrade. sh --set canary. weight=10 --image=$IMAGE" ]
environment: { name: production }
rules: [ { if: '$CI_COMMIT_TAG' } ]
promote_100:
stage: deploy when: manual script: [ "./helm_upgrade. sh --set canary. weight=100" ]
needs: [ "deploy_canary" ]
添加quality gates: SLO latency/error-rate從監控→分辨率/回滾。
8)家長/兒童和多項目管道
父母/兒童:加速大型單體(每個組件都是兒童管道)。
yaml trigger_components:
stage: build trigger:
include: [ "ci/component-a. yml", "ci/component-b. yml" ]
strategy: depend
Multi-Project:「發布」項目將CD觸發到manifor-repo(GitOps)中。
9) GitOps и Terraform/IaC
GitOps通過MR進入清單存儲庫
yaml gitops_bump:
stage: deploy image: alpine/git script:
- git clone $MANIFESTS_REPO manifests
- yq -i '.image = env(IMAGE)' manifests/apps/app/values. yaml
- cd manifests && git commit -am "bump $CI_COMMIT_SHA" && git push origin HEAD:$TARGET_BRANCH
Terraform в CI
yaml terraform:
stage: deploy image: hashicorp/terraform:1. 9 script:
- terraform init -backend-config="bucket=$TF_BUCKET"
- terraform plan -out tfplan
- terraform apply -auto-approve tfplan rules: [ { if: '$CI_COMMIT_BRANCH == "infra"'} ]
10)秘密和可用性
CI Variables: masked/protected;按環境/組劃分。
受保護的分支機構/標簽:在prod中丟棄-僅來自受保護的分支機構,並帶有手動確認。
外部秘密:與Secrets Manager/HashiCorp Vault (JWT/OIDC)集成,僅在工作時間內安裝到跑步機上。
11)piplines和SLO的可觀察性
Pipeline DORA/KPI: lead time, deployment frequency, change fail rate, MTTR.
工具:retrai/taymauts,用於非阻塞任務的「allow_failure」,代碼覆蓋報告。
導出指標:階段持續時間,排隊,成功率;ChatOps中的異常值。
12) FinOps: 成本和性能
Dependency Proxy+Docker依存關系和層緩存。
Runner池(prod/security/ML)與concurrency限制分離。
自動暫停Review Apps和非活動環境;'artifacts: expire_in'。
大型裝配在spot/premptable池上;預先預熱基本圖像。
13) iGaming案例模板
Backend/API服務
yaml include: [ "ci/includes/security. yml", "ci/includes/docker. yml" ]
deploy_prod:
stage: deploy environment: { name: production, url: https://api. example. com }
script: [ "./helm_upgrade. sh --env=prod --image=$IMAGE" ]
rules: [ { if: '$CI_COMMIT_TAG' } ]
ETL/DBT模型
yaml dbt_run:
stage: build image: ghcr. io/dbt-labs/dbt-snowflake:latest script: [ "dbt deps", "dbt run --profiles-dir. ", "dbt test" ]
artifacts: { paths: [ "target/" ], expire_in: 3 days }
ML/LLM工件
yaml ml_pack:
stage: package image: nvidia/cuda:12. 1. 0-runtime-ubuntu22. 04 tags: [ "gpu" ]
script:
- python export_onnx. py
- trtexec --onnx=model. onnx --saveEngine=model. plan artifacts: { paths: [ "model. plan", "model. onnx" ] }
14)實施支票
1.為命令(lint/test/build/security/deploy)定義管道模板和共享包絡。
2.展開短暫的K8s Runner,啟用dependency proxy(用於工件/緩存的對象存儲)。
3.輸入rules/needs/DAG、矩陣和並行性。
4.根據策略配置SAST/DAST/Secret/SBOM/許可證和 MR應用程序。
5.組織Environments/Review Apps、自動瀏覽和整潔URL。
6.啟用GitOps:一個單獨的清單回購,MR圖像/圖表卷。
7.提供保密管理(masked/Protected, Vault/OIDC),保護品牌/標簽。
8.連接Terraform/IaC和「監視代碼」。
9.輸入FinOps實踐:預約限制、緩存/代理、人工制品的曝光、展臺自動掛載。
10.常規遊戲日:跑步者下降,高速緩存填充,註冊表不可用。
15)反模式
一個沒有隔離和配額的「通用」跑步者。
沒有「規則」(始終運行),沒有「需要」(緩慢)的管道。
無限制的prod ranners中的特權DinD裝配。
將秘密存儲在存儲庫/工作日誌中。
缺乏安全階段和MR應用。
無限評論Apps沒有「on_stop」和「expire_in」。
在沒有受保護標簽的prod中手動發布。
結果
GitLab CI/CD為iGaming團隊提供了快速且可預測的版本:單個模板,自動滑行跑步者,高質量的安全門,環境和漸進式解鎖,GitOps和Terraform集成。添加可觀察性和FinOps-您的應用程序、ETL和ML服務將定期發布,安全且成本可控。