如何从 jenkins 管道中的 groovy 输出中获取值

How to get the values from a groovy output in jenkins pipeline

jenkins groovy 脚本的一个输出是:

{
  "Code" : "Success",
  "LastUpdated" : "2021-08-31T18:12:23Z",
  "Type" : "sample",
  "AccessKeyId" : "reallysample",
  "SecretAccessKey" : "valuesomerandom",
  "Token" : "hugewrong+data=",
  "Expiration" : "2021-09-01T00:48:09Z"
}

如何从中提取 - AccessKeyId 和 SecretAccessKey 的值。请帮忙,目前我被屏蔽了

您可以使用 readJSON 关键字,它是 Pipeline Utility Steps Jenkins 插件的一部分。

Reads a file in the current working directory or a String as a plain text JSON file. The returned object is a normal Map with String keys or a List of primitives or Map.

因此您可以使用 readJSON 将您的 JSON 输出转换为字典,以便轻松访问所有变量。
这是一个示例(假设您的输出在 output 变量内):

def props = readJSON text: output // assuming output contains your JSON text

println "AccessKeyId is: ${props.AccessKeyId}"  // you can also use props['AccessKeyId']
println "SecretAccessKey is: ${props.SecretAccessKey}"