Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

An easier way to create custom Jenkins containers

June 4, 2020
Jay Flowers
Related topics:
CI/CDContainersJavaKubernetes
Related products:
Red Hat OpenShift Container Platform

Share:

    In many organizations, it is a struggle for developers to get custom Jenkins container images created. Fortunately, in engineering, there is often more than one way to get the job done. In this article, I show you how to create your own custom Jenkins container image by aggregating readily available containers in a pod template.

    Note that I'm using Kubernetes Plugin for Jenkins version 1.18.2 for my example application, which is running in Red Hat OpenShift. If you are using a different version, use its documentation.

    Configuring the Jenkinsfile

    As you would expect of any best practice with Jenkins, we'll start with a Jenkinsfile. Ideally, you should locate this file in the root of your Git repository. For my example, I use a declarative pipeline. See the Jenkins documentation if you prefer using a scripted pipeline.

    The example is a simple Java project. We will need just two containers: One for the Java Network Launch Protocol (JNLP) agent and one for the Java tooling. The URL for the JNLP agent image on the Red Hat Registry is registry.redhat.io/openshift4/ose-jenkins-agent-base.

    For the Java tooling image, we have a few options:

    • Use a source-to-image (S2I) image for Red Hat Enterprise Linux 8 (RHEL 8) with OpenJDK 11, found at registry.redhat.io/openjdk/openjdk-11-rhel8.
    • Use a CodeReady Workspaces (CRW) image, found at registry.redhat.io/codeready-workspaces/stacks-java-rhel8.
    • Choose to use a Universal Base Image (UBI) or community image that your organization has already adopted.

    I will use the CodeReady Workspaces (CRW) image, which ensures I get the same results in my CRW IDE as on the Jenkins pipeline. Figure 1 shows the Jenkinsfile so far.

    A screenshot of the Jenkinsfile in the console.
    Figure 1. The Jenkinsfile with two containers.

    Note that every Jenkins agent pod must have a container named jnlp. If you don't provide this container, Kubernetes Plugin for Jenkins is hardcoded to add one (here is an example of that behavior). Unfortunately, the hardcoded image is not compatible with the Red Hat OpenShift Container Platform (OCP) Jenkins image. A future release of OCP Jenkins will provide a default JNLP container via a default pod template.

    You might also notice the imagePullSecrets configuration in Figure 1. I included that because I am pulling the images directly from registry.redhat.io, and the registry requires authentication. You also don't have to use OpenShift secrets; you can explore a variety of options to see what works best for you.

    Build the basic application

    The Jenkinsfile produces a pod with two containers, as specified. The two containers share a working directory and a volume mount that defaults to /home/jenkins/agent. The jnlp container takes care of the declarative checkout source code management (SCM) action. Unless otherwise specified, all other actions are executed in the Jenkins pipeline workspace. Figure 2 shows more of the Jenkinsfile, where we now begin to build the Java application.

    A screenshot of the Jenkinsfile in the console.
    Figure 2. Building the Java application.

    Notice how I use the closure container to specify the java container, which is the YAML configuration from the pod template. The jnlp container is the default execution container. I won't show much more of the example application in this article, but you can find the complete version here.

    Add all of the containers you need

    After adding the jnlp container, you can add as many containers as you like to your pod template. Does your application need to execute Ruby? Try adding the configuration shown in Figure 3.

    A screenshot of the configuration to add a Ruby container.
    Figure 3. Adding a Ruby container to the configuration.

    Once you've added a container, you can use it by entering a command like the one shown in Figure 4. Notice that the container names must match between the pod template and the closure.

    A screenshot of the 'Ruby Build' command.
    Figure 4. Execute the Ruby build.

    If you are using CodeReady Workspaces, then you should use the same images in your devfile.yaml and your Jenkinsfile. Using the same image in both of these files ensures that you always get the same results in your workspace, which becomes more critical once you get into your pipeline.

    Conclusion

    In this article, I've quickly shown you how to aggregate readily available containers into a pod template. This is an efficient way to publish a custom Jenkins container without having to worry about organizational barriers—instead, you can get back to delivering your application. As a developer, I hope this approach makes your life easier.

    If you want to learn more from the example application I used in this article, you can study the complete application code, which includes a devfile, as well as the Jenkinsfile that we developed together. For even more about developing Jenkins pipelines on OCP, see my video.

    Last updated: February 11, 2024

    Recent Posts

    • More Essential AI tutorials for Node.js Developers

    • How to run a fraud detection AI model on RHEL CVMs

    • How we use software provenance at Red Hat

    • Alternatives to creating bootc images from scratch

    • How to update OpenStack Services on OpenShift

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue