Gitlab_ci
Chapter 49: GitLab CI
Section titled “Chapter 49: GitLab CI”GitLab CI is GitLab’s built-in CI/CD platform for automating builds, tests, and deployments.
GitLab CI Overview
Section titled “GitLab CI Overview”┌─────────────────────────────────────────────────────────────────────────────┐│ GitLab CI Overview │├─────────────────────────────────────────────────────────────────────────────┤│ ││ ┌─────────────────────────────────────────────────────────────────────┐ ││ │ GitLab CI Architecture │ ││ │ │ ││ │ ┌─────────────────────────────────────────────────────────────┐ │ ││ │ │ GitLab Instance │ │ ││ │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │ ││ │ │ │ Git │ │ CI/CD │ │ Registry │ │ │ ││ │ │ │ Repo │ │ Engine │ │ (Docker) │ │ │ ││ │ │ └─────────────┘ └──────┬──────┘ └─────────────┘ │ │ ││ │ └───────────────────────────┼─────────────────────────────┘ │ ││ │ │ │ ││ │ ▼ │ ││ │ ┌─────────────────────────────────────────────────────────────┐ │ ││ │ │ GitLab Runners │ │ ││ │ │ ┌────────────┐ ┌────────────┐ ┌────────────┐ │ │ ││ │ │ │ Shared │ │ Group │ │ Specific │ │ │ ││ │ │ │ Runners │ │ Runners │ │ Runners │ │ │ ││ │ │ └────────────┘ └────────────┘ └────────────┘ │ │ ││ │ │ │ │ ││ │ │ Can be: Linux, macOS, Windows, Kubernetes, Docker │ │ ││ │ │ │ │ ││ │ └─────────────────────────────────────────────────────────────┘ │ ││ │ │ ││ └──────────────────────────────────────────────────────────────────────┘ ││ ││ Key Features: ││ ✓ Built into GitLab ││ ✓ .gitlab-ci.yml in repository ││ ✓ Auto DevOps ││ ✓ Docker registry integrated ││ ✓ Review apps ││ │└─────────────────────────────────────────────────────────────────────────────┘GitLab CI Configuration
Section titled “GitLab CI Configuration”stages: - build - test - deploy
variables: DOCKER_DRIVER: overlay2 MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
before_script: - echo "Before script section"
after_script: - echo "After script section"
build: stage: build image: maven:3.8-openjdk-11 script: - mvn clean package -DskipTests artifacts: paths: - target/*.jar expire_in: 1 hour cache: paths: - .m2/repository policy: pull-push
test: stage: test image: maven:3.8-openjdk-11 script: - mvn test artifacts: when: always reports: junit: target/surefire-reports/*.xml coverage: '/Total: .* ([\d\.]+)%/'
deploy-staging: stage: deploy script: - echo "Deploying to staging..." environment: name: staging url: https://staging.example.com only: - develop
deploy-production: stage: deploy script: - echo "Deploying to production..." environment: name: production url: https://example.com when: manual only: - mainJobs and Stages
Section titled “Jobs and Stages”stages: - build - test - deploy
# Job templates (YAML anchors).base-job: image: node:18 cache: key: ${CI_COMMIT_REF_SLUG} paths: - node_modules/
build: extends: .base-job stage: build script: - npm ci - npm run build artifacts: paths: - dist/
test:unit: extends: .base-job stage: test script: - npm ci - npm run test:unit coverage: '/Lines\s*:\s*(\d+\.\d+)%/'
test:e2e: extends: .base-job stage: test services: - name: postgres:14 alias: db - name: redis:7 alias: cache variables: POSTGRES_DB: test POSTGRES_USER: test POSTGRES_PASSWORD: test script: - npm ci - npm run test:e2e
deploy: stage: deploy script: - echo "Deploying..."Docker and Containers
Section titled “Docker and Containers”# .gitlab-ci.yml for Dockerstages: - build - test - push - deploy
variables: IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
build: stage: build image: docker:24 services: - docker:24-dind script: - docker build -t $IMAGE_TAG .
test: stage: test image: $IMAGE_TAG script: - docker-compose up -d - docker-compose exec app npm test - docker-compose down
push: stage: push image: docker:24 services: - docker:24-dind script: - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY - docker push $IMAGE_TAG - docker push $CI_REGISTRY_IMAGE:latest only: - main - develop
deploy: stage: deploy image: bitnami/kubectl:latest script: - kubectl config set-cluster production --server=$KUBE_SERVER --certificate-authority=$KUBE_CA - kubectl config set-credentials deployer --token=$KUBE_TOKEN - kubectl config use-context production - kubectl set image deployment/myapp myapp=$IMAGE_TAG only: - mainMatrix and Parallel Jobs
Section titled “Matrix and Parallel Jobs”# .gitlab-ci.yml with matrixstages: - test - build
test:matrix: stage: test image: node:18 parallel: matrix: - NODE_VERSION: [14, 16, 18, 20] OPERATING_SYSTEM: [ubuntu, debian] script: - echo "Testing with Node $NODE_VERSION on $OPERATING_SYSTEM" allow_failure: true
build:matrix: stage: build image: docker:24 parallel: matrix: - ARCHITECTURE: [amd64, arm64] TAG: [latest, stable] script: - echo "Building for $ARCHITECTURE with tag $TAG" - docker buildx build --platform linux/$ARCHITECTURE -t myapp:$TAG-$ARCHITECTURE .GitLab Runner Configuration
Section titled “GitLab Runner Configuration”# .gitlab-ci.yml with runner tagsstages: - build
# Use specific runnerbuild:linux: stage: build tags: - linux script: - echo "Running on Linux runner"
# Use docker runnerbuild:docker: stage: build tags: - docker image: docker:24 services: - docker:24-dind script: - docker build .
# Use kubernetes runnerbuild:k8s: stage: build tags: - kubernetes script: - kubectl get podsEnvironments and Deployments
Section titled “Environments and Deployments”# .gitlab-ci.yml with environmentsstages: - deploy
deploy:dev: stage: deploy script: - echo "Deploying to dev" environment: name: development url: https://dev.example.com on_stop: stop:dev only: - develop
stop:dev: stage: deploy script: - echo "Stopping dev environment" environment: name: development action: stop when: manual only: - develop
deploy:staging: stage: deploy script: - echo "Deploying to staging" environment: name: staging url: https://staging.example.com on_stop: stop:staging only: - develop
deploy:production: stage: deploy script: - echo "Deploying to production" environment: name: production url: https://example.com deployment_tier: production when: manual only: - main retry: max: 2 when: - runner_system_failure - stuck_or_timeout_failureAuto DevOps
Section titled “Auto DevOps”┌─────────────────────────────────────────────────────────────────────────────┐│ Auto DevOps │├─────────────────────────────────────────────────────────────────────────────┤│ ││ ┌─────────────────────────────────────────────────────────────────────┐ ││ │ Auto DevOps Pipeline │ ││ │ │ ││ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ ││ │ │ Build │ │ Test │ │ Review │ │ Staging │ │ ││ │ │ (Auto) │ │ (Auto) │ │ (Auto) │ │ (Auto) │ │ ││ │ └─────────┘ └─────────┘ └─────────┘ └─────────┘ │ ││ │ │ ││ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ ││ │ │ Prod │ │ Cycle │ │ Browser │ │ ││ │ │ (Auto) │ │ (Auto) │ │ Security│ │ ││ │ └─────────┘ └─────────┘ └─────────┘ │ ││ │ │ ││ └──────────────────────────────────────────────────────────────────────┘ ││ ││ Features: ││ ✓ Auto build (Auto CI) ││ ✓ Auto test (Auto Test) ││ ✓ Auto review apps ││ ✓ Auto deploy to staging ││ ✓ Auto deploy to production (with approval) ││ ✓ Auto monitoring ││ ✓ Browser security testing ││ │└─────────────────────────────────────────────────────────────────────────────┘Auto DevOps Configuration
Section titled “Auto DevOps Configuration”include: - template: Auto-DevOps.gitlab-ci.yml
variables: AUTO_DEVOPS_BUILD_IMAGE_TAG_PREFIX: "" AUTO_DEVOPS_DEPLOYMENTStrategy: ROLLING AUTO_DEVOPS_BROWSER_SKIP: "false"
# Override stagesstages: - build - test - deployGitLab CI vs GitHub Actions
Section titled “GitLab CI vs GitHub Actions”┌─────────────────────────────────────────────────────────────────────────────┐│ GitLab CI vs GitHub Actions │├─────────────────────────────────────────────────────────────────────────────┤│ ││ Feature GitLab CI GitHub Actions ││ ────────────────────────────────────────────────────────────────────── ││ Config File .gitlab-ci.yml .github/workflows/*.yml ││ Repository GitLab only Any GitHub repo ││ Storage GitLab instance GitHub ││ Runners Self-hosted or SaaS Self-hosted or GitHub-hosted ││ Containers Built-in registry GitHub Container Registry ││ Environments Built-in GitHub Environments ││ Free Tier 2000 min/mo 2000 min/mo ││ Enterprise GitLab Premium GitHub Enterprise ││ ││ GitLab CI Advantages: ││ ✓ Everything in one place (repo, CI, CD, registry) ││ ✓ Excellent environment management ││ ✓ Built-in Docker registry ││ ✓ Powerful Auto DevOps ││ ││ GitHub Actions Advantages: ││ ✓ Works with any GitHub repo ││ ✓ Large action marketplace ││ ✓ Native GitHub integration ││ ✓ Great for open source ││ │└─────────────────────────────────────────────────────────────────────────────┘Summary
Section titled “Summary”In this chapter, you learned:
- GitLab CI Overview: Architecture and key features
- Basic Configuration: .gitlab-ci.yml structure
- Jobs and Stages: Using extends and templates
- Docker Integration: Building and pushing containers
- Matrix Builds: Parallel testing and building
- Runner Configuration: Using specific runners
- Environments: Deployment management
- Auto DevOps: Automated CI/CD pipeline
- Comparison: GitLab CI vs GitHub Actions