← All TILs · gitlab-ci

Lire en français traduction automatique Google Traduire sur votre ordinateur Firefox

A simple GitLab CI pipeline for ansible-lint on a custom Python image

gitlab-ci - 2026-09-05

Installing ansible-lint with pip inside every single pipeline run works, but it's slow and re-downloads the same packages on every commit. Building a small custom image once — based on the official python image, nothing fancier — and reusing it is barely more setup and considerably faster.

The GitLab terms used below, as GitLab's CI/CD docs use them: a pipeline is made up of stages and jobs and starts whenever something triggers it, such as a commit; a job is the task performed in a stage; a runner is the agent that runs your jobs, and its executor decides how (Docker, Kubernetes, shell…). An image here is a container image: each job runs inside one. The Container Registry is the store for container images that every GitLab project has, and docker:dind (Docker-in-Docker) is an image that runs its own Docker engine inside the job, which is what lets a job build images.

The image

# Dockerfile
FROM python:3.13-slim
RUN pip install --no-cache-dir ansible-core ansible-lint

That's the whole image: the official Python base plus the two packages actually needed.

The pipeline

This version assumes a runner with the Docker executor in privileged mode (see the caveat below for the other cases):

stages:
  - build
  - lint

build-image:
  stage: build
  image: docker:24.0.5
  services:
    - docker:24.0.5-dind
  timeout: 20m
  rules:
    - if: $CI_PIPELINE_SOURCE == "web"
    - changes:
        - Dockerfile
        - .gitlab-ci.yml
  before_script:
    - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
  script:
    - docker build -t "$CI_REGISTRY_IMAGE/ansible-lint:latest" .
    - docker push "$CI_REGISTRY_IMAGE/ansible-lint:latest"

lint:
  stage: lint
  image: "$CI_REGISTRY_IMAGE/ansible-lint:latest"
  script:
    - ansible-lint .

The things doing the actual work:

Bootstrapping the image

On a push, changes: compares against the previous commit. So if you add this pipeline to an existing repository and the push doesn't touch the Dockerfile or .gitlab-ci.yml, the build is skipped, and lint fails with ImagePullBackOff / not found because the image doesn't exist yet. (A brand-new branch has nothing to compare against, so there changes: matches and both jobs run.)

That's what the if: $CI_PIPELINE_SOURCE == "web" rule is for: start a pipeline by hand from Build > Pipelines > Run pipeline, and it always builds. Do that once to create the image, and again whenever you want to pick up newer ansible-lint releases.

Two related things worth knowing:

The honest caveat

docker:dind needs the runner configured for privileged mode, which is a real security tradeoff on a shared runner (a privileged container can potentially interact with the host). Fine on a personal or trusted-team runner.

If the runner doesn't allow it, the job doesn't fail cleanly: the dind service never starts, and anything waiting for the engine hangs until the job times out — an hour by default. That's why the build job above sets timeout: 20m.

The snippet also depends on the executor:

Building without privileged mode

Kaniko builds images from a Dockerfile without a Docker engine, so it runs as an ordinary unprivileged job on any executor. It replaces the build job like this:

build-image:
  stage: build
  image:
    name: martizih/kaniko:v1.28.5-debug
    entrypoint: [""]
  timeout: 20m
  rules:
    - if: $CI_PIPELINE_SOURCE == "web"
    - changes:
        - Dockerfile
        - .gitlab-ci.yml
  script:
    - |
      cat > /kaniko/.docker/config.json <<EOF
      {"auths":{"$CI_REGISTRY":{"auth":"$(printf '%s:%s' "$CI_REGISTRY_USER" "$CI_REGISTRY_PASSWORD" | base64 | tr -d '\n')"}}}
      EOF
    - /kaniko/executor
        --context "$CI_PROJECT_DIR"
        --dockerfile "$CI_PROJECT_DIR/Dockerfile"
        --destination "$CI_REGISTRY_IMAGE/ansible-lint:latest"

The -debug variant is needed because it ships a shell for script:, and the empty entrypoint stops Kaniko from running before the script does. Upstream Kaniko (gcr.io/kaniko-project/executor) was archived in 2025; it still works, but martizih/kaniko is a maintained fork. GitLab's own docs also point at BuildKit (rootless) and Buildah.

Why not GitLab's Podman setup

GitLab also documents Podman on the Kubernetes executor, with the quay.io/podman/stable image. Podman builds images without a separate engine, so there's no dind service to wait for. The page gives two runner configurations, and neither is a job-level change like Kaniko:

Both need a runner or cluster admin to change something. The Kaniko job above only needs .gitlab-ci.yml.

If the build fails at random with i/o timeout while pulling the base image or packages, see Building CI images on a flaky network.

These pipelines were run on a GitLab runner on a private RKE2 (Kubernetes) node, not in the Linux cloud container where this entry was drafted.

Created 2026-09-05T19:56:41+02:00, updated 2026-10-27T08:43:37+01:00 · History · Edit