Configuring Jenkins images

OKD provides a container image for running Jenkins. This image provides a Jenkins server instance, which can be used to set up a basic flow for continuous testing, integration, and delivery.

The image is based on the Red Hat Universal Base Images (UBI).

OKD follows the LTS release of Jenkins. OKD provides an image that contains Jenkins 2.x.

The OKD Jenkins images are available on Quay.io or registry.redhat.io.

For example:

  1. $ podman pull registry.redhat.io/openshift4/ose-jenkins:<v4.3.0>

To use these images, you can either access them directly from these registries or push them into your OKD container image registry. Additionally, you can create an image stream that points to the image, either in your container image registry or at the external location. Your OKD resources can then reference the image stream.

But for convenience, OKD provides image streams in the openshift namespace for the core Jenkins image as well as the example Agent images provided for OKD integration with Jenkins.

Configuration and customization

You can manage Jenkins authentication in two ways:

  • OKD OAuth authentication provided by the OKD Login plug-in.

  • Standard authentication provided by Jenkins.

OKD OAuth authentication

OAuth authentication is activated by configuring options on the Configure Global Security panel in the Jenkins UI, or by setting the OPENSHIFT_ENABLE_OAUTH environment variable on the Jenkins Deployment configuration to anything other than false. This activates the OKD Login plug-in, which retrieves the configuration information from pod data or by interacting with the OKD API server.

Valid credentials are controlled by the OKD identity provider.

Jenkins supports both browser and non-browser access.

Valid users are automatically added to the Jenkins authorization matrix at log in, where OKD roles dictate the specific Jenkins permissions that users have. The roles used by default are the predefined admin, edit, and view. The login plug-in executes self-SAR requests against those roles in the project or namespace that Jenkins is running in.

Users with the admin role have the traditional Jenkins administrative user permissions. Users with the edit or view role have progressively fewer permissions.

The default OKD admin, edit, and view roles and the Jenkins permissions those roles are assigned in the Jenkins instance are configurable.

When running Jenkins in an OKD pod, the login plug-in looks for a config map named openshift-jenkins-login-plugin-config in the namespace that Jenkins is running in.

If this plug-in finds and can read in that config map, you can define the role to Jenkins Permission mappings. Specifically:

  • The login plug-in treats the key and value pairs in the config map as Jenkins permission to OKD role mappings.

  • The key is the Jenkins permission group short ID and the Jenkins permission short ID, with those two separated by a hyphen character.

  • If you want to add the Overall Jenkins Administer permission to an OKD role, the key should be Overall-Administer.

  • To get a sense of which permission groups and permissions IDs are available, go to the matrix authorization page in the Jenkins console and IDs for the groups and individual permissions in the table they provide.

  • The value of the key and value pair is the list of OKD roles the permission should apply to, with each role separated by a comma.

  • If you want to add the Overall Jenkins Administer permission to both the default admin and edit roles, as well as a new Jenkins role you have created, the value for the key Overall-Administer would be admin,edit,jenkins.

The admin user that is pre-populated in the OKD Jenkins image with administrative privileges is not given those privileges when OKD OAuth is used. To grant these permissions the OKD cluster administrator must explicitly define that user in the OKD identity provider and assigns the admin role to the user.

Jenkins users’ permissions that are stored can be changed after the users are initially established. The OKD Login plug-in polls the OKD API server for permissions and updates the permissions stored in Jenkins for each user with the permissions retrieved from OKD. If the Jenkins UI is used to update permissions for a Jenkins user, the permission changes are overwritten the next time the plug-in polls OKD.

You can control how often the polling occurs with the OPENSHIFT_PERMISSIONS_POLL_INTERVAL environment variable. The default polling interval is five minutes.

The easiest way to create a new Jenkins service using OAuth authentication is to use a template.

Jenkins authentication

Jenkins authentication is used by default if the image is run directly, without using a template.

