试图让这条管道工作.. 我需要在groovy中准备一些变量(列表或字符串),并在bash中迭代它。据我所知,groovy脚本在jenkins master上运行,但我需要将一些文件下载到构建工作区,这就是为什么我尝试在SH步骤中下载它们的原因。
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
"""
}
}
}
}
运行时出现此错误..
[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)
看起来我错过了一些明显的东西......
答案 0 :(得分:3)
使用反斜杠转义$变量的shell变量,这应该会有所帮助:
for dash in $dash_names_string;
do
echo \$dash
done
答案 1 :(得分:0)
问题出在第三行:
for dash in $dash_names_string;
do
echo $dash
done
它试图在groovy-land中找到一个$dash
属性并且找不到。我实际上无法想到如何使这个工作vi内联sh
步骤(可能没有足够的睡眠),但如果你将json响应的相关内容保存到文件然后用shell替换这四行读取文件并从Jenkins文件中调用它的脚本,如sh './hotScript.sh'
,它不会尝试将该美元值评估为groovy,并且至少应该以不同的方式失败。 :)