2019-02-18 09:20:16 +02:00
# ${docGenStepName}
2018-08-21 15:45:59 +02:00
2019-02-18 09:20:16 +02:00
## ${docGenDescription}
2018-08-21 15:45:59 +02:00
2018-10-04 17:06:42 +02:00
## Prerequisites
2018-11-06 14:50:09 +02:00
2018-08-21 15:45:59 +02:00
* The Jenkins should be running on kubernetes.
2018-10-04 17:06:42 +02:00
* An environment variable `ON_K8S` should be created on Jenkins and initialized to `true` . This could for example be done via _Jenkins_ - _Manage Jenkins_ - _Configure System_ - _Global properties_ - _Environment variables_
![Jenkins environment variable configuration ](../images/k8s_env.png )
2019-02-18 09:20:16 +02:00
## ${docGenParameters}
2018-10-04 17:06:42 +02:00
2019-02-18 09:20:16 +02:00
## ${docGenConfiguration}
2018-08-21 15:45:59 +02:00
2019-05-24 15:44:31 +02:00
## ${docJenkinsPluginDependencies}
2019-05-24 15:41:49 +02:00
2018-08-21 15:45:59 +02:00
## Side effects
2018-11-06 14:50:09 +02:00
2018-08-21 15:45:59 +02:00
none
## Exceptions
2018-11-06 14:50:09 +02:00
2018-08-21 15:45:59 +02:00
none
## Example 1: Run a closure in a single container pod
2018-11-06 14:50:09 +02:00
2018-08-21 15:45:59 +02:00
```sh
2018-10-04 17:06:42 +02:00
# set environment variable
2018-08-21 15:45:59 +02:00
export ON_K8S=true"
```
```groovy
dockerExecuteOnKubernetes(script: script, dockerImage: 'maven:3.5-jdk-7'){
2018-10-04 17:06:42 +02:00
sh "mvn clean install"
2018-08-21 15:45:59 +02:00
}
```
In the above example, a pod will be created with a docker container of image `maven:3.5-jdk-7` . The closure will be then executed inside the container.
## Example 2: Run a closure in a multi-container pod
2018-11-06 14:50:09 +02:00
2018-08-21 15:45:59 +02:00
```sh
2018-10-04 17:06:42 +02:00
# set environment variable
2018-08-21 15:45:59 +02:00
export ON_K8S=true"
```
```groovy
2019-10-25 17:49:54 +02:00
dockerExecuteOnKubernetes(script: script, containerMap: ['maven:3.5-jdk-8-alpine': 'maven', 'ppiper/cf-cli': 'cfcli']){
2018-08-21 15:45:59 +02:00
container('maven'){
2018-10-04 17:06:42 +02:00
sh "mvn clean install"
2018-08-21 15:45:59 +02:00
}
container('cfcli'){
sh "cf plugins"
}
}
```
2018-10-04 17:06:42 +02:00
In the above example, a pod will be created with multiple Docker containers that are passed as a `containerMap` . The containers can be chosen for executing by referring their labels as shown in the example.
2018-08-21 15:45:59 +02:00
2018-10-04 17:06:42 +02:00
## Example 3: Running a closure in a dedicated container of a multi-container pod
2018-08-21 15:45:59 +02:00
2018-10-04 17:06:42 +02:00
```sh
# set environment variable
export ON_K8S=true"
```
2018-08-21 15:45:59 +02:00
2018-10-04 17:06:42 +02:00
```groovy
dockerExecuteOnKubernetes(
script: script,
containerCommands: ['selenium/standalone-chrome': ''],
containerMap: ['maven:3.5-jdk-8-alpine': 'maven', 'selenium/standalone-chrome': 'selenium'],
containerName: 'maven',
containerPortMappings: ['selenium/standalone-chrome': [containerPort: 4444, hostPort: 4444]]
containerWorkspaces: ['selenium/standalone-chrome': '']
){
echo "Executing inside a Kubernetes Pod inside 'maven' container to run Selenium tests"
sh "mvn clean install"
}
```