github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/documentation/docs/steps/dockerExecuteOnKubernetes.md (about) 1 # ${docGenStepName} 2 3 ## ${docGenDescription} 4 5 ## Prerequisites 6 7 * The Jenkins should be running on kubernetes. 8 * 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_ 9 10  11 12 ## ${docGenParameters} 13 14 ## ${docGenConfiguration} 15 16 ## ${docJenkinsPluginDependencies} 17 18 ## Side effects 19 20 none 21 22 ## Exceptions 23 24 none 25 26 ## Example 1: Run a closure in a single container pod 27 28 ```sh 29 # set environment variable 30 export ON_K8S=true" 31 ``` 32 33 ```groovy 34 dockerExecuteOnKubernetes(script: script, dockerImage: 'maven:3.5-jdk-7'){ 35 sh "mvn clean install" 36 } 37 ``` 38 39 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. 40 41 ## Example 2: Run a closure in a multi-container pod 42 43 ```sh 44 # set environment variable 45 export ON_K8S=true" 46 ``` 47 48 ```groovy 49 dockerExecuteOnKubernetes(script: script, containerMap: ['maven:3.5-jdk-8-alpine': 'maven', 'ppiper/cf-cli:6': 'cfcli']){ 50 container('maven'){ 51 sh "mvn clean install" 52 } 53 container('cfcli'){ 54 sh "cf plugins" 55 } 56 } 57 ``` 58 59 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. 60 61 ## Example 3: Running a closure in a dedicated container of a multi-container pod 62 63 ```sh 64 # set environment variable 65 export ON_K8S=true" 66 ``` 67 68 ```groovy 69 dockerExecuteOnKubernetes( 70 script: script, 71 containerCommands: ['selenium/standalone-chrome': ''], 72 containerMap: ['maven:3.5-jdk-8-alpine': 'maven', 'selenium/standalone-chrome': 'selenium'], 73 containerName: 'maven', 74 containerPortMappings: ['selenium/standalone-chrome': [[containerPort: 4444, hostPort: 4444]]], 75 containerWorkspaces: ['selenium/standalone-chrome': ''] 76 ){ 77 echo "Executing inside a Kubernetes Pod inside 'maven' container to run Selenium tests" 78 sh "mvn clean install" 79 } 80 ```