In declarative pipelines, Jenkins allows the definition of parallel stages. It further allows scripted pipeline general purpose scripts to create and manipulate the artifacts of the declarative pipeline. For example, it can create stages
dynamically.
I would like to know how I can dynamically create parallel executing stages. Please note I am not talking about steps or actions, which are solved here:
How to properly achieve dynamic parallel action with a declarative pipeline?
Below is a simple Jenkinsfile that dynamically adds stages using groovy code. These stages are sequential. I would like a Jenkinsfile that adds stages dynamically as below, but uses the parallel
construct at the stage
level. As a result, three stages that run parallel should be generated by the program.
pipeline {
agent any
stages {
stage('Add regression tests') {
steps {
addStage('offline','open-source','webgoat')
addStage('offline','open-source','juice-shop')
addStage('online','internal','our-website')
}
}
}
}
final void addStage(final String type, final String suite,final String application) {
script {
stage("Run ${type} application '\n${application}' in test suite '${suite}'") {
runTestOnApplication(type,suite,application)
}
}
}
final void runTestOnApplication(final String type, final String suite,final String application) {
labelledShell label: "Run test shell script for ${type} application '${application}' in test suite '${suite}'", script: 'test.bash'
}