From c6cabe44ebb29ddfb7f9f213643ee24663459028 Mon Sep 17 00:00:00 2001 From: Manuele Simi Date: Fri, 16 Aug 2019 23:24:48 -0400 Subject: [PATCH] Add some useful and tested examples of dynamic pipelines. --- examples/dynamic_parallel_nested | 33 +++++++++++++++++++++ examples/dynamyc_sequential | 28 ++++++++++++++++++ examples/parallel_nested | 51 ++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 examples/dynamic_parallel_nested create mode 100644 examples/dynamyc_sequential create mode 100644 examples/parallel_nested diff --git a/examples/dynamic_parallel_nested b/examples/dynamic_parallel_nested new file mode 100644 index 0000000..1ee5609 --- /dev/null +++ b/examples/dynamic_parallel_nested @@ -0,0 +1,33 @@ +def apps = "job1,job2,job3,job4" +def applications = apps.split(",").findAll { it }.collect { it.trim() } +def environment = env.ENVIRONMENT +def version = env.VERSION +def jobs = [:] + +if (applications.size() < 1) { + error("ERROR: APPLICATIONS must be a comma-delimited list of applications to build") +} + +for (int i = 0; i < applications.size(); i++) { + def app = applications[i] + jobs["jobs-${app}"] = { + node { + stage("Build ${app}") { + build "${app}" + } + } + } +} + +pipeline { + agent none + stages { + stage('Build apps(s)') { + steps { + script { + parallel jobs + } + } + } + } +} \ No newline at end of file diff --git a/examples/dynamyc_sequential b/examples/dynamyc_sequential new file mode 100644 index 0000000..87f19de --- /dev/null +++ b/examples/dynamyc_sequential @@ -0,0 +1,28 @@ +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/parallel_nested b/examples/parallel_nested new file mode 100644 index 0000000..690b650 --- /dev/null +++ b/examples/parallel_nested @@ -0,0 +1,51 @@ +pipeline { + agent any + stages { + stage('Non-Parallel Stage') { + steps { + echo 'This stage will be executed first.' + } + } + stage('Parallel Stage') { + when { + branch 'master' + } + failFast true + parallel { + stage('Branch A') { + agent { + label "for-branch-a" + } + steps { + echo "On Branch A" + } + } + stage('Branch B') { + agent { + label "for-branch-b" + } + steps { + echo "On Branch B" + } + } + stage('Branch C') { + agent { + label "for-branch-c" + } + stages { + stage('Nested 1') { + steps { + echo "In stage Nested 1 within Branch C" + } + } + stage('Nested 2') { + steps { + echo "In stage Nested 2 within Branch C" + } + } + } + } + } + } + } +} \ No newline at end of file