The GitLab CI Commands Cheat Sheet is your ultimate reference for managing continuous integration (CI) and continuous deployment (CD) pipelines using GitLab. Whether you’re configuring jobs, runners, or automating deployments, understanding these commands will streamline your DevOps workflow and improve productivity.
This comprehensive guide includes categorized GitLab CI/CD commands, YAML configuration examples, and troubleshooting tips — everything you need to master GitLab CI pipelines from start to finish.
What Is GitLab CI/CD?
GitLab CI/CD is an integrated DevOps automation tool that enables developers to build, test, and deploy code using pipelines. It eliminates manual intervention, accelerates software delivery, and ensures consistent results across environments.
The GitLab CI Commands Cheat Sheet helps you execute commands efficiently for:
- Managing pipelines and jobs.
- Configuring runners and stages.
- Inspecting logs and artifacts.
- Automating build and deployment tasks.
GitLab CI relies on a .gitlab-ci.yml file that defines each step of your CI/CD process.
How GitLab CI Commands Work
GitLab CI commands are used within pipelines or executed locally with the GitLab Runner. The runner reads the .gitlab-ci.yml file and runs jobs according to defined stages and conditions.
For example:
stages:
- build
- test
- deploy
build-job:
stage: build
script:
- echo "Building the application..."
Each command inside the script: section executes sequentially in a runner environment, similar to how shell commands are executed in a terminal.
Installing GitLab Runner
Before running GitLab CI commands locally, you must install GitLab Runner, the component responsible for executing CI/CD jobs.
1. Install GitLab Runner on Linux
sudo apt-get install gitlab-runner
2. Register a Runner
sudo gitlab-runner register
Follow the interactive prompts to enter:
- GitLab instance URL
- Registration token
- Runner description
- Executor type (e.g., shell, docker, virtualbox)
3. Start the Runner
sudo gitlab-runner start
Once the runner is active, it listens for jobs triggered by GitLab pipelines.
GitLab CI Pipeline Commands
Pipelines are the heart of GitLab CI/CD. These commands help you manage and inspect them easily.
1. Trigger a Pipeline
curl --request POST \
--form "token=<trigger_token>" \
--form "ref=main" \
https://gitlab.com/api/v4/projects/<project_id>/trigger/pipeline
2. List All Pipelines
gitlab-runner list
3. Retry a Pipeline
curl --request POST \
--header "PRIVATE-TOKEN: <your_access_token>" \
"https://gitlab.com/api/v4/projects/<project_id>/pipelines/<id>/retry"
4. Cancel a Pipeline
curl --request POST \
--header "PRIVATE-TOKEN: <your_access_token>" \
"https://gitlab.com/api/v4/projects/<project_id>/pipelines/<id>/cancel"
Managing pipelines from the command line makes CI/CD automation faster and more reliable.
GitLab CI Job Commands
Jobs are individual tasks that run within stages. You can manage and debug them using the following commands.
1. Run a Job Manually
gitlab-runner exec shell <job_name>
2. View Job Logs
gitlab-runner logs <job_id>
3. Retry a Failed Job
gitlab-runner retry <job_id>
4. Cancel a Running Job
gitlab-runner stop <job_id>
5. Delete Job Artifacts
curl --request DELETE \
--header "PRIVATE-TOKEN: <token>" \
"https://gitlab.com/api/v4/projects/<project_id>/jobs/<job_id>/artifacts"
These commands help you maintain pipeline efficiency and keep storage clean by removing old artifacts.
Runner Management Commands
GitLab runners execute your CI/CD jobs. Managing them effectively ensures smooth execution of builds and tests.
1. List Registered Runners
sudo gitlab-runner list
2. Unregister a Runner
sudo gitlab-runner unregister --name "my-runner"
3. Verify a Runner
sudo gitlab-runner verify
4. Pause or Resume Runners
sudo gitlab-runner pause
sudo gitlab-runner resume
5. Update GitLab Runner
sudo gitlab-runner update
Regular runner updates prevent version mismatches with GitLab instances.
Common GitLab CI Script Commands
In this GitLab CI Commands Cheat Sheet, you’ll find commonly used script-level commands inside .gitlab-ci.yml files.
1. Build Stage Example
build:
stage: build
script:
- npm install
- npm run build
2. Test Stage Example
test:
stage: test
script:
- pytest tests/
3. Deploy Stage Example
deploy:
stage: deploy
script:
- echo "Deploying application..."
4. Conditional Deployment
only:
- main
Each stage can use environment variables and conditionals for flexible deployment workflows.
Environment and Variable Commands
GitLab CI lets you define custom variables for pipelines, jobs, and stages.
1. List Environment Variables
env
2. Use a Custom Variable
variables:
APP_ENV: production
3. Access Variable in Script
script:
- echo "Environment: $APP_ENV"
4. Mask or Protect Sensitive Variables
Go to:
Settings → CI/CD → Variables → Add Variable
Mark as Protected or Masked to secure secrets like tokens and passwords.
GitLab CI Artifacts and Cache Commands
Artifacts store job outputs (like build files), while caches improve job performance.
1. Define Artifacts
artifacts:
paths:
- build/
expire_in: 1 week
2. Use Cache
cache:
paths:
- node_modules/
3. Download Artifacts
curl --header "PRIVATE-TOKEN: <token>" \
"https://gitlab.com/api/v4/projects/<id>/jobs/<job_id>/artifacts/download"
Proper use of caching reduces build time and speeds up pipelines.
GitLab CI YAML Configuration Commands
The .gitlab-ci.yml file is the core of GitLab CI. Understanding its syntax is essential.
1. Validate YAML File
curl --request POST \
--form "token=<token>" \
--form "ref=main" \
--form "variables[RUN_VALIDATION]=true" \
"https://gitlab.com/api/v4/projects/<project_id>/ci/lint"
2. Include External YAML Files
include:
- local: path/to/file.yml
- remote: https://example.com/common.yml
3. Define Global Variables
variables:
GLOBAL_TIMEOUT: 30m
These commands make pipelines reusable and modular.
GitLab CI Trigger and Schedule Commands
Triggers and schedules help automate pipeline execution.
1. Create a Pipeline Trigger
curl --request POST \
--form "token=<trigger_token>" \
--form "ref=main" \
https://gitlab.com/api/v4/projects/<project_id>/trigger/pipeline
2. List Scheduled Pipelines
curl --header "PRIVATE-TOKEN: <token>" \
"https://gitlab.com/api/v4/projects/<project_id>/pipeline_schedules"
3. Delete a Scheduled Pipeline
curl --request DELETE \
--header "PRIVATE-TOKEN: <token>" \
"https://gitlab.com/api/v4/projects/<project_id>/pipeline_schedules/<id>"
Schedules are ideal for recurring tasks like nightly builds or weekly deployments.
GitLab CI Debugging and Troubleshooting
When pipelines fail, these GitLab CI commands can help you diagnose issues quickly.
1. Check Runner Logs
sudo gitlab-runner verify
2. Debug a Pipeline Job
script:
- set -x
3. Test CI Locally
gitlab-runner exec shell test
4. View Job Exit Codes
echo $?
Debugging early prevents broken builds from reaching production.
GitLab CI for Docker Users
If you’re using Docker-based builds, here are some essential commands and YAML snippets.
1. Use Docker Executor
sudo gitlab-runner register --executor docker
2. Example Docker Build Job
docker-build:
stage: build
image: docker:latest
script:
- docker build -t myapp .
3. Push Image to Docker Hub
script:
- docker login -u $DOCKER_USER -p $DOCKER_PASS
- docker push myapp:latest
GitLab CI integrates seamlessly with Docker, making container-based pipelines simple to manage.
Best Practices for Using GitLab CI Commands
- Keep pipelines modular and lightweight.
- Use YAML anchors to avoid repetitive definitions.
- Always validate
.gitlab-ci.ymlbefore committing. - Secure sensitive tokens with GitLab’s masked variable feature.
- Regularly update GitLab Runner to the latest version.
Following these best practices ensures smooth automation and consistent deployment workflows.
Conclusion
This GitLab CI Commands Cheat Sheet is a complete command reference for developers and DevOps engineers looking to streamline CI/CD processes. From pipeline creation to debugging and runner management, mastering these commands will improve automation efficiency and reduce deployment errors.
Keep this cheat sheet handy as a quick reference guide to navigate daily GitLab operations and optimize your DevOps workflow.
FAQs About GitLab CI Commands Cheat Sheet
1. What is GitLab CI used for?
GitLab CI is used for automating the software build, test, and deployment process through pipelines, reducing manual effort in DevOps workflows.
2. How do I trigger a GitLab pipeline manually?
You can trigger pipelines using:curl --request POST \
--form "token=<trigger_token>" \
--form "ref=main" \
https://gitlab.com/api/v4/projects/<project_id>/trigger/pipeline
3. Can I test GitLab CI jobs locally?
Yes, you can use:gitlab-runner exec shell <job_name>
to simulate job execution on your local machine.
4. What is a GitLab Runner?
A GitLab Runner is an agent that executes CI/CD jobs defined in your .gitlab-ci.yml file on different environments like shell, Docker, or virtual machines.
5. How can I debug GitLab CI pipelines?
Enable debugging by adding:script:
- set -x
This displays each command as it runs, helping identify errors quickly.