The first time Jenkins starts, the configuration is created along with the administrator user and password. The default user credentials are admin and password. Configure the default password by setting the JENKINS_PASSWORD environment variable when using, and only when using, standard Jenkins authentication.

Procedure

  • Create a Jenkins application that uses standard Jenkins authentication:

    1. $ oc new-app -e \
    2. JENKINS_PASSWORD=<password> \
    3. openshift4/ose-jenkins

Jenkins environment variables

The Jenkins server can be configured with the following environment variables:

VariableDefinitionExample values and settings

OPENSHIFT_ENABLE_OAUTH

Determines whether the OKD Login plug-in manages authentication when logging in to Jenkins. To enable, set to true.

Default: false

JENKINS_PASSWORD

The password for the admin user when using standard Jenkins authentication. Not applicable when OPENSHIFT_ENABLE_OAUTH is set to true.

Default: password

JAVA_MAX_HEAP_PARAM, CONTAINER_HEAP_PERCENT, JENKINS_MAX_HEAP_UPPER_BOUND_MB

These values control the maximum heap size of the Jenkins JVM. If JAVA_MAX_HEAP_PARAM is set, its value takes precedence. Otherwise, the maximum heap size is dynamically calculated as CONTAINER_HEAP_PERCENT of the container memory limit, optionally capped at JENKINS_MAX_HEAP_UPPER_BOUND_MB MiB.

By default, the maximum heap size of the Jenkins JVM is set to 50% of the container memory limit with no cap.

JAVA_MAX_HEAP_PARAM example setting: -Xmx512m

CONTAINER_HEAP_PERCENT default: 0.5, or 50%

JENKINS_MAX_HEAP_UPPER_BOUND_MB example setting: 512 MiB

JAVA_INITIAL_HEAP_PARAM, CONTAINER_INITIAL_PERCENT

These values control the initial heap size of the Jenkins JVM. If JAVA_INITIAL_HEAP_PARAM is set, its value takes precedence. Otherwise, the initial heap size is dynamically calculated as CONTAINER_INITIAL_PERCENT of the dynamically calculated maximum heap size.

By default, the JVM sets the initial heap size.

JAVA_INITIAL_HEAP_PARAM example setting: -Xms32m

CONTAINER_INITIAL_PERCENT example setting: 0.1, or 10%

CONTAINER_CORE_LIMIT

If set, specifies an integer number of cores used for sizing numbers of internal JVM threads.

Example setting: 2

JAVA_TOOL_OPTIONS

Specifies options to apply to all JVMs running in this container. It is not recommended to override this value.

Default: -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Dsun.zip.disableMemoryMapping=true

JAVA_GC_OPTS

Specifies Jenkins JVM garbage collection parameters. It is not recommended to override this value.

Default: -XX:+UseParallelGC -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=10 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90

JENKINS_JAVA_OVERRIDES

Specifies additional options for the Jenkins JVM. These options are appended to all other options, including the Java options above, and may be used to override any of them if necessary. Separate each additional option with a space; if any option contains space characters, escape them with a backslash.

Example settings: -Dfoo -Dbar; -Dfoo=first\ value -Dbar=second\ value.

JENKINS_OPTS

Specifies arguments to Jenkins.

INSTALL_PLUGINS

Specifies additional Jenkins plug-ins to install when the container is first run or when OVERRIDE_PV_PLUGINS_WITH_IMAGE_PLUGINS is set to true. Plug-ins are specified as a comma-delimited list of name:version pairs.

Example setting: git:3.7.0,subversion:2.10.2.

OPENSHIFT_PERMISSIONS_POLL_INTERVAL

Specifies the interval in milliseconds that the OKD Login plug-in polls OKD for the permissions that are associated with each user that is defined in Jenkins.

Default: 300000 - 5 minutes

OVERRIDE_PV_CONFIG_WITH_IMAGE_CONFIG

