How can I build Jenkins Projects with a custom JDK?
Asked Answered
C

2

2

I have a regular Jenkins instance running with some multibranch pipelines. The instance runs on JDK 11, as higher versions are not really supported with Jenkins. That's alright.

What is not alright though, is that all my pipelines also seem to be capped to Java 11 by that. Jenkins just runs all the builds with the JDK it uses itself too. That is not alright, though.

At this point, any solution would do, but ideally I would just like to have two seperate JDKs: JDK 11, for Jenkins itself and another JDK it automatically runs all builds on.

Is there any way to achieve this? Thanks in advance - Emil

Cremate answered 24/8, 2021 at 15:14 Comment(0)
I
4

You can use the desired JDK with a docker agent. With this, you can defined an image which use the appropriate JDK or any image.

For example, in your case, you can have the JDK 11 for jenkins itself and the one you want in each job. Example with openjdk :

pipeline {
    agent {
        docker { image 'openjdk:11' }
    }
    stages {
        stage('Test java version') {
            steps {
                sh 'java -version'
            }
        }
    }
}

You can choose the tag you want to select the appropriate version.

You just need to setup docker in your jenkins instance.

Itol answered 24/8, 2021 at 15:49 Comment(0)
G
2

You may not need to go the docker agent route, especially of you particular JDK does not have an image.

JDK for JENKINS itself

When you run Jenkins, it runs on the Java you specify, typically what's available on the command line (ie: java -jar jenkins.war).

When you launch a standard linux agent (SSH Build Agents (aka:ssh-slaves) plugin), by default agents launch using the available JDK/JRE. However, the plugin also allows you to specify a custom JavaPath and runtime options.

This above is the "default" or "System" JDK available to your jobs and steps running on the agent. Note that Jenkins 2.x supports only Java 8 and Java 11. Java 11 is supported in Jenkins 2.164.1 and as of the August releases, Docker images for Controllers (master) use Java 11 by default as noted:

Jenkins 2.306 running as jenkins/jenkins:latest uses Java 8. When Jenkins 2.307 or later is run with jenkins/jenkins:latest, it will use Java 11

Jenkins 2.289.3 running as jenkins/jenkins:lts uses Java 8. When Jenkins 2.303.1 or later is run with jenkins/jenkins:lts, it will use Java 11

JDK for JENKINS Jobs

Jenkins allows multiple tools, including jdk, NodeJS, Docker, ant, gradle, maven, etc. versions to be made available to your build. They must be pre-configured configured under Manage Jenkins → Global Tool Configuration ( ${JENKINS_URL}/configureTools/ ).

The Jenkins "Managing Tools" documentation is lacking at this time, and the Pipeline documentation provides an limited explanation. Cloudbees explains a bit more. This old S/O answer explains the basics, incl duding the often overlooked "JDK" [ Default (System) ], located at the end of the "General" section of your job.

Once you've configured Global defaults, There's also an option to override the default location of the various instances on a per Node level, as noted in this S/O answer.

Whatever options are chosen, that overrides the JAVA_HOME (ANT_HOME, M2_HOME, etc.) made available to your job steps (Job overrides node overrides Global). For pipeline jobs, you can specify that using the "tools" option inside the pipeline block or a stage block, eg:

tools {
    jdk 'jdk1.8' 
}

We have multiple versions (7/8/11) of Oracle JDK, JRockit, OpenJDK Hotspot and OpenJ9 and use the Node level Tool installations to override the installed path, for example on Win boxes.

There's also extra / custom Tool installer plugins which extend this flexible Tool Mgmt to other tools.

For completeness, you can also use the Environment Injector plugin to override environment variables, including the JAVA_HOME for either the whole job or starting from a specific step.

Gokey answered 25/8, 2021 at 9:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.