Jenkins

Run Jenkins on OpenShift from the cluster's Jenkins image, and keep the pipeline in a Jenkinsfile that calls the verify script and starts a build.

Updated CompTech Team

The continuous integration lab is a script, ci/verify.sh. This lab runs that script from Jenkins, on OpenShift. The pipeline lives in a Jenkinsfile in the same Git repository as the code. A job that only exists in the Jenkins UI is not a pipeline you can review.

OpenShift Pipelines is the pipeline engine that ships with the platform. Use this lab when the team already has Jenkinsfiles. The branch rules are the ones from the source control lab: main moves through a pull request, and verify has to be green.

Prerequisites

  • OpenShift 4.x and oc
  • The cluster can pull registry.redhat.io/ocp-tools-4/jenkins-rhel9. The samples operator installs an ImageStream named jenkins that points at this image. The tag matches the cluster minor. On 4.22 that tag is v4.22.0.
  • A Git repository Jenkins can clone
  • The ci/verify.sh script from the CI lab, on main

Install Jenkins

The supported install is the jenkins-persistent template. It creates a DeploymentConfig, a Service, a Route, a PersistentVolumeClaim, and a service account. The DeploymentConfig has an image-change trigger on the jenkins ImageStream, which is the same trigger shape as the DeploymentConfig in the EAP lab.

oc get template jenkins-persistent -n openshift
oc new-project ci
oc new-app jenkins-persistent -p MEMORY_LIMIT=2Gi -p VOLUME_CAPACITY=5Gi -p NAMESPACE=openshift
oc rollout status dc/jenkins
oc get route jenkins

NAMESPACE=openshift is where the template looks up the jenkins ImageStream, not the project you deploy into. Log in through the Route with the same user as oc whoami. The template wires OpenShift login. The first login takes a minute while Jenkins copies plugins into the volume.

If oc get template says the template is missing, the samples operator is not installing the Jenkins sample. An administrator enables that sample for this cluster version. The import below only updates the ImageStream. Jenkins itself still comes from jenkins-persistent once that template is present:

oc import-image jenkins \
  --from=registry.redhat.io/ocp-tools-4/jenkins-rhel9:v4.22.0 \
  --confirm -n openshift

Change v4.22.0 to your cluster's minor version. The agent image, when a pipeline asks for the default agent, is registry.redhat.io/ocp-tools-4/jenkins-agent-base-rhel9 with the same version tag. That agent image does not contain Maven. The Jenkinsfile below brings its own Maven container.

The Jenkinsfile

Put Jenkinsfile at the root of the application repository, next to ci/verify.sh. This one uses the Kubernetes plugin, which the OpenShift Jenkins image already has, and runs Maven in a UBI OpenJDK container:

pipeline {
  agent {
    kubernetes {
      yaml """
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: maven
    image: registry.access.redhat.com/ubi9/openjdk-21:latest
    command: ["sleep", "infinity"]
    resources:
      requests: { cpu: "200m", memory: "512Mi" }
      limits: { memory: "1Gi" }
"""
    }
  }
  stages {
    stage("Verify") {
      steps {
        container("maven") {
          sh "./ci/verify.sh"
        }
      }
    }
    stage("Image") {
      when { branch "main" }
      steps {
        script {
          openshift.withCluster() {
            openshift.withProject("inventory") {
              openshift.startBuild("inventory", "--follow")
            }
          }
        }
      }
    }
  }
}

Verify is the CI contract. It runs on every branch, so a pull request goes red before it merges. Image runs only on main. The when line applies to a multibranch Pipeline; a job that only builds main can drop it. The OpenShift Client plugin, already in this Jenkins image, starts the BuildConfig from the container builds lab. It uses the Jenkins service account, which needs to create builds in the application project. For a lab project:

oc policy add-role-to-user edit system:serviceaccount:ci:jenkins -n inventory

That role is wider than a production pipeline should have. Narrow it to build creation on inventory when you leave the lab. The DevSecOps lab is where the image gets scanned and signed after this stage.

sleep infinity keeps the Maven container up so the plugin can exec into it. The plugin adds its own jnlp container. You do not put an agent jar in the UBI image.

A job that reads the file

In Jenkins: New Item, name it after the repository, choose Pipeline, and under Pipeline select "Pipeline script from SCM". Point SCM at the Git URL, the branch */main plus the feature branches you want built, and the script path Jenkinsfile.

A multibranch Pipeline is the better object once the repository has pull requests. It discovers branches and builds each one. The Jenkinsfile stays the same.

Do not paste the pipeline into the job. A change to the stages is a commit, reviewed like any other change to the repository. That is what "pipeline as code" means here.

The OpenShift Jenkins image can also watch a BuildConfig whose strategy type is JenkinsPipeline and whose jenkinsfilePath is Jenkinsfile. That strategy is deprecated. Keep the Jenkinsfile. Run it from the Pipeline job above, or move the same stages to a Tekton Pipeline in the OpenShift Pipelines lab.

Save the job and choose Build Now. The first run pulls ubi9/openjdk-21 and then runs ./ci/verify.sh. A green Verify stage is the check the source control lab is waiting for. Publish it back to GitHub or GitLab with the plugin your host uses, and name the status verify, or change protection.json to the name Jenkins actually reports.

See it roll

oc get pods -n ci
oc logs dc/jenkins -n ci --tail=20
oc get builds -n inventory

Merge a pull request to main. The Image stage calls openshift.startBuild("inventory", "--follow"), the client plugin's form of oc start-build inventory --follow, and the inventory ImageStream tag moves. If the Deployment has an image-change trigger, the new tag rolls it.

The Jenkins home is the PersistentVolumeClaim. oc delete pod -l name=jenkins -n ci starts a new pod with the same jobs. jenkins-ephemeral is the template without that volume. A restart on the ephemeral template drops the jobs you created in the UI and keeps whatever is in Git. Prefer the persistent template, and prefer Git either way.

Troubleshooting

Symptom Likely cause Fix
jenkins-persistent is not found Samples operator is not installing it Import the image, or enable the Jenkins sample for your cluster version
Login redirect fails Route or OAuth annotation missing oc describe route jenkins; the template's Route is the one that works
Image stage cannot find openshift The job is not running on the OpenShift Jenkins image Use the jenkins-persistent template; the client plugin is part of that image
verify.sh is not found The checkout is not the repository root Script path is Jenkinsfile; the script is ci/verify.sh from the root
Build is forbidden The Jenkins service account cannot see inventory The oc policy add-role-to-user command above, on that project

Clean up

oc delete project ci

What's next

OpenShift Pipelines runs the same verify script as a Tekton Task. GitLab CI runs it from .gitlab-ci.yml. Container builds is the BuildConfig the Image stage starts. The DevOps overview is the map these pieces sit on.