When running this image with an OKD persistent volume (PV) for the Jenkins configuration directory, the transfer of configuration from the image to the PV is performed only the first time the image starts because the PV is assigned when the persistent volume claim (PVC) is created. If you create a custom image that extends this image and updates the configuration in the custom image after the initial startup, the configuration is not copied over unless you set this environment variable to true.

Default: false

OVERRIDE_PV_PLUGINS_WITH_IMAGE_PLUGINS

When running this image with an OKD PV for the Jenkins configuration directory, the transfer of plug-ins from the image to the PV is performed only the first time the image starts because the PV is assigned when the PVC is created. If you create a custom image that extends this image and updates plug-ins in the custom image after the initial startup, the plug-ins are not copied over unless you set this environment variable to true.

Default: false

ENABLE_FATAL_ERROR_LOG_FILE

When running this image with an OKD PVC for the Jenkins configuration directory, this environment variable allows the fatal error log file to persist when a fatal error occurs. The fatal error file is saved at /var/lib/jenkins/logs.

Default: false

NODEJS_SLAVE_IMAGE

Setting this value overrides the image that is used for the default Node.js agent pod configuration. A related image stream tag named jenkins-agent-nodejs is in the project. This variable must be set before Jenkins starts the first time for it to have an effect.

Default Node.js agent image in Jenkins server: image-registry.openshift-image-registry.svc:5000/openshift/jenkins-agent-nodejs:latest

MAVEN_SLAVE_IMAGE

Setting this value overrides the image used for the default maven agent pod configuration. A related image stream tag named jenkins-agent-maven is in the project. This variable must be set before Jenkins starts the first time for it to have an effect.

Default Maven agent image in Jenkins server: image-registry.openshift-image-registry.svc:5000/openshift/jenkins-agent-maven:latest

AGENT_BASE_IMAGE

Setting this value overrides the image used for the jnlp container in the sample Kubernetes plug-in pod templates provided with this image. Otherwise, the image from the jenkins-agent-base:latest image stream tag in the openshift namespace is used.

Default: image-registry.openshift-image-registry.svc:5000/openshift/jenkins-agent-base:latest

JAVA_BUILDER_IMAGE

Setting this value overrides the image used for the java-builder container in the java-builder sample Kubernetes plug-in pod templates provided with this image. Otherwise, the image from the java:latest image stream tag in the openshift namespace is used.

Default: image-registry.openshift-image-registry.svc:5000/openshift/java:latest

NODEJS_BUILDER_IMAGE

Setting this value overrides the image used for the nodejs-builder container in the nodejs-builder sample Kubernetes plug-in pod templates provided with this image. Otherwise, the image from the nodejs:latest image stream tag in the openshift namespace is used.

Default: image-registry.openshift-image-registry.svc:5000/openshift/nodejs:latest

Providing Jenkins cross project access

If you are going to run Jenkins somewhere other than your same project, you must provide an access token to Jenkins to access your project.

Procedure

  1. Identify the secret for the service account that has appropriate permissions to access the project Jenkins must access:

    1. $ oc describe serviceaccount jenkins

    Example output

    1. Name: default
    2. Labels: <none>
    3. Secrets: { jenkins-token-uyswp }
    4. { jenkins-dockercfg-xcr3d }
    5. Tokens: jenkins-token-izv1u
    6. jenkins-token-uyswp

    In this case the secret is named jenkins-token-uyswp.

  2. Retrieve the token from the secret:

    1. $ oc describe secret <secret name from above>

    Example output

    1. Name: jenkins-token-uyswp
    2. Labels: <none>
    3. Annotations: kubernetes.io/service-account.name=jenkins,kubernetes.io/service-account.uid=32f5b661-2a8f-11e5-9528-3c970e3bf0b7
    4. Type: kubernetes.io/service-account-token
    5. Data
    6. ====
    7. ca.crt: 1066 bytes
    8. token: eyJhbGc..<content cut>....wRA

    The token parameter contains the token value Jenkins requires to access the project.

Jenkins cross volume mount points

The Jenkins image can be run with mounted volumes to enable persistent storage for the configuration:

  • /var/lib/jenkins is the data directory where Jenkins stores configuration files, including job definitions.

