Groovy版本 $ groovy --version Groovy版本:2.4.7 JVM:1.8.0_91供应商:Oracle Corporation操作系统:Mac OS X
以下代码适用于我的Mac
import groovy.json.JsonSlurper
def StackStatusJson="aws cloudformation describe-stacks --stack-name XXXXXX".execute().text
println(StackStatusJson)
try{
def jsonSlurper = new JsonSlurper()
def object=jsonSlurper.parseText(StackStatusJson)
println("StackStatus->"+object.Stacks.StackStatus)
String v=object.Stacks.StackStatus
if(v.equals("[CREATE_COMPLETE]"))
{
println("Calling Deploy Artifacts")
deployArtifacts()
}
}catch(err)
{
println(err);
}
def deployArtifacts()
{
println "I am called"
}
但是在Jenkins v 2.15管道上,下面的字符串比较失败了。我不明白为什么它没有使条件成真
if(v.equals("[CREATE_COMPLETE]"))
{
println("Calling Deploy Artifacts")
deployArtifacts()
}
}catch(err)
{
println(err);
}
用于测试的示例JSON低于Stubbed值
{
"Stacks": [
{
"StackId": "XXXXX",
"Description": "XXXXXX Environment",
"Parameters": [
{
"ParameterValue": "XXXXXXX",
"ParameterKey": "proxy"
},
{
"ParameterValue": "XXXXXX",
"ParameterKey": "amiid"
},
{
"ParameterValue": "XXXXXX",
"ParameterKey": "TopicARN"
},
{
"ParameterValue": "XXXXXX",
"ParameterKey": "yumproxy"
},
{
"ParameterValue": "m3.large",
"ParameterKey": "InstanceType"
}
],
"Tags": [],
"CreationTime": "2016-08-28T06:34:12.324Z",
"StackName": "XXXXXX",
"NotificationARNs": [],
"StackStatus": "CREATE_COMPLETE",
"DisableRollback": false
}
]
}
答案 0 :(得分:1)
代码行
String v = object.Stacks[0].StackStatus
应该阅读
object.Stacks
因为array(5) {
["posts_per_page"]=>
int(6)
["orderby"]=>
array(2) {
["uss_product_price"]=>
string(4) "DESC"
["uss_product_weight"]=>
string(4) "DESC"
}
["meta_query"]=>
array(3) {
["relation"]=>
string(3) "AND"
["sortprimary_clause"]=>
array(3) {
["key"]=>
string(17) "uss_product_price"
["compare"]=>
string(6) "EXISTS"
["type"]=>
string(7) "numeric"
}
["sortsecondary_clause"]=>
array(2) {
["key"]=>
string(18) "uss_product_weight"
["compare"]=>
string(6) "EXISTS"
}
}
["paged"]=>
string(1) "1"
["post_type"]=>
string(7) "product"
}
是一个数组。