GitLab is a web-based DevOps platform that provides a robust set of tools for source code management, CI/CD, project management, and deployment automation. This cheatsheet covers everything from basic usage to advanced GitLab features.
GitLab is an open-source DevOps platform offering integrated tools for:
- Source control (Git)
- Continuous Integration/Continuous Deployment (CI/CD)
- Issue tracking and project management
- Container registry and DevSecOps
- Git Repository Management: Handles distributed version control and code review.
- CI/CD Pipelines: Automates testing, integration, and deployment.
- DevSecOps: Built-in security scanning for dependencies, container images, and code.
- Container Registry: Docker container management.
- Sign up: Visit GitLab and create an account.
- Create a Project:
- Go to Projects → New Project.
- Choose Blank Project, Import, or Template.
- Configure visibility (Private, Internal, or Public).
-
Generate an SSH key:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -
Copy the public key:
cat ~/.ssh/id_rsa.pub -
Add the key in GitLab:
- Go to User Settings → SSH Keys → Paste the public key.
git clone git@gitlab.com:username/projectname.git# Stage files
git add .
# Commit files
git commit -m "Initial commit"
# Push changes
git push origin main-
Create a branch:
git checkout -b feature-branch
-
Push the branch:
git push origin feature-branch
- Go to your project on GitLab.
- Navigate to Merge Requests → New Merge Request.
- Select source and target branches and create an MR.
The .gitlab-ci.yml file defines the CI/CD pipeline.
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Building the project"
- ./build-script.sh
test_job:
stage: test
script:
- echo "Running tests"
- ./test-script.sh
deploy_job:
stage: deploy
script:
- echo "Deploying to production"
- ./deploy-script.sh- Stages: Define steps (e.g.,
build,test,deploy). - Jobs: Define tasks in each stage.
- Runners: Execute pipeline jobs (shared or custom).
-
Push changes to a branch:
git push origin branch-name
-
Check pipelines:
- Navigate to CI/CD → Pipelines in GitLab.
- Runners execute CI/CD jobs.
- Shared Runners: Provided by GitLab.
- Custom Runners: Self-hosted.
-
Install GitLab Runner:
sudo apt install gitlab-runner
-
Register the Runner:
gitlab-runner register
- Enter GitLab URL, registration token, executor (e.g.,
shell,docker), and tags.
- Enter GitLab URL, registration token, executor (e.g.,
-
Set Environment Variables:
- Go to Settings → CI/CD → Variables.
- Add variables (e.g.,
AWS_ACCESS_KEY,DOCKER_PASSWORD).
-
Use in
.gitlab-ci.yml:script: - echo $MY_VARIABLE
Artifacts store job outputs.
test_job:
stage: test
script:
- ./run-tests
artifacts:
paths:
- test-results/Host static websites directly on GitLab.
pages:
stage: deploy
script:
- mkdir .public
- cp -r * .public
artifacts:
paths:
- public-
GitLab provides a built-in Docker registry for container storage.
-
Push an Image:
docker build -t registry.gitlab.com/username/projectname:tag . docker login registry.gitlab.com docker push registry.gitlab.com/username/projectname:tag
- Integrate Kubernetes clusters with GitLab for deployments.
- Navigate to Operations → Kubernetes to connect your cluster.
deploy:
stage: deploy
script:
- helm install my-app ./helm-chart-
Enable SAST to scan for vulnerabilities:
include: - template: Security/SAST.gitlab-ci.yml
-
Perform runtime vulnerability scans:
include: - template: Security/DAST.gitlab-ci.yml
-
Detect hardcoded secrets:
include: - template: Security/Secret-Detection.gitlab-ci.yml
- Navigate to Analytics → CI/CD → Pipelines to review pipeline efficiency.
-
Enable coverage reports in
.gitlab-ci.yml:test_job: stage: test script: - ./run-tests coverage: '/Code Coverage: \d+%/'
-
Scan Docker images for vulnerabilities:
include: - template: Security/Container-Scanning.gitlab-ci.yml
-
For self-hosted GitLab, run:
gitlab-backup create
-
Backup includes repositories, CI/CD logs, uploads, and settings.
-
Restore a backup:
gitlab-restore restore BACKUP_FILE=backup_filename
- Pipeline Failures:
- Check pipeline logs in CI/CD → Jobs.
- Runner Issues:
- Ensure the runner is active:
gitlab-runner status.
- Ensure the runner is active:
- Permission Errors:
- Verify SSH key and repository access.
-
Add verbose logging:
script: - echo "Debugging info" - set -x - ./my-script.sh
- Use Branching Strategies:
- Implement GitLab Flow or GitFlow for streamlined collaboration.
- Secure CI/CD Pipelines:
- Use environment variables to manage sensitive data.
- Automate Reviews:
- Use merge request templates and code owners.
- Leverage GitLab Templates:
- Use pre-built
.gitlab-ci.ymltemplates to save time.
- Use pre-built
- Monitor Usage:
- Regularly check project and pipeline analytics.
-
Login to GitLab CLI:
glab auth login
-
List Repositories:
glab repo list
-
Create an Issue:
glab issue create --title "Bug report" --description "Details here"
