From fef59450af524e52593c96247c4271bc020a8a36 Mon Sep 17 00:00:00 2001 From: Manuele Simi Date: Thu, 22 Aug 2019 20:27:55 -0400 Subject: [PATCH] Define dynamic stages with sequential steps. --- examples/dynamyc_sequential | 28 ----------------------- examples/dynamyc_sequential_stages | 36 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 28 deletions(-) delete mode 100644 examples/dynamyc_sequential create mode 100644 examples/dynamyc_sequential_stages diff --git a/examples/dynamyc_sequential b/examples/dynamyc_sequential deleted file mode 100644 index 87f19de..0000000 --- a/examples/dynamyc_sequential +++ /dev/null @@ -1,28 +0,0 @@ -def list -pipeline { - agent { - label 'pipeline-agent' - } - - stages { - stage('Create Job List') { - steps { - script { - // you may create your list here, lets say reading from a file after checkout - list = ["Test-1", "Test-2", "Test-3", "Test-4", "Test-5"] - } - } - } - stage('Dynamic Stages') { - steps { - script { - for(int i=0; i < list.size(); i++) { - stage(list[i]){ - echo "Element: $i" - } - } - } - } - } - } -} \ No newline at end of file diff --git a/examples/dynamyc_sequential_stages b/examples/dynamyc_sequential_stages new file mode 100644 index 0000000..c4b285e --- /dev/null +++ b/examples/dynamyc_sequential_stages @@ -0,0 +1,36 @@ +def apps = "app1,app2,app3".split(",").findAll { it }.collect { it.trim() } +def jobs = "job1,job2,job3,job4,job5".split(",").findAll { it }.collect { it.trim() } + +def environment = env.ENVIRONMENT +def version = env.VERSION +def dynamicStages = [:] + +if (apps.size() < 1) { + error("ERROR: APPLICATIONS must be a comma-delimited list of applications to build") +} + +for (int i = 0; i < apps.size(); i++) { + def app = apps[i] + dynamicStages["stage-${app}"] = { + node { + stage("Build ${app}") { + for (int i2 = 0; i2 < jobs.size(); i2++) { + build "${i2}" + } + } + } + } +} + +pipeline { + agent none + stages { + stage('Build apps(s)') { + steps { + script { + parallel dynamicStages + } + } + } + } +} \ No newline at end of file