hunterex
hunterex

Reputation: 595

How to read values from a property in JSON and pass to matrix axis values in Jenkinsfile?

I have microservices.json file which looks like this:

[
    {
        "build": "cat",
        "microservices": [
            "a",
            "b"
        ]
    },
    {
        "build": "dog",
        "microservices": [
            "a",
            "b"
        ]
    }
]

I have a jenkinsfile, which has a matrix structure like below:

pipeline {
    agent { label 'Ubuntu' }
    environment{
        def microserviceBuild = readJSON file: 'microservices.json'
        def builds = "${microserviceBuild[0].build}"  // how to get each build property value ?????
        //def builds = new JsonSlurper().parseText( microserviceBuild )
    }
    stages {
        stage('Build') {
            matrix {
                axes {
                    axis {
                        name 'BUILD'
                        values 'cat', 'dog'  // <--------------- how to pass builds list here????
                    }
                }
                stages {
                    stage('Check values'){
                        steps {
                            pwsh """
                            Write-Host ${BUILD}
                            """
                        }
                    }
                }
            }
        }

    }
}

Running def microserviceBuild = readJSON file: 'microservices.json' alone in Jenkins throws a error message below:

hudson.remoting.ProxyException: net.sf.json.JSONException: Invalid JSON String at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:143) at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:103) at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:84) at org.jenkinsci.plugins.pipeline.utility.steps.json.ReadJSONStepExecution.doRun(ReadJSONStepExecution.java:77) at org.jenkinsci.plugins.pipeline.utility.steps.AbstractFileOrTextStepExecution.run(AbstractFileOrTextStepExecution.java:32) at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:834) Finished: FAILURE

  1. How do I get each build values as list using readJSON?
  2. How to pass the build values to the matrix axis values? Is it possible?

Thanks

Upvotes: 0

Views: 1012

Answers (2)

Altaf
Altaf

Reputation: 3076

Dynamic axis values are not supported in Declarative Pipeline as of now . There is a jenkins issue created for it. It accepts only string literals.
https://issues.jenkins.io/browse/JENKINS-62127

Upvotes: 0

Dmitriy Tarasevich
Dmitriy Tarasevich

Reputation: 1242

As I know it is not possible to use variables in matrix.

Upvotes: 0

Related Questions