Jenkins pipeline shell step

Viewed 5924

Trying to get this pipeline working.. I need to prepare some variables (list or string) in groovy, and iterate over it in bash. As I understand, groovy scripts run on jenkins master, but I need to download some files into build workspace, that's why I try to download them in SH step.

import groovy.json.JsonSlurper
import hudson.FilePath
pipeline {
    agent { label 'xxx' }
    parameters {
 ...
    }
    stages {
        stage ('Get rendered images') {
            steps {
                script {
                    //select grafana API url based on environment
                    if ( params.grafana_env == "111" ) {
                        grafana_url = "http://xxx:3001"
                    } else if ( params.grafana_env == "222" ) {
                        grafana_url = "http://yyy:3001"
                    }

                    //get available grafana dashboards
                    def grafana_url = "${grafana_url}/api/search"
                    URL apiUrl = grafana_url.toURL()
                    List json = new JsonSlurper().parse(apiUrl.newReader())
                    def workspace = pwd()
                    List dash_names = []
                    // save png for each available dashboard
                    for ( dash in json ) { 
                        def dash_name = dash['uri'].split('/')
                        dash_names.add(dash_name[1])
                    }
                    dash_names_string = dash_names.join(" ")
                }
                sh "echo $dash_names_string"
                sh """
                    for dash in $dash_names_string;
                    do
                        echo $dash
                    done
                    """
            }
        }
    }
}

I get this error when run..

[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: dash for class: WorkflowScript
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
    at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:33)
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
    at WorkflowScript.run(WorkflowScript:42)

Looks like I'm missing something obvious...

2 Answers
Related