GitLab CI

Run a self-hosted GitLab on OpenShift, execute each job in a GitLab Runner pod, and push images with Buildah to the internal registry.

Updated CompTech Team

A pipeline on a shared hosted runner sends your source off the cluster. A self-hosted GitLab keeps the repositories, the job logs and the runners on OpenShift, next to the clusters they deploy to.

This lab stands up GitLab and a GitLab Runner that starts every job as a pod. The sample pipeline tests a service, builds an image with Buildah, and commits the new tag to a GitOps repository. OpenShift GitOps is what rolls that tag out. The runner never applies manifests itself.

Prerequisites

  • An OpenShift 4.x cluster, with cluster-admin for the operator install
  • The oc CLI
  • A project called payments for the application image

Install GitLab and a runner

Install the GitLab operator and the GitLab Runner operator from OperatorHub. Create a project for the server and start from the sample GitLab resource the operator offers. Set spec.chart.version to the chart version on that page, and set the host so the UI is an OpenShift Route:

apiVersion: apps.gitlab.com/v1beta1
kind: GitLab
metadata:
  name: gitlab
  namespace: gitlab-system
spec:
  chart:
    values:
      global:
        hosts:
          domain: apps.example.com
          https: true
        ingress:
          class: openshift-default
          configureCertmanager: false
          annotations:
            route.openshift.io/termination: edge
oc new-project gitlab-system
oc apply -f gitlab.yaml
oc get gitlab -n gitlab-system
oc get secret -n gitlab-system -o name | grep initial-root-password

Sign in, create a group platform and a project payments, and create a runner authentication token. It starts with glrt-. Export it as RUNNER_TOKEN, then create the runner. The custom config makes the Kubernetes executor start jobs in gitlab-runner with the service account that can push images.

oc new-project gitlab-runner
oc create sa gitlab-runner -n gitlab-runner
oc policy add-role-to-user system:image-builder \
  system:serviceaccount:gitlab-runner:gitlab-runner -n payments
oc create secret generic gitlab-runner-secret -n gitlab-runner \
  --from-literal=runner-token="$RUNNER_TOKEN"
oc create configmap runner-config -n gitlab-runner --from-file=config.toml
[[runners]]
  name = "openshift"
  executor = "kubernetes"
  [runners.kubernetes]
    namespace = "gitlab-runner"
    service_account = "gitlab-runner"
    image = "registry.access.redhat.com/ubi9/ubi:latest"
apiVersion: apps.gitlab.com/v1beta2
kind: Runner
metadata:
  name: openshift
  namespace: gitlab-runner
spec:
  gitlabUrl: https://gitlab.apps.example.com
  token: gitlab-runner-secret
  config: runner-config
  tags: "openshift"

If a Buildah job is rejected by the restricted security context, grant anyuid to that service account in gitlab-runner only. Keep STORAGE_DRIVER=vfs so the job still does not need a nested container runtime.

The pipeline

.gitlab-ci.yml in the application repository:

stages: [test, build, release]
variables:
  IMAGE: image-registry.openshift-image-registry.svc:5000/payments/payments

test:
  stage: test
  image: registry.access.redhat.com/ubi9/openjdk-21:latest
  script: ["./mvnw -B verify"]
  tags: [openshift]

build:
  stage: build
  image: quay.io/buildah/stable
  tags: [openshift]
  variables:
    STORAGE_DRIVER: vfs
    BUILDAH_ISOLATION: chroot
  script:
    - buildah login --tls-verify=false -u unused
        -p "$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)"
        image-registry.openshift-image-registry.svc:5000
    - buildah bud -t "$IMAGE:$CI_COMMIT_SHORT_SHA" .
    - buildah push "$IMAGE:$CI_COMMIT_SHORT_SHA"
  rules:
    - if: $CI_COMMIT_BRANCH == "main"

release:
  stage: release
  image: registry.access.redhat.com/ubi9/ubi:latest
  tags: [openshift]
  script:
    - git clone "https://oauth2:${GITOPS_TOKEN}@gitlab.apps.example.com/platform/payments-gitops.git"
    - cd payments-gitops
    - sed -i "s|newTag:.*|newTag: ${CI_COMMIT_SHORT_SHA}|" overlays/dev/kustomization.yaml
    - git config user.email ci@example.com && git config user.name "GitLab CI"
    - git commit -am "deploy ${CI_COMMIT_SHORT_SHA}" && git push
  rules:
    - if: $CI_COMMIT_BRANCH == "main"

Store GITOPS_TOKEN as a masked, protected CI variable. The GitOps overlay's kustomization.yaml carries newTag, which this job rewrites. Push a commit to main and watch the job pods:

oc get pods -n gitlab-runner -w

What usually breaks

Symptom What to fix
Jobs stay pending The runner tag on the job does not match spec.tags, or the runner pod is not Ready
buildah push returns 403 system:image-builder is missing on the payments project
Buildah cannot write /var/lib/containers vfs isolation, or anyuid on the runner service account
release cannot push to GitOps GITOPS_TOKEN is not protected, or it has no write role on that project

What's next

  1. The same build as pods, without GitLab, in OpenShift Pipelines.
  2. Let OpenShift GitOps sync the tag this pipeline commits.
  3. Add scanning and signing in DevSecOps and supply chain before release runs.