1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-18 05:18:24 +02:00

Pass configured env vars to docker execution in existing container (#851)

This commit is contained in:
Florian Geckeler 2019-08-16 17:05:18 +02:00 committed by Florian Wilhelm
parent fa3b6b68db
commit f69eac6f5f
2 changed files with 15 additions and 3 deletions

View File

@ -48,10 +48,15 @@ class DockerExecuteTest extends BasePiperTest {
@Test
void testExecuteInsideContainerOfExistingPod() throws Exception {
List usedDockerEnvVars
helper.registerAllowedMethod('container', [String.class, Closure.class], { String container, Closure body ->
containerName = container
body()
})
helper.registerAllowedMethod('withEnv',[List.class, Closure.class], { List envVars, Closure body ->
usedDockerEnvVars = envVars
body()
})
binding.setVariable('env', [POD_NAME: 'testpod', ON_K8S: 'true'])
ContainerMap.instance.setMap(['testpod': ['maven:3.5-jdk-8-alpine': 'mavenexec']])
stepRule.step.dockerExecute(script: nullScript,
@ -61,6 +66,7 @@ class DockerExecuteTest extends BasePiperTest {
}
assertTrue(loggingRule.log.contains('Executing inside a Kubernetes Container'))
assertEquals('mavenexec', containerName)
assertEquals(usedDockerEnvVars[0].toString(), "http_proxy=http://proxy:8000")
assertTrue(bodyExecuted)
}

View File

@ -133,11 +133,17 @@ void call(Map parameters = [:], body) {
], config)
if (isKubernetes() && config.dockerImage) {
List dockerEnvVars = []
config.dockerEnvVars?.each { key, value ->
dockerEnvVars << "$key=$value"
}
if (env.POD_NAME && isContainerDefined(config)) {
container(getContainerDefined(config)) {
echo "[INFO][${STEP_NAME}] Executing inside a Kubernetes Container."
body()
sh "chown -R 1000:1000 ."
withEnv(dockerEnvVars) {
echo "[INFO][${STEP_NAME}] Executing inside a Kubernetes Container."
body()
sh "chown -R 1000:1000 ."
}
}
} else {
if (!config.sidecarImage) {