Created
March 3, 2022 15:40
-
-
Save dievops/388cce38bb76acce82da8d1ec47c0c1c to your computer and use it in GitHub Desktop.
Jenkinsfile scripted inside declarative with foor loop and parallel agent on kubernetes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pipeline { | |
agent none | |
options {timeout(time: 300, unit: 'Minutes')} | |
environment { | |
IMAGE = "docker:latest" | |
PARALLEL_TEST = "40" // change here how many parallel jobs | |
} | |
stages{ | |
stage('script-test') { | |
agent none | |
options({skipDefaultCheckout true}) | |
steps { | |
script { | |
// Variables | |
println "Running " + env.PARALLEL_TEST.toString() + " parallel tests" | |
def parallelProcesses = env.PARALLEL_TEST.toInteger() | |
def tests = [] // empty list | |
def jobs = [:] //empty map | |
for (int i = 1; i <= parallelProcesses; i++) { | |
tests.add(i.toString()) | |
} | |
println tests // show all tests | |
// building a map | |
tests.each { t -> | |
jobs["jobs-${t}"] = { | |
stage("jobs-${t}") { | |
podTemplate(yaml: """ | |
apiVersion: v1 | |
kind: Pod | |
spec: | |
containers: | |
- name: jobs-test-${t} | |
image: $IMAGE | |
command: ["tail", "-f", "/dev/null"] | |
""") { | |
node(POD_LABEL) { | |
container("jobs-test-${t}") { | |
sh """ | |
echo hello from ${t} | |
""" | |
} | |
} | |
} | |
} | |
} | |
} | |
parallel jobs | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment