GitLab CI — Fundamentals
What is GitLab CI?
| GitLab CI | Jenkins | GitHub Actions |
|---|
|---|---|---|---|
| Where config lives | `.gitlab-ci.yml` in repo | Jenkinsfile in repo or UI | `.github/workflows/*.yml` |
|---|---|---|---|
| Infrastructure | GitLab-hosted or self-hosted runners | Self-managed servers + agents | GitHub-hosted runners |
| Registry | Built-in container registry | External | GHCR (GitHub Packages) |
| Best for | GitLab users, integrated DevOps platform | Complex enterprise pipelines | GitHub repos, open source |
Pipeline Syntax
Every pipeline has stages (ordered groups), jobs (the unit of work inside a stage), artifacts (files passed between jobs), and cache (persisted between pipeline runs). Jobs in the same stage run in parallel.
Advanced — Rules, Needs, Cache, Include
Runners
| Type | Pros | Cons |
|---|
|---|---|---|
| Shared (GitLab.com) | Zero setup, 400 min/month free | Limited minutes, public runner (a security concern for sensitive code) |
|---|---|---|
| Self-hosted (shell) | No minute limits, fast (no startup) | Build pollution between jobs |
| Self-hosted (Docker) | Clean environment per job, reproducible | Docker pull on every job (mitigate with a registry cache) |
| Self-hosted (Kubernetes) | Scales to zero, unlimited parallel jobs | Pod startup time (10-30 sec) |
Variables & Secrets
Marking a variable Protected restricts it to protected branches only (main, release branches); marking it Masked hides its value in job logs. Both should be enabled by default for any real secret.
Interview Questions
What is GitLab CI and how does it differ from Jenkins?
GitLab CI is CI/CD built directly into the GitLab platform — the pipeline is defined in a .gitlab-ci.yml file in the repository root, and when code is pushed, GitLab automatically reads this file and runs the pipeline with no separate server and no plugins to manage. Key differences from Jenkins: GitLab CI configuration lives in the repo itself, pipeline-as-code from day one, while Jenkins requires a separate server, plugin management, and manual Jenkinsfile setup. GitLab CI has a built-in container registry, artifact storage, and environment tracking. GitLab CI runners are simpler to scale on Kubernetes than Jenkins agents, while Jenkins has a more mature shared-library ecosystem and more plugin options for complex enterprise workflows. Choose GitLab CI when the team already uses GitLab for code, issues, and merge requests, since the integrated experience (pipeline status directly on the MR) is genuinely valuable; choose Jenkins for very complex pipeline logic requiring Groovy, existing enterprise Jenkins infrastructure, or working across multiple source control systems.
What is the difference between artifacts and cache in GitLab CI?
Artifacts are files produced by a job that need to be passed to later jobs in the same pipeline — they're uploaded to the GitLab server and downloaded by dependent jobs. For example, a build job compiles a JAR file and the deploy job needs it: the build job declares it as an artifact, and the deploy job automatically receives it. Cache, by contrast, is meant to persist across pipeline runs to speed up repeated work — node_modules or a Maven .m2 directory cached between runs so dependencies aren't re-downloaded every single pipeline execution. The practical distinction: artifacts are for passing this run's outputs forward within the same pipeline; cache is for avoiding redundant work across separate runs. Using cache where an artifact is needed (or vice versa) is a common beginner mistake — cache isn't guaranteed to be available (it can be evicted), while artifacts reliably exist for the pipeline that created them.