Customizing the Jenkins image through source-to-image

To customize the official OKD Jenkins image, you can use the image as a source-to-image (S2I) builder.

You can use S2I to copy your custom Jenkins jobs definitions, add additional plug-ins, or replace the provided config.xml file with your own, custom, configuration.

To include your modifications in the Jenkins image, you must have a Git repository with the following directory structure:

plugins

This directory contains those binary Jenkins plug-ins you want to copy into Jenkins.

plugins.txt

This file lists the plug-ins you want to install using the following syntax:

  1. pluginId:pluginVersion

configuration/jobs

This directory contains the Jenkins job definitions.

configuration/config.xml

This file contains your custom Jenkins configuration.

The contents of the configuration/ directory is copied to the /var/lib/jenkins/ directory, so you can also include additional files, such as credentials.xml, there.

Sample build configuration customizes the Jenkins image in OKD

  1. apiVersion: build.openshift.io/v1
  2. kind: BuildConfig
  3. metadata:
  4. name: custom-jenkins-build
  5. spec:
  6. source: (1)
  7. git:
  8. uri: https://github.com/custom/repository
  9. type: Git
  10. strategy: (2)
  11. sourceStrategy:
  12. from:
  13. kind: ImageStreamTag
  14. name: jenkins:2
  15. namespace: openshift
  16. type: Source
  17. output: (3)
  18. to:
  19. kind: ImageStreamTag
  20. name: custom-jenkins:latest
1The source parameter defines the source Git repository with the layout described above.
2The strategy parameter defines the original Jenkins image to use as a source image for the build.
3The output parameter defines the resulting, customized Jenkins image that you can use in deployment configurations instead of the official Jenkins image.

Configuring the Jenkins Kubernetes plug-in

The OKD Jenkins image includes the pre-installed Kubernetes plug-in that allows Jenkins agents to be dynamically provisioned on multiple container hosts using Kubernetes and OKD.

To use the Kubernetes plug-in, OKD provides images that are suitable for use as Jenkins agents, including the Base, Maven, and Node.js images.

Both the Maven and Node.js agent images are automatically configured as Kubernetes pod template images within the OKD Jenkins image configuration for the Kubernetes plug-in. That configuration includes labels for each of the images that can be applied to any of your Jenkins jobs under their Restrict where this project can be run setting. If the label is applied, jobs run under an OKD pod running the respective agent image.

In OKD 4.10 and later, the recommended pattern for running Jenkins agents using the Kubernetes plug-in is to use pod templates with both jnlp and sidecar containers. The jnlp container uses the OKD Jenkins Base agent image to facilitate launching a separate pod for your build. The sidecar container image has the tools needed to build in a particular language within the separate pod that was launched. Many container images from the Red Hat Container Catalog are referenced in the sample image streams present in the openshift namespace. The OKD Jenkins image has two pod templates named java-build and nodejs-builder with sidecar containers that demonstrate this approach. These two pod templates use the latest Java and NodeJS versions provided by the java and nodejs image streams in the openshift namespace.

With this update, in OKD 4.10 and later, the non-sidecar maven and nodejs pod templates for Jenkins are deprecated. These pod templates are planned for removal in a future release. Bug fixes and support are provided through the end of that future life cycle, after which no new feature enhancements will be made.

The Jenkins image also provides auto-discovery and auto-configuration of additional agent images for the Kubernetes plug-in.

With the OKD sync plug-in, the Jenkins image on Jenkins startup searches for the following within the project that it is running or the projects specifically listed in the plug-in’s configuration:

  • Image streams that have the label role set to jenkins-agent.

  • Image stream tags that have the annotation role set to jenkins-agent.

  • Config maps that have the label role set to jenkins-agent.

When it finds an image stream with the appropriate label, or image stream tag with the appropriate annotation, it generates the corresponding Kubernetes plug-in configuration so you can assign your Jenkins jobs to run in a pod that runs the container image that is provided by the image stream.

The name and image references of the image stream or image stream tag are mapped to the name and image fields in the Kubernetes plug-in pod template. You can control the label field of the Kubernetes plug-in pod template by setting an annotation on the image stream or image stream tag object with the key agent-label. Otherwise, the name is used as the label.

Do not log in to the Jenkins console and change the pod template configuration. If you do so after the pod template is created, and the OKD Sync plug-in detects that the image associated with the image stream or image stream tag has changed, it replaces the pod template and overwrites those configuration changes. You cannot merge a new configuration with the existing configuration.

Consider the config map approach if you have more complex configuration needs.

When it finds a config map with the appropriate label, it assumes that any values in the key-value data payload of the config map contain Extensible Markup Language (XML) that is consistent with the configuration format for Jenkins and the Kubernetes plug-in pod templates. One key benefit of using config maps, rather than image streams or image stream tags, is that you can control all the parameters of the Kubernetes plug-in pod template.

Sample config map for jenkins-agent

  1. kind: ConfigMap
  2. apiVersion: v1
  3. metadata:
  4. name: jenkins-agent
  5. labels:
  6. role: jenkins-agent
  7. data:
  8. template1: |-
  9. <org.csanchez.jenkins.plugins.kubernetes.PodTemplate>
  10. <inheritFrom></inheritFrom>
  11. <name>template1</name>
  12. <instanceCap>2147483647</instanceCap>
  13. <idleMinutes>0</idleMinutes>
  14. <label>template1</label>
  15. <serviceAccount>jenkins</serviceAccount>
  16. <nodeSelector></nodeSelector>
  17. <volumes/>
  18. <containers>
  19. <org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate>
  20. <name>jnlp</name>
  21. <image>openshift/jenkins-agent-maven-35-centos7:v3.10</image>
  22. <privileged>false</privileged>
  23. <alwaysPullImage>true</alwaysPullImage>
  24. <workingDir>/tmp</workingDir>
  25. <command></command>
  26. <args>${computer.jnlpmac} ${computer.name}</args>
  27. <ttyEnabled>false</ttyEnabled>
  28. <resourceRequestCpu></resourceRequestCpu>
  29. <resourceRequestMemory></resourceRequestMemory>
  30. <resourceLimitCpu></resourceLimitCpu>
  31. <resourceLimitMemory></resourceLimitMemory>
  32. <envVars/>
  33. </org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate>
  34. </containers>
  35. <envVars/>
  36. <annotations/>
  37. <imagePullSecrets/>
  38. <nodeProperties/>
  39. </org.csanchez.jenkins.plugins.kubernetes.PodTemplate>

