agentleFS
leroyguillaumeleroyguillaume/skills/gitlab-ci-conventions/SKILL.md

gitlab-ci-conventions

Skill7 starsChanged 3 days agoMITView on GitHub

GitLab CI mechanics, any instance or tier: rules, includes, scans, releases, Pages. TRIGGER when: editing `.gitlab-ci.yml`, `.gitlab/ci/`, `.gitlab/changelog_config.yml` or a CI/CD component `templates/*.yml`; setting up CI on GitLab; user asks about GitLab CI rules, runners, schedules, the vulnerability report, the coverage report, Releases or Pages. SKIP when: the repo is on GitHub (`github-actions-conventions`). Load `ci-conventions` alongside.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

9 rules. About 0 are common, 5 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈5

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    rules: only: Never only:/except:. They can't be combined with rules: and they hide the real trigger logic.

    line 32
  2. Distinctive:

    Write the digest to an artifact file per arch.

    line 176
  3. Very specific:

    Before GitLab 17.9, publish is not a pages: property: the job must be named pages and publish public through artifacts: paths:.

    line 290
  4. Very specific:

    Never use only:/except:, and never filter a job with changes alone.

    line 325
  5. Distinctive:

    Never include a template or component by branch, bare tag or remote URL.

    line 327
  6. Distinctive:

    Never make publishing credentials unprotected, and never push from an MR pipeline.

    line 328
  7. Distinctive:

    Never download a Trivy report template at run time.

    line 330
  8. Very specific:

    Never create a GitLab Release for a tag with a -.

    line 331
  9. Distinctive:

    Never let a Pages job run outside the default branch.

    line 332

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

GitLab CI conventions

How the pipelines from ci-conventions are written on GitLab. The skill assumes nothing about the instance. Runner tags, registry host and tier all vary, so read them from the repo or ask. Never guess a runner tag.

Layout and style

  • One .gitlab-ci.yml. Once it passes a couple of hundred lines, split it into .gitlab/ci/<pipeline>.yml (quality, coverage, build, security, chart, release, docs, each only when ci-conventions calls for it), pulled in with include: local:.
  • Each canonical pipeline is a set of jobs, not a file. Name jobs in lowercase kebab-case after what they do (pre-commit, test, build-image, trivy-fs), and keep them static. A parallel: matrix already suffixes the name.
  • rules: only. Never only:/except:. They can't be combined with rules: and they hide the real trigger logic.
  • needs: to build a DAG. Stages are for ordering the UI, not for sequencing jobs.
  • Shared rules go through !reference [.rules-build, rules] rather than YAML anchors, because anchors don't cross include: boundaries. pre-commit's check-yaml rejects custom tags, so give it the --unsafe arg in a repo that uses !reference.
  • Block style, no leading ---, per yaml-conventions — with one exception: !reference takes its path in flow style, the only form GitLab documents, so mark the line # yamllint disable-line rule:brackets or the strict yamllint hook rejects it.

The pipeline header

workflow:
  auto_cancel:
    on_new_commit: interruptible
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      when: never
    - if: $CI_COMMIT_BRANCH
    - if: $CI_COMMIT_TAG

default:
  interruptible: true
  • Without the second rule, a push to a branch with an open MR runs two pipelines, a branch one and an MR one.
  • interruptible: true on every job, publishing included. That is the GitLab form of the "cancel superseded runs" rule. Never override it to false. Auto-cancel also needs the project setting Auto-cancel redundant pipelines, which is on by default. Leave it on.

rules:changes traps

  • changes is always true on tag pipelines, scheduled and manual pipelines, and the first push of a new branch. A job filtered only by changes therefore runs on every schedule. Pair changes with an if: on the pipeline source:
    rules:
      - if: $CI_PIPELINE_SOURCE == "schedule"
        when: never
      - if: $CI_PIPELINE_SOURCE == "merge_request_event" || $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
        changes:
          paths:
            - src/**/*
            - Cargo.toml
            - Cargo.lock
            - Dockerfile
            - .dockerignore
            - .gitlab/ci/build.yml
  • In an MR pipeline, changes compares against the target branch. On a branch pipeline without an MR, add compare_to: refs/heads/<default>. Otherwise it compares against the previous push only.
  • A job skipped by rules doesn't block Pipelines must succeed. A pipeline with no job does. Because quality always runs, that never happens.

Pinning include:

include:
  - component: $CI_SERVER_FQDN/<group>/<project>/<name>@<40-char-sha>  # 1.4.0
  - project: <group>/<project>
    ref: <40-char-sha>  # v1.4.0
    file: /templates/build.yml
  • A full SHA with the version as a comment, bumped by Renovate (dependency-update-conventions). Never a branch, never a bare tag.
  • No include: remote: from a URL you don't control. Mirror the file into a project and include it by SHA.
  • include: local: is the repo itself and needs no pin.

Publishing credentials

  • Protect the release tag patterns (v*, chart-*) so that only maintainers can create them.
  • Registry and API credentials for publishing are CI/CD variables that are protected and masked (hidden where the instance supports it). Protected variables only reach pipelines on protected branches and tags, so an MR pipeline never sees them. That is the whole point.
  • Restrict CI_JOB_TOKEN access to the projects that actually need it (the job token allowlist).

coverage

One job, on the same rules as the test job:

coverage:
  image: ghcr.io/jdx/mise:<mise version>-debian
  allow_failure: true
  script:
    - mise install --locked
    - mise exec -- task coverage
  coverage: <the language skill's regex>
  artifacts:
    expose_as: coverage report
    paths:
      - <the language skill's HTML report directory>/
    reports:
      coverage_report:
        coverage_format: cobertura
        path: <the language skill's Cobertura file>
  • allow_failure: true is how it stays out of the merge gate: GitLab has no per-job required check, and Pipelines must succeed waits on every job not allowed to fail (ci-conventions says why it does not gate).
  • coverage: reads the total from the job log into the MR widget and the project's coverage history; the regex matches the line the tool prints.
  • coverage_report annotates the MR diff, line by line. It takes Cobertura, so task coverage writes that format too, at the path the language skill gives.

build

