GitLab CI/CD¶
Scope: designing CI/CD pipelines on GitLab CI/CD, including CI/CD components, merge request pipelines, runners and deployments. Pipelines run with access to your code, secrets and cloud accounts, so many rules here are about keeping them secure. GitHub Actions covers the same concerns on GitHub. For general secrets and supply-chain rules, see Security; for building container images, see Docker.
Entries are sorted by score from 1 to 10 (criteria): 9โ10 start here or strongly recommended, 6โ8 recommended, 1โ5 further reading or conditional.
Adopted references¶
- 8 Pipeline security (GitLab Docs): the baseline for pipeline security: where to store secrets, inputs instead of pipeline variables, and pinning images, dependencies, scripts and included configuration.
- 8 Security for self-managed runners (GitLab Docs): the risks of each executor, privileged containers, image pull policies, shared Git working copies and network segmentation.
- 8 Deployment safety (GitLab Docs): protected environments, one deployment at a time, outdated deployment jobs, deploy freezes, production secrets and a separate project for deployments.
- 7 CI/CD components (GitLab Docs): reusable, versioned pipeline configuration, the CI/CD Catalog, and security best practices for component users and maintainers.
- 7 OpenID Connect authentication using ID tokens (GitLab Docs): short-lived credentials for cloud providers and secret managers instead of stored secrets, with guides per cloud provider.
- 7 CI/CD job token (GitLab Docs): what
CI_JOB_TOKENcan access, and how the job token allowlist limits which projects can use it. - 6 Pipeline efficiency (GitLab Docs): finding bottlenecks, failing fast,
needs, components, caching and Docker images. - 6 Pipeline architecture (GitLab Docs): when to use basic pipelines,
needs, parent-child pipelines and multi-project pipelines.
Key rules¶
Security¶
- 9
GITLAB-1Pin everyincludeand CI/CD component to a commit SHA or to a protected tag, pin job images by digest, and let a bot such as Renovate propose updates. Avoid~latest, branch names and versionless includes, and copy remote files into the repository or pin them withinclude:integrity. (GitLab, Pipeline security: included configurations, Docker images, CI/CD component security best practices, include:integrity, Renovate, GitLab CI/CD manager) - 9
GITLAB-2Keep secrets in an external secrets manager. If a secret must be a CI/CD variable, make it masked and hidden, and protected so that only pipelines for protected branches and tags receive it. Masking only keeps values out of job logs: code that runs in the job can still read and send them. (GitLab, Pipeline security: secrets storage, CI/CD variables: protect, mask, hide) - 8
GITLAB-3Authenticate to cloud providers and secret managers with ID tokens (id_tokens) instead of stored credentials, and restrict the trust policy to specific projects and protected branches or tags. Where the provider supports it, add stable claims such asproject_idto thesubclaim, so that the policy isn't affected when a project or group is renamed or moved. (GitLab, Use ID token claims in cloud trust policies, Configure a conditional role with OIDC claims) - 8
GITLAB-4Run a pipeline in the parent project for a merge request from a fork only after reviewing its changes, including changes to.gitlab-ci.yml. That pipeline runs the fork's CI configuration with the parent project's variables and runners, and with the permissions of the person who starts it. (GitLab, Run pipelines in the parent project, CI/CD variable security) - 8
GITLAB-5Don't use the shell executor or privileged Docker containers on runners that run jobs from several projects or untrusted users. Run privileged jobs only on isolated, ephemeral machines, and let runners that hold secrets or run privileged jobs take jobs from protected branches only. (GitLab, Security risks for different executors, Reduce the security risk of privileged containers, Prevent runners from revealing sensitive information) - 7
GITLAB-6Keep the CI/CD job token allowlist enabled, and add only the groups and projects that need cross-project access. With the allowlist turned off, jobs in any project can use their job token to access your project. (GitLab, Control job token access to your project, Allow any project to access your project) - 7
GITLAB-7Pass parameters to pipelines as typed CI/CD inputs, and set the minimum role to use pipeline variables tono_one_allowedor Maintainer. Where an input reaches a script, restrict it with a number or boolean type,regexoroptions:posix_escapedoesn't make untrusted values safe. (GitLab, Pipeline security: pass parameters, Restrict pipeline variables, CI/CD inputs: posix_escape) - 6
GITLAB-8Never commitCI_DEBUG_TRACEto CI configuration, and turn on debug logging only temporarily, while only team members can view job logs. Debug logs contain every variable and secret available to the job. (GitLab, Enable debug logging) - 6
GITLAB-9Keep separate caches for protected and unprotected branches, which is the default, and don't setcache:unprotect. Pass build outputs between jobs as artifacts, and use the cache only for dependencies such as downloaded packages. (GitLab, Use the same cache for all branches, cache:unprotect, How cache is different from artifacts) - 6
GITLAB-10Require Code Owner approval for changes to.gitlab-ci.ymland other CI configuration on protected branches. Code Owner approval needs GitLab Premium or Ultimate. (GitLab, Require Code Owner approval, Code Owners, CI/CD variable security)
Pipeline design¶
- 7
GITLAB-11Userulesinstead of the deprecatedonlyandexcept, and don't mix them in one pipeline. Addworkflow:rulesthat choose between branch and merge request pipelines, so that a push to a branch with an open merge request doesn't start two pipelines. (GitLab, Deprecated keywords: only and except, Avoid duplicate pipelines, Switch between branch and merge request pipelines) - 6
GITLAB-12Run merge request pipelines, and enable merged results pipelines so that each merge request is tested combined with the latest target branch. Add merge trains when many merge requests are ready to merge at the same time. Merged results pipelines and merge trains need GitLab Premium or Ultimate. (GitLab, Merge request pipelines, Merged results pipelines, Merge trains) - 6
GITLAB-13Declare job dependencies withneeds, so that each job starts as soon as the jobs it depends on finish, and give jobs that depend on nothing, such as linters,needs: []. (GitLab, Make jobs start earlier with needs) - 6
GITLAB-14Share CI configuration as versioned CI/CD components with inputs instead of copying YAML between projects. In components, replace hardcoded values with inputs and avoid global keywords, which would change every job in the including pipeline. (GitLab, CI/CD components, Replace hardcoded values with inputs, Avoid using global keywords, Pipeline efficiency: reuse configuration)
Deployments¶
- 7
GITLAB-15Deploy to production only through a protected environment: limit who can deploy, require deployment approvals, and scope production variables to that environment. Protected environments and deployment approvals need GitLab Premium or Ultimate. (GitLab, Deployment safety: protect production secrets, Protected environments, Deployment approvals, CI/CD variables: environment scope) - 7
GITLAB-16Give each deployment job aresource_group, so that only one deployment to an environment runs at a time, and turn on "Prevent outdated deployment jobs". Otherwise an older pipeline can finish last and overwrite a newer deployment. (GitLab, Ensure only one deployment job runs at a time, Prevent outdated deployment jobs, Resource group)
How to enforce¶
Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.
- 7 CI Lint: GitLab's built-in validation of
.gitlab-ci.yml, with its includes, which can also simulate pipeline creation. Run it in the pipeline editor, withglab ci lintor through the CI Lint API. Its pipeline warnings flag rules that can start duplicate pipelines. Partly checksGITLAB-11. - 7 GitLab CI/CD settings: setting "Minimum role to use pipeline variables" to
no_one_allowedblocks pipeline variables. Other settings keep the job token allowlist on (administrators of self-managed instances can enforce it for every project), keep separate caches for protected branches, and turn on Prevent outdated deployment jobs. Partly checksGITLAB-6,GITLAB-7,GITLAB-9andGITLAB-16. - 5 Plumber: a CI/CD security scanner for GitLab CI and GitHub Actions, run as a CLI or a GitLab CI/CD component. Its GitLab controls flag includes on forbidden versions such as
latest,mainorHEAD, images not pinned by digest, unprotected and unmasked variables, and pipelines that enableCI_DEBUG_TRACE. Partly checksGITLAB-1,GITLAB-2andGITLAB-8. - 5 poutine: a build pipeline scanner for GitHub Actions and GitLab CI. For GitLab, it flags
CI_DEBUG_TRACE, and its injection rule flags inputs interpolated withexpand_varsinto scripts. Partly checksGITLAB-7andGITLAB-8. - 5 Checkov GitLab CI checks:
CKV_GITLABCI_2flags job rules that create duplicate pipelines. Partly checksGITLAB-11.
Tutorials and further reading¶
- 6 Tutorial: Create and run your first GitLab CI/CD pipeline (GitLab Docs): runners,
.gitlab-ci.yml, jobs and stages from scratch. - 6 Tutorial: Create a complex pipeline (GitLab Docs): builds and deploys a site step by step, adding artifacts, test jobs, merge request pipelines and less duplicated configuration.