使用groovy解析Jenkin的Shell脚本中的JSON对象 [英] Using groovy to parse JSON object in shell scripts for Jenkin

查看:121
本文介绍了使用groovy解析Jenkin的Shell脚本中的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个如下的JSON:

Say I have a JSON as following:

{"id":"1.0.0-6",
"version":"1.0.0",
"build":6,
"tag":"android-v1.0.0-6",
"commitHash":"5a78c4665xxxxxxxxxxe1b62c682f84",
"dateCreated":"2020-03-02T08:11:29.912Z"}

我想从Jenkins Groovy文件中从中获取版本ID,并将版本ID传递给名为XRAY的JIRA插件,以便它将在JIRA中创建一个构建版本作为Label.

I want to take out the version id from it in Jenkins Groovy file and pass the version id to a JIRA Plugin called XRAY so that it will create a build version as Label in JIRA.

stage('Get App version') {
      steps {
          container('devicefarm') {
               steps {
                   sh "LATEST_VERSION=$(curl ${APP_ARTIFACTORY_URL}/${XRAY_PLATFORM}/builds/latest.json | sed \"s/.*$VERSION_KEY\":\"\\([^\"]*\\).*/'\\1'/\")"
               }
           }
      }
}
environment {
        AWS_DEFAULT_REGION = 'uk-xxx'
        XRAY_ENVIRONMENT = 'e2e'
        VERSION_KEY = 'id'
        XRAY_PLATFORM = 'Android'
        APP_ARTIFACTORY_URL = 'https://artifactory.example.com/mobile'
        LATEST_VERSION = ''
}

我有两个问题,是否会按预期将curl命令的结果分配给同一Jenkins文件"LATEST_VERSION"中定义的变量?

I have two questions, will the result of curl command been assigned to variable defined in the same Jenkins file called 'LATEST_VERSION' as expected?

我可能可以通过在Jenkins上运行管道来对其进行测试,但是我遇到了另一个使我无法执行此操作的问题,它抱怨说期望标识符或代码块".

I probably can test it by running the pipe line on Jenkins but I got another issue that prevent me from doing this, it complains that 'Identifier or code block expected'.

虽然在sh文件中以相同的方式运行,但没有此问题,但已按预期从JSON中检索了版本ID.

While running same in a sh file, it doesn't have this issue, version id was retrieved from JSON as expected.

推荐答案

有一些步骤可以实现这一目标.首先,我们需要修复您的Shell方法执行.我们将其转换为有效的shell执行,该执行将返回执行中的标准并将其分配给变量:

There are a few steps to achieving this. First, we need to fix your shell method execution. We will convert it into a valid shell execution that returns the standard out of the execution and assigns it to a variable:

build_json = sh(label: 'Retrieve Build Info', script: "curl ${APP_ARTIFACTORY_URL}/${XRAY_PLATFORM}/builds/latest.json", returnStdout: true)

这是有效的shell方法执行,还将执行标准返回并将其分配给变量.请参见文档有关更多信息.

This is a valid shell method execution that will also return the standard out and assign it to a variable. See the documentation for more information.

接下来,我们需要解析结果JSON,并将该返回值分配给变量:

Next, we need to parse the resulting JSON, and assign that return value to a variable:

build_map = readJSON(text: build_json)

请参见文档以获取更多信息.

See the documentation for more information.

最后,我们现在有了一个Map,可以在其中访问latest_version键的值并将其分配给变量.我们可以使用以下语法进行访问:

Finally, we now have a Map where we can access the value for the latest_version key and assign it to a variable. We can access with this syntax:

latest_version = build_map['version']

或者这个:

latest_version = build_map.version

请注意,尽管这确实回答了您的问题,但是您无法在管道执行期间将动态值传递给environment块(您的问题暗示您希望稍后执行).因此,您需要为此尝试其他方法,并且可能需要询问有关此问题的后续问题.

Note that while this does answer your question, you cannot pass dynamic values to an environment block during the pipeline execution (which your question implies you want to perform later). So, you are going to need try a different route for that, and may need to ask a followup question about it.

这篇关于使用groovy解析Jenkin的Shell脚本中的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