Search code examples
pythonjenkinsjenkins-pipeline

Return a list of values from Python Script to Jenkins Pipeline


I have a jenkins pipeline which contains the following 3 stages(just description):

Stage 1: Calling a python script, that should return a list of version numbers for which a new release should be built

Stage 2: Call Jenkins-Jobs that do the built for every version number of Stage 1 in parallel.

Stage 3: After all built jobs have finished do other stuff with the built releases.

The issue I got is, I don't know how to process the output of the python script. How can I use a list, that is returned from a python function for the following Stages?

I call the python script via a bat command. I saw, there is the possibility to redirect the output of a batch command via returnStdout flag, but this only passes the output of the python script.

One possiblity I can think of is to store the list in a json file in the workspace and then read it back in with readJson in the pipeline. But maybe there is a more elegant solution to this.


Solution

  • Since you're using the Pipeline DSL, you can use Groovy to process the result of the call to bat

    pipeline {
        agent any
        steps {
            step('Get Build Numbers') {
                script {
                    def version_numbers = bat(script: 'python get_version_numbers.py', returnStdout: true)
                    def versions_as_array = version_numbers.split('\n')
                }
            }
        }
    }
    

    From there, it's a matter of generating the build steps and wrapping them in a parallel block. For that, take a look at this answer: Ideas to implement dynamic parallel build using jenkins pipeline plugin