Cơ Bản Về Pipeline
Cách Pipeline Hoạt Động
PipelineContainer cấp cao nhất; một pipeline cho mỗi commit/trigger
StageNhóm các job chạy song song
JobTác vụ đơn lẻ (script) trong một stage
RunnerAgent thực thi các job
Kích Hoạt Pipeline
Push to branchTự động (mặc định)
Merge requestQua workflow:rules hoặc only: merge_requests
ScheduleCI/CD → Schedules trong cài đặt dự án
APIPOST /projects/:id/trigger/pipeline
ManualNút Run Pipeline trong menu CI/CD
.gitlab-ci.yml
Cấu Hình Tối Giản
stages: [build, test, deploy] build-job: stage: build script: echo "Compiling..."
Từ Khóa Toàn Cục
stagesĐịnh nghĩa thứ tự stage
defaultGiá trị mặc định cho tất cả job
variablesBiến CI/CD toàn cục
workflowKiểm soát khi nào pipeline được tạo
includeImport file YAML bên ngoài
Include Templates
include: - template: Auto-DevOps.gitlab-ci.yml - local: .ci/lint.yml - project: 'group/shared-ci' file: '/templates/deploy.yml'
Jobs
Định Nghĩa Job
test-unit: stage: test image: node:20 script: - npm ci - npm test
Từ Khóa Job
scriptLệnh shell cần chạy (bắt buộc)
before_scriptLệnh chạy trước script chính
after_scriptLệnh chạy sau (kể cả khi thất bại)
imageDocker image cho job
rulesĐiều kiện để đưa job vào
needsPhụ thuộc DAG (bỏ qua thứ tự stage)
allow_failurePipeline tiếp tục nếu job thất bại
retrySố lần tự động thử lại (0-2)
timeoutThời gian tối đa cho job
Rules
deploy: rules: - if: '$CI_COMMIT_BRANCH == "main"' when: manual - if: '$CI_PIPELINE_SOURCE == "merge_request_event"' when: never - when: on_success
Stages
Thứ Tự Stage
stages: - lint - build - test - deploy
Stage Mặc Định
.preLuôn chạy đầu tiên
buildStage mặc định 1
testStage mặc định 2
deployStage mặc định 3
.postLuôn chạy cuối cùng
DAG với needs
test-api: stage: test needs: ["build-api"] # skip waiting for full stage test-web: stage: test needs: ["build-web"] # runs as soon as build-web done
Variables
Định Nghĩa Biến
variables: NODE_ENV: "production" DB_HOST: "postgres" job: variables: NODE_ENV: "test" # job-level override
Biến Định Sẵn
CI_COMMIT_SHAHash commit đầy đủ
CI_COMMIT_BRANCHTên branch
CI_COMMIT_TAGTên tag (nếu là tag pipeline)
CI_PIPELINE_IDID pipeline duy nhất
CI_PROJECT_DIRĐường dẫn checkout repo
CI_MERGE_REQUEST_IIDSố MR (chỉ cho MR pipeline)
CI_REGISTRY_IMAGEĐường dẫn image container registry
Protected & Masked
ProtectedChỉ khả dụng trên branch/tag được bảo vệ
MaskedẨn trong log job
FileGhi vào file tạm; đường dẫn trong biến
Artifacts
Lưu Artifacts
build: script: npm run build artifacts: paths: [dist/] expire_in: 1 week
Loại Artifact
pathsFile/thư mục cần lưu
excludeMẫu cần bỏ qua
expire_inTự động xóa sau khoảng thời gian
reports:junitJUnit XML cho tóm tắt test trong MR
reports:coverage_reportTrực quan hóa coverage Cobertura
JUnit Report
test: script: pytest --junitxml=report.xml artifacts: reports: junit: report.xml
Cache
Cache Dependencies
test: cache: key: ${CI_COMMIT_REF_SLUG} paths: [node_modules/] script: npm ci && npm test
Cache vs Artifacts
CacheTăng tốc job; không đảm bảo; tái sử dụng cùng key
ArtifactsTruyền file giữa jobs/stages; đảm bảo
Cache Policies
pull-pushTải xuống + tải lên (mặc định)
pullChỉ tải xuống (nhanh hơn cho consumer)
pushChỉ tải lên (cho producer)
Environments
Định Nghĩa Environments
deploy-staging: stage: deploy environment: name: staging url: https://staging.example.com script: ./deploy.sh staging
Tính Năng Environment
nameTên environment (hiển thị trong UI)
urlLink đến ứng dụng đã triển khai
on_stopJob chạy khi environment bị dừng
auto_stop_inTự động dừng sau khoảng thời gian
action: stopĐánh dấu job là hành động dừng
Review Apps
review: environment: name: review/$CI_COMMIT_REF_SLUG url: https://$CI_COMMIT_REF_SLUG.example.com on_stop: stop-review auto_stop_in: 1 week
Docker
Build & Push Image
build-image: image: docker:24 services: [docker:24-dind] script: - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA . - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
Services (Sidecar Containers)
test: image: python:3.12 services: - postgres:16 - redis:7 variables: POSTGRES_DB: testdb POSTGRES_PASSWORD: secret
Docker-in-Docker
docker:24-dindImage DinD service
DOCKER_TLS_CERTDIRĐặt '/certs' hoặc '' để cấu hình TLS
DOCKER_HOSTtcp://docker:2376 (TLS) hoặc :2375
Mẫu Phổ Biến
Monorepo (changes)
test-api: rules: - changes: [api/**/*] test-web: rules: - changes: [web/**/*]
Manual Deploy Gate
deploy-prod: stage: deploy when: manual rules: - if: '$CI_COMMIT_BRANCH == "main"'
Parallel Matrix
test: parallel: matrix: - PYTHON: ["3.10", "3.11", "3.12"] DB: ["postgres", "sqlite"] script: tox -e py${PYTHON}-${DB}