The following example shows two containers that reference image streams that are present in the openshift namespace. One container handles the JNLP contract for launching Pods as Jenkins Agents. The other container uses an image with tools for building code in a particular coding language:

  1. kind: ConfigMap
  2. apiVersion: v1
  3. metadata:
  4. name: jenkins-agent
  5. labels:
  6. role: jenkins-agent
  7. data:
  8. template2: |-
  9. <org.csanchez.jenkins.plugins.kubernetes.PodTemplate>
  10. <inheritFrom></inheritFrom>
  11. <name>template2</name>
  12. <instanceCap>2147483647</instanceCap>
  13. <idleMinutes>0</idleMinutes>
  14. <label>template2</label>
  15. <serviceAccount>jenkins</serviceAccount>
  16. <nodeSelector></nodeSelector>
  17. <volumes/>
  18. <containers>
  19. <org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate>
  20. <name>jnlp</name>
  21. <image>image-registry.openshift-image-registry.svc:5000/openshift/jenkins-agent-base:latest</image>
  22. <privileged>false</privileged>
  23. <alwaysPullImage>true</alwaysPullImage>
  24. <workingDir>/home/jenkins/agent</workingDir>
  25. <command></command>
  26. <args>\$(JENKINS_SECRET) \$(JENKINS_NAME)</args>
  27. <ttyEnabled>false</ttyEnabled>
  28. <resourceRequestCpu></resourceRequestCpu>
  29. <resourceRequestMemory></resourceRequestMemory>
  30. <resourceLimitCpu></resourceLimitCpu>
  31. <resourceLimitMemory></resourceLimitMemory>
  32. <envVars/>
  33. </org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate>
  34. <org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate>
  35. <name>java</name>
  36. <image>image-registry.openshift-image-registry.svc:5000/openshift/java:latest</image>
  37. <privileged>false</privileged>
  38. <alwaysPullImage>true</alwaysPullImage>
  39. <workingDir>/home/jenkins/agent</workingDir>
  40. <command>cat</command>
  41. <args></args>
  42. <ttyEnabled>true</ttyEnabled>
  43. <resourceRequestCpu></resourceRequestCpu>
  44. <resourceRequestMemory></resourceRequestMemory>
  45. <resourceLimitCpu></resourceLimitCpu>
  46. <resourceLimitMemory></resourceLimitMemory>
  47. <envVars/>
  48. </org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate>
  49. </containers>
  50. <envVars/>
  51. <annotations/>
  52. <imagePullSecrets/>
  53. <nodeProperties/>
  54. </org.csanchez.jenkins.plugins.kubernetes.PodTemplate>

If you log in to the Jenkins console and make further changes to the pod template configuration after the pod template is created, and the OKD Sync plug-in detects that the config map has changed, it will replace the pod template and overwrite those configuration changes. You cannot merge a new configuration with the existing configuration.

Do not log in to the Jenkins console and change the pod template configuration. If you do so after the pod template is created, and the OKD Sync plug-in detects that the image associated with the image stream or image stream tag has changed, it replaces the pod template and overwrites those configuration changes. You cannot merge a new configuration with the existing configuration.

Consider the config map approach if you have more complex configuration needs.

After it is installed, the OKD Sync plug-in monitors the API server of OKD for updates to image streams, image stream tags, and config maps and adjusts the configuration of the Kubernetes plug-in.

The following rules apply:

  • Removing the label or annotation from the config map, image stream, or image stream tag results in the deletion of any existing PodTemplate from the configuration of the Kubernetes plug-in.

  • If those objects are removed, the corresponding configuration is removed from the Kubernetes plug-in.

  • Either creating appropriately labeled or annotated ConfigMap, ImageStream, or ImageStreamTag objects, or the adding of labels after their initial creation, leads to creating of a PodTemplate in the Kubernetes-plugin configuration.

  • In the case of the PodTemplate by config map form, changes to the config map data for the PodTemplate are applied to the PodTemplate settings in the Kubernetes plug-in configuration and overrides any changes that were made to the PodTemplate through the Jenkins UI between changes to the config map.

To use a container image as a Jenkins agent, the image must run the agent as an entry point. For more details, see the official Jenkins documentation.

Jenkins permissions

If in the config map the <serviceAccount> element of the pod template XML is the OKD service account used for the resulting pod, the service account credentials are mounted into the pod. The permissions are associated with the service account and control which operations against the OKD master are allowed from the pod.

Consider the following scenario with service accounts used for the pod, which is launched by the Kubernetes Plug-in that runs in the OKD Jenkins image.

If you use the example template for Jenkins that is provided by OKD, the jenkins service account is defined with the edit role for the project Jenkins runs in, and the master Jenkins pod has that service account mounted.

The two default Maven and NodeJS pod templates that are injected into the Jenkins configuration are also set to use the same service account as the Jenkins master.

  • Any pod templates that are automatically discovered by the OKD sync plug-in because their image streams or image stream tags have the required label or annotations are configured to use the Jenkins master service account as their service account.

  • For the other ways you can provide a pod template definition into Jenkins and the Kubernetes plug-in, you have to explicitly specify the service account to use. Those other ways include the Jenkins console, the podTemplate pipeline DSL that is provided by the Kubernetes plug-in, or labeling a config map whose data is the XML configuration for a pod template.

  • If you do not specify a value for the service account, the default service account is used.

  • Ensure that whatever service account is used has the necessary permissions, roles, and so on defined within OKD to manipulate whatever projects you choose to manipulate from the within the pod.

