Assigning variables in a parallel step using declarative pipeline steps in Jenkins

I am trying to convert a script pipeline into a declarative pipeline and work on some of the main (I think) issues.

stage ('Deploy to Docker') { steps { parallel ( "instance1" : { environment { containerId = sh(script: "docker ps --quiet --filter name=${fullDockerImageName}", returnStdout: true).trim() } steps { .... } } ) } } 

This throws the following exception:

 WorkflowScript: 197: Expected a step @ line 197, column 29. containerId = sh(script: "docker ps --quiet --filter name=${fullDockerImageName} ", returnStdout: true).trim() 

Since I am not allowed to assign variables inside the steps {} block, I moved it to the environment {} block, but that also doesn't seem like a trick.

Any idea on how to set these variables?

+2
jenkins jenkins-pipeline
source share
1 answer

See my answer here , you can execute an arbitrary script pipeline inside the script step. So let's take your example:

 stage ('Deploy to Docker') { steps { parallel ( "instance1" : { steps { script { env['containerId'] = sh(script: "docker ps --quiet --filter name=${fullDockerImageName}", returnStdout: true).trim() } .... } } ) } } 
+3
source share

All Articles