logo
eng-flag

GitLab CI/CD Cheatsheet

Table of Contents

  1. GitLab CI/CD Basics
  2. GitLab CI/CD Configuration
  3. GitLab Runners
  4. GitLab CI/CD Pipeline
  5. GitLab CI/CD Variables
  6. GitLab CI/CD Artifacts
  7. GitLab CI/CD Caching
  8. GitLab CI/CD with Docker
  9. GitLab CI/CD with Kubernetes
  10. GitLab CI/CD Templates
  11. GitLab CI/CD Auto DevOps
  12. GitLab CI/CD Security Scanning
  13. GitLab CI/CD Best Practices
  14. Troubleshooting GitLab CI/CD

GitLab CI/CD Basics

What is GitLab CI/CD?

GitLab CI/CD is a tool built into GitLab for software development through continuous methodologies:

  • Continuous Integration (CI)
  • Continuous Delivery (CD)
  • Continuous Deployment (CD)

Key Components

  • .gitlab-ci.yml file: Defines the CI/CD pipeline
  • GitLab Runner: Executes the jobs in the pipeline
  • Pipelines: Defines the stages and jobs to be executed
  • Jobs: Individual tasks in the pipeline

GitLab CI/CD Configuration

Basic .gitlab-ci.yml structure

stages:
  - build
  - test
  - deploy

build_job:
  stage: build
  script:
    - echo "Building the application"

test_job:
  stage: test
  script:
    - echo "Running tests"

deploy_job:
  stage: deploy
  script:
    - echo "Deploying the application"

Job structure

job_name:
  stage: stage_name
  image: docker_image
  script:
    - command1
    - command2
  only:
    - branch_name
  tags:
    - runner_tag

GitLab Runners

Install GitLab Runner

# For Debian/Ubuntu
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
sudo apt-get install gitlab-runner

# For CentOS/RHEL
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash
sudo yum install gitlab-runner

Register a Runner

sudo gitlab-runner register

List Runners

sudo gitlab-runner list

Start/Stop Runner

sudo gitlab-runner start
sudo gitlab-runner stop

GitLab CI/CD Pipeline

Define stages

stages:
  - build
  - test
  - deploy

Define jobs

build_job:
  stage: build
  script:
    - echo "Building the application"

test_job:
  stage: test
  script:
    - echo "Running tests"

deploy_job:
  stage: deploy
  script:
    - echo "Deploying the application"

Use conditions

deploy_job:
  stage: deploy
  script:
    - echo "Deploying the application"
  only:
    - master
  when: manual

GitLab CI/CD Variables

Predefined variables

  • CI_COMMIT_SHA: The commit revision for which project is built
  • CI_COMMIT_SHORT_SHA: The first eight characters of CI_COMMIT_SHA
  • CI_COMMIT_REF_NAME: The branch or tag name for which project is built
  • CI_JOB_ID: The unique ID of the current job

Custom variables

variables:
  MY_VARIABLE: "my-value"

job_name:
  script:
    - echo $MY_VARIABLE

Masked variables

Set in GitLab UI: Settings > CI/CD > Variables

GitLab CI/CD Artifacts

Define artifacts

job_name:
  script:
    - make build
  artifacts:
    paths:
      - build/
    expire_in: 1 week

Download artifacts

Go to your project's CI/CD > Jobs > Select a job > Download artifacts

GitLab CI/CD Caching

Define cache

cache:
  paths:
    - node_modules/

job_name:
  script:
    - npm install
    - npm run build

Cache per-job

job_name:
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
  script:
    - npm install
    - npm run build

GitLab CI/CD with Docker

Use Docker image

job_name:
  image: python:3.9
  script:
    - python --version

Build Docker image

build_image:
  image: docker:latest
  services:
    - docker:dind
  script:
    - docker build -t my-image:$CI_COMMIT_SHA .
    - docker push my-image:$CI_COMMIT_SHA

GitLab CI/CD with Kubernetes

Deploy to Kubernetes

deploy_to_k8s:
  image: bitnami/kubectl:latest
  script:
    - kubectl config set-cluster mycluster --server="$KUBE_URL" --insecure-skip-tls-verify=true
    - kubectl config set-credentials admin --token="$KUBE_TOKEN"
    - kubectl config set-context default --cluster=mycluster --user=admin
    - kubectl config use-context default
    - kubectl apply -f k8s/deployment.yaml

GitLab CI/CD Templates

Use a template

include:
  - template: Auto-DevOps.gitlab-ci.yml

Create a custom template

  1. Create a file (e.g., my-template.yml) in your project
  2. Include it in your .gitlab-ci.yml:
include:
  - local: my-template.yml

GitLab CI/CD Auto DevOps

Enable Auto DevOps

  1. Go to your project's Settings > CI/CD > Auto DevOps
  2. Check "Default to Auto DevOps pipeline"
  3. Configure the deployment strategy and other options

Customize Auto DevOps

Create a .gitlab-ci.yml file to override or extend Auto DevOps:

include:
  - template: Auto-DevOps.gitlab-ci.yml

stages:
  - build
  - test
  - deploy

# Add your custom jobs here

GitLab CI/CD Security Scanning

Enable SAST (Static Application Security Testing)

include:
  - template: Security/SAST.gitlab-ci.yml

Enable Container Scanning

include:
  - template: Security/Container-Scanning.gitlab-ci.yml

Enable Dependency Scanning

include:
  - template: Security/Dependency-Scanning.gitlab-ci.yml

GitLab CI/CD Best Practices

  1. Keep your .gitlab-ci.yml file simple and readable
  2. Use stages to organize your pipeline
  3. Use variables for configuration
  4. Use caching to speed up your pipeline
  5. Use artifacts to pass data between jobs
  6. Use templates to reuse common configurations
  7. Use merge request pipelines
  8. Implement proper error handling and notifications
  9. Use review apps for feature branches
  10. Implement security scanning in your pipeline

Troubleshooting GitLab CI/CD

Check pipeline status

Go to your project's CI/CD > Pipelines

View job logs

Go to your project's CI/CD > Jobs > Select a job

Debug with SSH

  1. Enable debug tracing in the job:
job_name:
  variables:
    CI_DEBUG_TRACE: "true"
  1. Use gitlab-runner to debug locally:
gitlab-runner exec docker job_name

Common issues

  • Runner connection issues: Check runner status and network
  • Job timeout: Increase timeout in job configuration
  • Artifact upload failure: Check storage quota and network
  • Cache issues: Clear cache and rebuild

2024 © All rights reserved - buraxta.com