Creating a Jenkins service from a template

Templates provide parameter fields to define all the environment variables with predefined default values. OKD provides templates to make creating a new Jenkins service easy. The Jenkins templates should be registered in the default openshift project by your cluster administrator during the initial cluster setup.

The two available templates both define deployment configuration and a service. The templates differ in their storage strategy, which affects whether the Jenkins content persists across a pod restart.

A pod might be restarted when it is moved to another node or when an update of the deployment configuration triggers a redeployment.

  • jenkins-ephemeral uses ephemeral storage. On pod restart, all data is lost. This template is only useful for development or testing.

  • jenkins-persistent uses a Persistent Volume (PV) store. Data survives a pod restart.

To use a PV store, the cluster administrator must define a PV pool in the OKD deployment.

After you select which template you want, you must instantiate the template to be able to use Jenkins.

Procedure

  1. Create a new Jenkins application using one of the following methods:

    • A PV:

      1. $ oc new-app jenkins-persistent
    • Or an emptyDir type volume where configuration does not persist across pod restarts:

      1. $ oc new-app jenkins-ephemeral

With both templates, you can run oc describe on them to see all the parameters available for overriding.

For example:

  1. $ oc describe jenkins-ephemeral

Using the Jenkins Kubernetes plug-in

In the following example, the openshift-jee-sample BuildConfig object causes a Jenkins Maven agent pod to be dynamically provisioned. The pod clones some Java source code, builds a WAR file, and causes a second BuildConfig, openshift-jee-sample-docker to run. The second BuildConfig layers the new WAR file into a container image.

Sample BuildConfig that uses the Jenkins Kubernetes plug-in

  1. kind: List
  2. apiVersion: v1
  3. items:
  4. - kind: ImageStream
  5. apiVersion: image.openshift.io/v1
  6. metadata:
  7. name: openshift-jee-sample
  8. - kind: BuildConfig
  9. apiVersion: build.openshift.io/v1
  10. metadata:
  11. name: openshift-jee-sample-docker
  12. spec:
  13. strategy:
  14. type: Docker
  15. source:
  16. type: Docker
  17. dockerfile: |-
  18. FROM openshift/wildfly-101-centos7:latest
  19. COPY ROOT.war /wildfly/standalone/deployments/ROOT.war
  20. CMD $STI_SCRIPTS_PATH/run
  21. binary:
  22. asFile: ROOT.war
  23. output:
  24. to:
  25. kind: ImageStreamTag
  26. name: openshift-jee-sample:latest
  27. - kind: BuildConfig
  28. apiVersion: build.openshift.io/v1
  29. metadata:
  30. name: openshift-jee-sample
  31. spec:
  32. strategy:
  33. type: JenkinsPipeline
  34. jenkinsPipelineStrategy:
  35. jenkinsfile: |-
  36. node("maven") {
  37. sh "git clone https://github.com/openshift/openshift-jee-sample.git ."
  38. sh "mvn -B -Popenshift package"
  39. sh "oc start-build -F openshift-jee-sample-docker --from-file=target/ROOT.war"
  40. }
  41. triggers:
  42. - type: ConfigChange

It is also possible to override the specification of the dynamically created Jenkins agent pod. The following is a modification to the preceding example, which overrides the container memory and specifies an environment variable.

Sample BuildConfig that the Jenkins Kubernetes Plug-in, specifying memory limit and environment variable

  1. kind: BuildConfig
  2. apiVersion: build.openshift.io/v1
  3. metadata:
  4. name: openshift-jee-sample
  5. spec:
  6. strategy:
  7. type: JenkinsPipeline
  8. jenkinsPipelineStrategy:
  9. jenkinsfile: |-
  10. podTemplate(label: "mypod", (1)
  11. cloud: "openshift", (2)
  12. inheritFrom: "maven", (3)
  13. containers: [
  14. containerTemplate(name: "jnlp", (4)
  15. image: "openshift/jenkins-agent-maven-35-centos7:v3.10", (5)
  16. resourceRequestMemory: "512Mi", (6)
  17. resourceLimitMemory: "512Mi", (7)
  18. envVars: [
  19. envVar(key: "CONTAINER_HEAP_PERCENT", value: "0.25") (8)
  20. ])
  21. ]) {
  22. node("mypod") { (9)
  23. sh "git clone https://github.com/openshift/openshift-jee-sample.git ."
  24. sh "mvn -B -Popenshift package"
  25. sh "oc start-build -F openshift-jee-sample-docker --from-file=target/ROOT.war"
  26. }
  27. }
  28. triggers:
  29. - type: ConfigChange
1A new pod template called mypod is defined dynamically. The new pod template name is referenced in the node stanza.
2The cloud value must be set to openshift.
3The new pod template can inherit its configuration from an existing pod template. In this case, inherited from the Maven pod template that is pre-defined by OKD.
4This example overrides values in the pre-existing container, and must be specified by name. All Jenkins agent images shipped with OKD use the Container name jnlp.
5Specify the container image name again. This is a known issue.
6A memory request of 512 Mi is specified.
7A memory limit of 512 Mi is specified.
8An environment variable CONTAINER_HEAP_PERCENT, with value 0.25, is specified.
9The node stanza references the name of the defined pod template.

By default, the pod is deleted when the build completes. This behavior can be modified with the plug-in or within a pipeline Jenkinsfile.

Upstream Jenkins has more recently introduced a YAML declarative format for defining a podTemplate pipeline DSL in-line with your pipelines. An example of this format, using the sample java-builder pod template that is defined in the OKD Jenkins image:

  1. def nodeLabel = 'java-buidler'
  2. pipeline {
  3. agent {
  4. kubernetes {
  5. cloud 'openshift'
  6. label nodeLabel
  7. yaml """
  8. apiVersion: v1
  9. kind: Pod
  10. metadata:
  11. labels:
  12. worker: ${nodeLabel}
  13. spec:
  14. containers:
  15. - name: jnlp
  16. image: image-registry.openshift-image-registry.svc:5000/openshift/jenkins-agent-base:latest
  17. args: ['\$(JENKINS_SECRET)', '\$(JENKINS_NAME)']
  18. - name: java
  19. image: image-registry.openshift-image-registry.svc:5000/openshift/java:latest
  20. command:
  21. - cat
  22. tty: true
  23. """
  24. }
  25. }
  26. options {
  27. timeout(time: 20, unit: 'MINUTES')
  28. }
  29. stages {
  30. stage('Build App') {
  31. steps {
  32. container("java") {
  33. sh "mvn --version"
  34. }
  35. }
  36. }
  37. }
  38. }

Jenkins memory requirements

When deployed by the provided Jenkins Ephemeral or Jenkins Persistent templates, the default memory limit is 1 Gi.

By default, all other process that run in the Jenkins container cannot use more than a total of 512 MiB of memory. If they require more memory, the container halts. It is therefore highly recommended that pipelines run external commands in an agent container wherever possible.

And if Project quotas allow for it, see recommendations from the Jenkins documentation on what a Jenkins master should have from a memory perspective. Those recommendations proscribe to allocate even more memory for the Jenkins master.

It is recommended to specify memory request and limit values on agent containers created by the Jenkins Kubernetes plug-in. Admin users can set default values on a per-agent image basis through the Jenkins configuration. The memory request and limit parameters can also be overridden on a per-container basis.

You can increase the amount of memory available to Jenkins by overriding the MEMORY_LIMIT parameter when instantiating the Jenkins Ephemeral or Jenkins Persistent template.

Additional resources