Skip to content

Instantly share code, notes, and snippets.

@sheeeng
Last active January 26, 2022 14:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheeeng/8d9274ccd9f9d6370feba6f5888f5dee to your computer and use it in GitHub Desktop.
Save sheeeng/8d9274ccd9f9d6370feba6f5888f5dee to your computer and use it in GitHub Desktop.
Generate parallel stages in Jenkinsfile Pipeline using Scripted Pipeline. However, the agents/nodes will not able to get the SCM credential and branch name automatically compared to Declarative Pipeline.
def stagesList = [
"SubStageA",
"SubStageB",
"SubStageC"
]
def parallelStagesMap = stagesList.collectEntries {
["${it}" : generateStage(it)]
}
def generateStage(job) {
return {
stage("Parallel SubStage: ${job}") {
node() {
// What the heck is this PATH+ANYSTRING syntax?
// ${ANYSTRING}:$PATH is expressed as PATH+ANYSTRING=${ANYSTRING}.
// ANYSTRING is just a name to help readability.
// Omit ANYSTRING if you think it does not help readability.
// Thus, it is equivalent to PATH+=${ANYSTRING}.
// https://stackoverflow.com/a/44380495/4763512
withEnv([
"PATH+JENKINS_HOME=${JENKINS_HOME}:${PATH}",
"PYTHONPATH=${WORKSPACE}/python"
]) {
// https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#sh-shell-script
echo sh(returnStdout: true, script: 'env') // sh 'env'
echo "PATH is: $PATH"
echo "PYTHONPATH is: $PYTHONPATH"
unstash "file1"
unstash "file2"
unstash "file3"
unstash "file4"
}
}
}
}
}
pipeline {
agent any
stages {
stage('non-parallel stage') {
steps {
sh '''#!/usr/bin/env bash
touch file{1..4}.txt'''
stash includes: 'file1.txt', name: 'file1'
stash includes: 'file2.txt', name: 'file2'
stash includes: 'file3.txt', name: 'file3'
stash includes: 'file4.txt', name: 'file4'
}
}
stage('parallel stage') {
steps {
script {
parallelStagesMap.failFast = true
parallel parallelStagesMap
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment