1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-03-03 15:02:35 +02:00

Take proper jnlp image as default for Kubernetes execution (#759)

* Take proper jnlp image as default for Kubernetes execution

Following changes are contained:

* removal of custom jnlp image as default
* allow customization of jnlp image via system environment

fixes #757

* add documentation
This commit is contained in:
Oliver Nocon 2019-07-17 12:01:24 +02:00 committed by GitHub
parent 31b9874eff
commit be33eccbec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 7 deletions

View File

@ -38,7 +38,6 @@ general:
githubServerUrl: 'https://github.com'
gitSshKeyCredentialsId: '' #needed to allow sshagent to run with local ssh key
jenkinsKubernetes:
jnlpAgent: 's4sdk/jenkins-agent-k8s:latest'
securityContext:
# Setting security context globally is currently not working with jaas
# runAsUser: 1000

View File

@ -261,12 +261,10 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
assertThat(containerName, is('mavenexecute'))
assertThat(containersList, allOf(
hasItem('jnlp'),
hasItem('mavenexecute'),
hasItem('selenium'),
))
assertThat(imageList, allOf(
hasItem('s4sdk/jenkins-agent-k8s:latest'),
hasItem('maven:3.5-jdk-8-alpine'),
hasItem('selenium/standalone-chrome'),
))
@ -388,6 +386,54 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
assertThat(podNodeSelector, is('size:big'))
}
@Test
void testDockerExecuteOnKubernetesCustomJnlpViaEnv() {
nullScript.configuration = [
general: [jenkinsKubernetes: [jnlpAgent: 'config/jnlp:latest']]
]
binding.variables.env.JENKINS_JNLP_IMAGE = 'env/jnlp:latest'
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
dockerImage: 'maven:3.5-jdk-8-alpine',
) { bodyExecuted = true }
assertTrue(bodyExecuted)
assertThat(containersList, allOf(
hasItem('jnlp'),
hasItem('container-exec')
))
assertThat(imageList, allOf(
hasItem('env/jnlp:latest'),
hasItem('maven:3.5-jdk-8-alpine'),
))
}
@Test
void testDockerExecuteOnKubernetesCustomJnlpViaConfig() {
nullScript.configuration = [
general: [jenkinsKubernetes: [jnlpAgent: 'config/jnlp:latest']]
]
binding.variables.env.JENKINS_JNLP_IMAGE = 'config/jnlp:latest'
stepRule.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
dockerImage: 'maven:3.5-jdk-8-alpine',
) { bodyExecuted = true }
assertTrue(bodyExecuted)
assertThat(containersList, allOf(
hasItem('jnlp'),
hasItem('container-exec')
))
assertThat(imageList, allOf(
hasItem('config/jnlp:latest'),
hasItem('maven:3.5-jdk-8-alpine'),
))
}
private container(options, body) {
containerName = options.name

View File

@ -106,6 +106,15 @@ import hudson.AbortException
/**
* Executes a closure inside a container in a kubernetes pod.
* Proxy environment variables defined on the Jenkins machine are also available in the container.
*
* By default jnlp agent defined for kubernetes-plugin will be used (see https://github.com/jenkinsci/kubernetes-plugin#pipeline-support).
*
* It is possible to define a custom jnlp agent image by
*
* 1. Defining the jnlp image via environment variable JENKINS_JNLP_IMAGE in the Kubernetes landscape
* 2. Defining the image via config (`jenkinsKubernetes.jnlpAgent`)
*
* Option 1 will take precedence over option 2.
*/
@GenerateDocumentation
void call(Map parameters = [:], body) {
@ -262,10 +271,17 @@ private void unstashWorkspace(config, prefix) {
}
private List getContainerList(config) {
def result = [[
name: 'jnlp',
image: config.jenkinsKubernetes.jnlpAgent
]]
//If no custom jnlp agent provided as default jnlp agent (jenkins/jnlp-slave) as defined in the plugin, see https://github.com/jenkinsci/kubernetes-plugin#pipeline-support
def result = []
//allow definition of jnlp image via environment variable JENKINS_JNLP_IMAGE in the Kubernetes landscape or via config as fallback
if (env.JENKINS_JNLP_IMAGE || config.jenkinsKubernetes.jnlpAgent) {
result.push([
name: 'jnlp',
image: env.JENKINS_JNLP_IMAGE ?: config.jenkinsKubernetes.jnlpAgent
])
}
config.containerMap.each { imageName, containerName ->
def containerPullImage = config.containerPullImageFlags?.get(imageName)
def containerSpec = [