Catalog / GitLab CI/CD Cheatsheet
GitLab CI/CD Cheatsheet
A comprehensive cheat sheet for GitLab CI/CD, covering essential concepts, syntax, and best practices for automating your software development pipeline.
GitLab CI/CD Basics
Core Concepts
CI/CD: Continuous Integration and Continuous Delivery/Deployment. Automates the software development lifecycle. GitLab CI/CD: Integrated CI/CD tool within GitLab for building, testing, and deploying code. .gitlab-ci.yml: Configuration file defining the CI/CD pipeline. Located in the root of your repository. |
Pipeline: A set of stages and jobs defining the CI/CD process. Stage: A logical division within a pipeline. Stages run sequentially. Job: An individual task within a stage. Jobs run in parallel within a stage. |
Runner: Executes the jobs defined in the Artifacts: Files or directories generated by a job that can be used by subsequent jobs or downloaded. |
.gitlab-ci.yml Structure
|
Key Directives
|
Defines the stages of the pipeline (e.g., build, test, deploy). |
|
Specifies the Docker image to use for the job. |
|
Commands to execute within the job. |
|
Assigns the job to a specific stage. |
|
Controls when a job runs based on branch, tags, etc. |
|
Defines environment variables for the job. |
Advanced Configuration
Variables
Define variables in |
|
Precedence (highest to lowest): |
CI/CD variables -> Project variables -> Group variables -> Instance variables |
Masked variables: |
Sensitive variables can be masked in the GitLab UI to prevent them from being printed in job logs. |
Artifacts
|
|
Artifacts can be downloaded or passed to subsequent jobs. |
Caching
|
|
Caching can significantly speed up build times by reusing dependencies and build outputs. |
Conditional Execution & Triggers
Only/Except
|
Run job only for specified refs (branches, tags). |
|
Run job for all refs except specified ones. |
Example: |
|
Rules
More flexible conditional execution based on various conditions. |
|
|
Pipeline Triggers
Trigger pipelines from other pipelines or external sources. |
|
Use |
Best Practices & Tips
Security
Use masked variables for sensitive information (passwords, API keys). Avoid storing secrets directly in Regularly audit your CI/CD configuration. |
Use GitLab’s security scanning tools to identify vulnerabilities in your code and dependencies. |
Performance
Use caching to reduce build times. Optimize your Docker images for size and performance. Run jobs in parallel whenever possible. |
Use GitLab Runner autoscaling to dynamically scale your runner infrastructure based on demand. |
Maintainability
Keep your Use templates to reuse common CI/CD configurations across multiple projects. Regularly update your CI/CD configuration to take advantage of new features and improvements. |
Test your CI/CD pipeline thoroughly to ensure it is working as expected. |