Define IMAGE once in top-level variables: ($CI_REGISTRY_IMAGE when using the GitLab registry, otherwise the organisation's registry). Tags and OCI labels derive from $CI_COMMIT_TAG, $CI_COMMIT_SHA and $CI_PROJECT_URL, and are computed in one place.

  • One job with a parallel: matrix over ARCH and a per-arch runner in tags:. Ask the user which runner tags exist.

    parallel:
      matrix:
        - ARCH:
            - amd64
            - arm64
  • In the same job: docker buildx build --load --platform linux/$ARCH, then the two Trivy scans (below) on the loaded image, then, only when pushing, the same build again with --output type=image,push-by-digest=true,name-canonical=true,push=true. It is a cache hit, so the pushed layers are the ones that were scanned. Write the digest to an artifact file per arch.
  • A manifest job (needs: both arch jobs, pushing runs only) assembles the tags with docker buildx imagetools create.
  • Layer cache: --cache-from type=registry,ref=$IMAGE:buildcache-$ARCH always, --cache-to only on pipelines that hold push credentials.
  • Push is enabled on v* tag pipelines and on manual (web) runs with a PUSH: "true" variable. MR pipelines never push.

Trivy on GitLab

  • The official Trivy image, pinned to an exact version. TRIVY_CACHE_DIR: $CI_PROJECT_DIR/.cache/trivy with a cache: on that path, because cache: only saves paths inside the project directory.
  • The reporting scan runs first, with --exit-code 0, then the gate (--exit-code 1 --severity HIGH,CRITICAL --ignore-unfixed). Reports are uploaded with artifacts: when: always.
  • Reports, using the templates shipped in the image (@/contrib/…), never downloaded:
    • every tier: --format template --template @/contrib/junit.tpl into artifacts: reports: junit:, which shows up in the MR widget;
    • Ultimate: also @/contrib/gitlab.tpl into artifacts: reports: container_scanning: for the image. The project vulnerability report is only fed from default-branch pipelines, so the scan has to run there.

Schedules

Pipeline schedules live in the project settings, not in the repo, so CONTRIBUTING.md documents each one (cron, ref, variables).

  • The security schedule sets SCHEDULE_TASK: security, and jobs select on $CI_PIPELINE_SOURCE == "schedule" && $SCHEDULE_TASK == "security". Other schedules (Renovate, …) pick their own value, and every other job has a when: never rule for schedule.
  • Find the latest stable release with git ls-remote --tags origin 'v*' rather than git tag, since the clone is shallow.
  • A schedule runs as its owner and is deactivated when that user is blocked or removed. Own it with a bot or service account.

release

On $CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+$/ only. The regex is what keeps -rcN tags from getting a release page, while build still pushes them.

release:
  stage: release
  needs:
    - manifest
  rules:
    - if: $CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+$/
  script:
    - scripts/release-notes.sh > release-notes.md
  release:
    tag_name: $CI_COMMIT_TAG
    description: release-notes.md
  • scripts/release-notes.sh gets the notes from the changelog API (GET /projects/:id/repository/changelog?version=<v>&from=<previous stable tag>), called with a protected project access token with read_api. CI_JOB_TOKEN only reaches a short allowlist of endpoints.
  • The API groups commits by their Changelog: git trailer (added, changed, fixed, security, deprecated, performance, removed, other), titled by .gitlab/changelog_config.yml. The trailer must be on the commit that lands on the default branch. With squash merges, that is the squash commit message, set in the MR.
  • The job image must carry the release tool the instance expects (glab, or release-cli, deprecated since GitLab 18.0).

chart

On $CI_COMMIT_TAG =~ /^chart-\d+\.\d+\.\d+(-rc\d+)?$/ (an rc chart tag publishes too) and manual runs: helm package --version <v> then helm push to oci://<registry>/charts, after the same version drift guard as release (release-script-conventions).

docs and GitLab Pages

What the pipeline does is in starlight-conventions. On GitLab:

  • Any pipeline that runs a Pages job deploys, whatever its ref; there is no environment protection to fall back on. The rules are the only guard.
  • Two jobs. docs-build runs on MRs touching the site (validation only) and on the default branch. pages deploys: a job with the pages: keyword, pages: publish: public, run on the default branch only, never on a schedule:
    pages:
      stage: deploy
      image: ghcr.io/jdx/mise:<mise version>-debian
      variables:
        GIT_DEPTH: 0
      rules:
        - if: $CI_PIPELINE_SOURCE == "schedule"
          when: never
        - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "pipeline"
        - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
          changes:
            paths:
              - docs/**/*
              - logo.svg
              - .gitlab/ci/docs.yml
      script:
        - git fetch --tags --force origin
        - mise install --locked
        - mise exec -- npm ci --prefix docs
        - mise exec -- npm --prefix docs run build:versions
        - mv docs/dist public
      pages:
        publish: public
    docs-build is the same script without pages:, with the MR rule instead. Before GitLab 17.9, publish is not a pages: property: the job must be named pages and publish public through artifacts: paths:.
  • GIT_DEPTH: 0 plus an explicit git fetch --tags: the runner's fetch refspecs do not bring the release tags, and a shallow clone sees no version.
  • After a stable release, deploy from the default branch, not the tag. The release job's pipeline triggers one on the default branch, which arrives with $CI_PIPELINE_SOURCE == "pipeline" (hence the pages rule above):
    docs:
      stage: release
      needs:
        - release
      rules:
        - if: $CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+$/
      trigger:
        project: $CI_PROJECT_PATH
        branch: $CI_DEFAULT_BRANCH
  • The URL depends on the instance and the project: a project with Pages unique domains on (the default for a new project) gets a unique domain served at /, others <group>.<pages-domain>/<project>, and a custom domain overrides both. Read it from the project's Pages settings before setting site/base.
  • Parallel deployments (pages: path_prefix:) are not how versions are served: every version is built into the one artifact.

Lint

  • The check-gitlab-ci hook from check-jsonschema, which validates against the vendored schema and understands !reference.
  • It doesn't resolve include:. glab ci lint does, but it needs the API, so it is a manual check and not a hook.

Never

  • Never use only:/except:, and never filter a job with changes alone.
  • Never set interruptible: false.
  • Never include a template or component by branch, bare tag or remote URL.
  • Never make publishing credentials unprotected, and never push from an MR pipeline.
  • Never download a Trivy report template at run time.
  • Never create a GitLab Release for a tag with a -.
  • Never let a Pages job run outside the default branch.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.