I'd like to ask for help with a a Jenkins groovy pipeline, copied from here:
Is it possible to create parallel Jenkins Declarative Pipeline stages in a loop?
I'd like for a several sets of vars to be passed in a under a map, for several stages under a parallel run. However, only the last set (square brackets at the bottom of the map) gets registered for my map.
When the parallel stage runs, the map iterates successfully, but only with the last set (currently install_Stage(it)), ignoring other sets. Meaning that I get a pipeline showing four "stage: install ${product}" stages in parallel, and that's it. I'd like to get three parallels with four stages (network setup, revert, and install), as per my code below:
#!groovy
#Library('ci_builds')
def products = ["A", "B", "C", "D"]
def parallelStagesMap = products.collectEntries {
switch (it) {
case "A":
static_ip_address = "10.100.100.6"; static_vm_name = "install-vm1"; version = "14.1.60"
break
case "B":
static_ip_address = "10.100.100.7"; static_vm_name = "install-vm2"; version = "15.1"
break
case "C":
static_ip_address = "10.100.100.8"; static_vm_name = "install-vm3"; version = "15.1"
break
case "D":
static_ip_address = "10.100.100.9"; static_vm_name = "install-vm4"; version = "15.2"
break
default:
static_ip_address = "The product name is not on the switch list - please enter an ip address"
version = "The product name is not on the switch list - please enter a version"
break
}
["${it}" : network_reg(it)]
["${it}" : revert_to_snapshot_Stage(it)]
["${it}" : install_Stage(it)]
}
def network_reg(product) {
return {
stage("stage: setup network for ${product}") {
echo "setting network on ${static_vm_name} with ${static_ip_address}."
sh script: "sleep 15"
}
}
}
def revert_to_snapshot_Stage(product) {
return {
stage("stage: revert ${product}") {
echo "reverting ${static_vm_name} for ${product} on ${static_ip_address}."
sh script: "sleep 15"
}
}
}
def install_Stage(product) {
return {
stage("stage: install ${product}") {
echo "installing ${product} on ${static_ip_address}."
sh script: "sleep 15"
}
}
}
pipeline {
agent any
stages {
stage('non-parallel env check') {
steps {
echo 'This stage will be executed first.'
}
}
stage('parallel stage') {
steps {
script {
parallel parallelStagesMap
}
}
}
}
}
The network_reg and revert_to_snapshot_Stage won't run (unless I place them as the last set instead of ["${it}" : install_Stage(it)] , in which case, again, only the one of the parallel stages is run)
I don't mind a different approach to run several map definitions, but others such as: How to define and iterate over map in Jenkinsfile don't allow for a full multi variable map (more than a key+value pair)
Any help would be appreciated, Thanks!
I assume you have a similar issue like I had trying to dynamically build the parallel branches for parallel execution.
Two things were very important:
Make a copy of the loop variable (in you case: it) and use that copy only inside the parallel branch; if you don't all branches (closures) will reference the very same variable which of course will have the same value. That is particular to closures. See also: http://groovy-lang.org/closures.html.
Don't use collectEntries{}. Stick to the java-style loops as groovy loops most of the time do not work properly. Some .each{} constructs may work already but if in doubt switch to the java loops. See also: Impossibility to iterate over a Map using Groovy within Jenkins Pipeline
Following stripped down example works for me. I believe you'll be able to adjust it to your needs.
def products = ["A", "B", "C", "D"]
def parallelStagesMap = [:]
// use java-style loop
for (def product: products) {
// make a copy to ensure that each closure will get it's own variable
def copyOfProduct = product
parallelStagesMap[product] = {echo "install_Stage($copyOfProduct)"}
}
echo parallelStagesMap.toString()
pipeline {
agent any
stages {
stage('parallel stage') {
steps {
script {
parallel parallelStagesMap
}
}
}
}
}
If it still doesn't work: Check whether there's and upgrade your Pipeline: Groovy plugin as they usually fix lot of issues which usually work in groovy but won`t in pipeline.
You may want to check following related question which contains a minimal example as well:
Currying groovy CPS closure for parallel execution
Related
From my experience with Jenkins declarative-syntax pipelines, I'm aware that you can conditionally skip a stage with a when clause. E.g.:
run_one = true
run_two = false
run_three = true
pipeline {
agent any
stages {
stage('one') {
when {
expression { run_one }
}
steps {
echo 'one'
}
}
stage('two') {
when {
expression { run_two }
}
steps {
echo 'two'
}
}
stage('three') {
when {
expression { run_three }
}
steps {
echo 'three'
}
}
}
}
...in the above code block, there are three stages, one, two, and three, each of whose execution is conditional on a boolean variable.
I.e. the paradigm is that there is a fixed superset of known stages, of which individual stages may be conditionally skipped.
Does Jenkins pipeline script support a model where there is no fixed superset of known stages, and stages can be "looked up" for conditional execution?
To phrase it as pseudocode, is something along the lines of the following possible:
my_list = list populated _somehow_, maybe reading a file, maybe Jenkins build params, etc.
pipeline {
agent any
stages {
if (stage(my_list[0]) exists) {
run(stage(my_list[0]))
}
if (stage(my_list[1]) exists) {
run(stage(my_list[1]))
}
if (stage(my_list[2]) exists) {
run(stage(my_list[2]))
}
}
}
?
I think another way to think about what I'm asking is: is there a way to dynamically build a pipeline from some dynamic assembly of stages?
For dynamic stages you could write either a fully scripted pipeline or use a declarative pipeline with a scripted section (e. g. by using the script {…} step or calling your own function). For an overview see Declarative versus Scripted Pipeline syntax and Pipeline syntax overview.
Declarative pipeline is better supported by Blue Ocean so I personally would use that as a starting point. Disadvantage might be that you need to have a fixed root stage, but I usually name that "start" or "init" so it doesn't look too awkward.
In scripted sections you can call stage as a function, so it can be used completely dynamic.
pipeline {
agent any
stages {
stage('start') {
steps {
createDynamicStages()
}
}
}
}
void createDynamicStages() {
// Stage list could be read from a file or whatever
def stageList = ['foo', 'bar']
for( stageName in stageList ) {
stage( stageName ) {
echo "Hello from stage $stageName"
}
}
}
This shows in Blue Ocean like this:
This really helpful answer, got me 95% of the way there. Using this solution, I'm able to start n build stages in parallel. However, the map of parallel stages is essentially hardcoded. I want to be able to create it dynamically. The first step in this process is changing parallelStagesMap from a map, to a function that returns a map.
Unfortunatey, this small change causes my build to fail without any apparent error logs related to syntax.
How can I accomplish this? Am I using malformed Groovy syntax? I'd be grateful for any help.
def jobs = ["JobA", "JobB", "JobC"]
def parallelStagesMap() { // This is now a function that returns a map.
return jobs.collectEntries {
["${it}" : generateStage(it)]
}
}
def generateStage(job) {
return {
stage("stage: ${job}") {
echo "This is ${job}."
sh script: "sleep 15"
}
}
}
pipeline {
agent any
stages {
stage('parallel stage') {
steps {
script {
parallel parallelStagesMap() // I call the function here.
}
}
}
}
}
I got a working solution! It's not perfect, because I would like to extract the jobs.collectEntries part to my own function, but now I can define the contents of my parallel stages inline, instead of at the top of the file!
I tried writing a function matching the same signature as Map.collectEntries: ({ Closure -> Map }), but the Jenkins build fails without any logs once it hits my function. If someone's able to work that out, I'd be grateful.
def jobs = ["JobA", "JobB", "JobC"]
pipeline {
agent any
stages {
stage('parallel stage') {
steps {
script {
parallel jobs.collectEntries { j ->
["${j}" : { job -> return {
stage("stage: ${job}") {
echo "This is ${job}."
sh script: "sleep 15"
}
}}(j)]
}
}
}
}
}
}
The output of this python eval looks like it could be stages in a jenkins pipeline
$ python3 -c 'print("\n".join(["stage({val}) {{ do something with {val} }}".format(val=i) for i in range(3)]))'
stage(0) { do something with 0 }
stage(1) { do something with 1 }
stage(2) { do something with 2 }
Is it possible for jenkins to use output like this to create steps or stages in a pipeline so the running python script is able to update jenkins ? The point of this would be to have Blue Ocean pipeline have a stage dot that was made by an external script running separate jobs.
To elaborate on the example ... if this demo.py script which outputs the uptime in a stage
#!/bin/env python3.6
import subprocess, time
def uptime():
return (subprocess.run('uptime', stdout=subprocess.PIPE, encoding='utf8')).stdout.strip()
for i in range(3):
print("stage({val}) {{\n echo \"{output}\" \n}}".format(val=i, output=uptime()))
time.sleep(1)
where to be setup in a jenkins pipeline
node {
stage("start demo"){
sh "/tmp/demo.py"
}
}
As is this demo just outputs the text and does not create any stages in blue ocean
[Pipeline] sh
+ /tmp/demo.py
stage(0) {
echo "03:17:16 up 182 days, 12:17, 8 users, load average: 0.00, 0.03, 0.05"
}
stage(1) {
echo "03:17:17 up 182 days, 12:17, 8 users, load average: 0.00, 0.03, 0.05"
}
stage(2) {
echo "03:17:18 up 182 days, 12:17, 8 users, load average: 0.00, 0.03, 0.05"
}
Again the point of this would be to have Blue Ocean pipeline have a stage dot with a log
You can evaluate an expression and then call it.
node(''){
Closure x = evaluate("{it -> evaluate(it)}" )
x(" stage('test'){ script { echo 'hi'}}")
}
Since Jenkins converts your Groovy script into Java, compiles it and then executes the result, it would be quite hard to use an external program to generate more Groovy to execute, since that additional groovy code would need to be converted. But the generated code is a result of running, which means that the conversion is already done.
Instead, you may want to programmatically build your stages in Groovy.
some_array = ["/tmp/demo.py", "sleep 10", "uptime"]
def getBuilders()
{
def builders = [:]
some_array.eachWithIndex { it, index ->
// name the stage
def name = 'Stage #' + (index + 1)
builders[name] = {
stage (name) {
def my_label = "jenkins_label" // can choose programmatically if needed
node(my_label) {
try {
doSomething(it)
}
catch (err) { println "Failed to run ${it}"; throw err }
finally { }
}
}
}
};
return builders
}
def doSomething(something) {
sh "${something}"
}
And later in your main pipeline
stage('Do it all') {
steps {
script {
def builders = getBuilders()
parallel builders
}
}
This will run three parallel stages, where one would be running /tmp/demo.py, the second sleep 10, and the third uptime.
I am using a declarative pipeline in a Jenkinsfile but I would like to derive some variables from a parameter.
For example given:
parameters {
choice(name: 'Platform',choices: ['Debian9', 'CentOS7'], description: 'Target OS platform', )
}
I would like to add a block like:
script {
switch(param.Platform) {
case "Centos7":
def DockerFile = 'src/main/docker/Jenkins-Centos.Dockerfile'
def PackageType = 'RPM'
def PackageSuffix = '.rpm'
break
case "Debian9":
default:
def DockerFile = 'src/main/docker/Jenkins-Debian.Dockerfile'
def PackageType = 'DEB'
def PackageSuffix = '.deb'
break
}
}
Such that I can use variables elsewhere in the pipeline. For example:
agent {
dockerfile {
filename "$DockerFile"
}
}
etc..
but script is illegal in the parameter, environment & agent sections.
It can only be used in steps.
I need to use the parameter in the agent block and I want to avoid repeating myself where the variables are used in different steps.
Is there a sane way to achieve this? My preferences in order are:
a declarative pipeline
a scripted pipeline (less good)
via a plugin to the Jenkins UI (least good)
A shared library might be appropriate here regardless of whether it is actually shared.
The intention is to support a multi-configuration project by creating a parameterised build and invoking it for different parameter sets with a red/blue status light for each configuration.
It could be that I have assumed an 'old fashioned' design. In which case an acceptable answer would explain the modern best practice for creating a multi-configuration multi-branch pipeline. Something like: https://support.cloudbees.com/hc/en-us/articles/115000088431-Create-a-Matrix-like-flow-with-Pipeline or Jenkins Pipeline Multiconfiguration Project
See also Multiconfiguration / matrix build pipeline in Jenkins for less specific discussion of best practices.
Never really used the Jenkins declarative pipeline before but I think the way you refer to params is incorrect?
I think it might be: ${params.Platform} or params.Platform instead of param.
So something like the below maybe?
pipeline {
agent any
stages {
stage('example') {
steps {
script {
switch(${params.Platform}) {
...
}
}
}
}
}
}
As I said, never really used it before so not 100%. I was just looking at the syntax used for parameters on the docs: https://jenkins.io/doc/book/pipeline/syntax/#parameters
I think that the key for solving your issue is the declaration of your variables. Do not use def if you want your variable to be accessible from other stages.
Here is an example of a solution for your issue :
pipeline{
agent none
parameters {
choice(name: 'Platform',choices: ['Debian9', 'CentOS7'], description: 'Target OS platform', )
}
stages{
stage('Setting stage'){
agent any
steps {
script {
switch(params.Platform){
case 'CentOS7' :
DockerFile = 'src/main/docker/Jenkins-Centos.Dockerfile'
PackageType = 'RPM'
PackageSuffix = '.rpm'
break
case 'Debian9' :
DockerFile = 'src/main/docker/Jenkins-Debian.Dockerfile'
PackageType = 'DEB'
PackageSuffix = '.deb'
break
}
}
}
}
stage('Echo stage'){
agent {
dockerfile {
filename "$DockerFile"
}
}
steps{
echo PackageType
echo PackageSuffix
}
}
}
}
What is definitely possible on windows:
stage('var from groovy') {
steps {
script {
anvar = "foo"
}
bat "${anyvar}"
}
}
This is an example that I have in production
def dest_app_instance = "${params.Destination}"
switch(dest_app_instance) {
case "CASE1":
dest_server = "server1"
break
case "CASE2":
dest_server = "server2"
break
}
I have a list of long running Gradle tasks on different sub projects in my project. I would like to run these in parallel using Jenkins declarative pipeline.
I was hoping something like this might work:
projects = [":a", ":b", ":c"]
pipeline {
stage("Deploy"){
parallel {
for(project in projects){
stage(project ) {
when {
expression {
someConditionalFunction(project)
}
}
steps {
sh "./gradlew ${project}:someLongrunningGradleTask"
}
}
}
}
}
}
Needless to say that gives a compile error since it was expecting stage instead of for. Any ideas on how to overcome this? Thanks
I was trying to reduce duplicated code in my existing Jenkinsfile using declarative pipeline syntax. Finally I was able to wrap my head around the difference between scripted and declarative syntax.
It is possible to use scripted pipeline syntax in a declarative pipeline by wrapping it with a script {} block.
Check out my example below: you will see that all three parallel stages finish at the same time after waking up from the sleep command.
def jobs = ["JobA", "JobB", "JobC"]
def parallelStagesMap = jobs.collectEntries {
["${it}" : generateStage(it)]
}
def generateStage(job) {
return {
stage("stage: ${job}") {
echo "This is ${job}."
sh script: "sleep 15"
}
}
}
pipeline {
agent any
stages {
stage('non-parallel stage') {
steps {
echo 'This stage will be executed first.'
}
}
stage('parallel stage') {
steps {
script {
parallel parallelStagesMap
}
}
}
}
}
Parallel wants a map structure. You are doing this a little inside-out. Build your map and then just pass it to parallel, rather than trying to iterate inside parallel.
Option 2 on this page shows you a way to do something similar to what you are trying.
At this link you can find a complex way I did this similar to a matrix/multi-config job: