从 Jenkins REST 查询检索参数

2024-01-01


以下 REST 查询将返回上次成功构建作业的参数:https://localhost/job/test1/lastSuccessfulBuild/api/json https://localhost/job/test1/lastSuccessfulBuild/api/json
我有兴趣检索此构建的参数之一,BUILD_VERSION:

{

    "_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
    "actions": [
        {
            "_class": "hudson.model.CauseAction",
            "causes": [
                {
                    "_class": "hudson.model.Cause$UpstreamCause",
                    "shortDescription": "Started by upstream project \"continuous-testing-pipeline-for-nightly\" build number 114",
                    "upstreamBuild": 114,
                    "upstreamProject": "continuous-testing-pipeline-for-nightly",
                    "upstreamUrl": "job/continuous-testing-pipeline-for-nightly/"
                }
            ]
        },
        { },
        {
            "_class": "hudson.model.ParametersAction",
            "parameters": [

                {
                    "_class": "hudson.model.StringParameterValue",
                    "name": "BUILD_VERSION",
                    "value": "1.1.15"

有没有办法直接使用 REST Api 检索 BUILD_VERSION (1.1.15) 还是我必须手动解析 json 字符串? 谢谢


简短回答:不。

以编程方式访问通过 JSON API 公开的任何属性的最简单方法是从 Jenkins 支持的 JSON API 之一获取 JSON(在您的情况下:https://localhost/job/<jobname>/lastSuccessfulBuild/api/json)

  1. 将生成的 JSON 复制到http://json2csharp.com http://json2csharp.com
  2. 生成相应的C#代码。不要忘记为顶级课程创建一个有意义的名称。
  3. 使用 C# 以编程方式调用 Rest API休息锐利 https://www.nuget.org/packages/RestSharp/.
  4. 将 json 反序列化为您在上面 2 中定义的 C# 类。

Wammo,您可以访问整个对象树及其所有值。

我使用这种方法编写了一个名为“BuildDashboard”的 MVC5 ASP.NET 站点,以提供开发团队可能需要的所有信息并回答他们提出的每个问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 Jenkins REST 查询检索参数 的相关文章

随机推荐