1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-11-24 08:32:32 +02:00

executeDockerOnKubernetes - specify custom shell (#428)

Depending on the Docker image used the default shell will not work in certain cases.
This extends the executeDockerOnKubernetes step to be able to use a custom shell according to https://github.com/jenkinsci/kubernetes-plugin#specifying-a-different-shell-command-other-than-binsh
This commit is contained in:
Oliver Nocon 2019-01-08 19:44:28 +01:00 committed by GitHub
parent db9ba38ed1
commit 724a851bcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 1 deletions

View File

@ -21,6 +21,7 @@ Executes a closure inside a container in a kubernetes pod. Proxy environment var
|containerMap|no|`[:]`||
|containerName|no|||
|containerPortMappings|no|||
|containerShell|no|||
|containerWorkspaces|no|||
|dockerEnvVars|no|`[:]`||
|dockerImage|yes|||
@ -37,6 +38,7 @@ Executes a closure inside a container in a kubernetes pod. Proxy environment var
* `containerName`: optional configuration in combination with containerMap to define the container where the commands should be executed in
* `containerPortMappings`: Map which defines per docker image the port mappings, like `containerPortMappings: ['selenium/standalone-chrome': [[name: 'selPort', containerPort: 4444, hostPort: 4444]]]`
* `containerShell` allows to specify the shell to be executed for container with containerName
* `containerWorkspaces` specifies workspace (=home directory of user) per container. If not provided `dockerWorkspace` will be used. If empty, home directory will not be set.
* `dockerImage` Name of the docker image that should be used. If empty, Docker is not used.
* `dockerEnvVars` Environment variables to set in the container, e.g. [http_proxy:'proxy:8080']

View File

@ -49,6 +49,7 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
def containersList = []
def imageList = []
def containerName = ''
def containerShell = ''
def envList = []
def portList = []
def containerCommands = []
@ -260,9 +261,23 @@ class DockerExecuteOnKubernetesTest extends BasePiperTest {
assertThat(envList, hasItem(hasItem(allOf(hasEntry('key', 'customEnvKey'), hasEntry ('value','customEnvValue')))))
}
@Test
void testDockerExecuteOnKubernetesWithCustomShell() {
jsr.step.dockerExecuteOnKubernetes(
script: nullScript,
juStabUtils: utils,
dockerImage: 'maven:3.5-jdk-8-alpine',
containerShell: '/busybox/sh'
) {
//nothing to exeute
}
assertThat(containerShell, is('/busybox/sh'))
}
private container(options, body) {
containerName = options.name
containerShell = options.shell
body()
}
}

View File

@ -16,6 +16,7 @@ import hudson.AbortException
'containerMap', //specify multiple images which then form a kubernetes pod, example: containerMap: ['maven:3.5-jdk-8-alpine': 'mavenexecute','selenium/standalone-chrome': 'selenium']
'containerName', //optional configuration in combination with containerMap to define the container where the commands should be executed in
'containerPortMappings', //map which defines per docker image the port mappings, like containerPortMappings: ['selenium/standalone-chrome': [[name: 'selPort', containerPort: 4444, hostPort: 4444]]]
'containerShell', // allows to specify the shell to be executed for container with containerName
'containerWorkspaces', //specify workspace (=home directory of user) per container. If not provided dockerWorkspace will be used. If empty, home directory will not be set.
'dockerImage',
'dockerWorkspace',
@ -77,7 +78,11 @@ void executeOnPod(Map config, utils, Closure body) {
podTemplate(getOptions(config)) {
node(config.uniqueId) {
if (config.containerName) {
container(name: config.containerName){
Map containerParams = [name: config.containerName]
if (config.containerShell) {
containerParams.shell = config.containerShell
}
container(containerParams){
try {
utils.unstashAll(config.stashContent)
body()