Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

持续集成:使用 Jenkins REST API 进行远程构建与状态查询 #284

Open
yanyue404 opened this issue Oct 12, 2024 · 0 comments
Open

Comments

@yanyue404
Copy link
Owner

查询 Jenkins 中指定任务 (job) 的最新任务编号

使用以下命令获取指定 Jenkins 任务的最新构建编号:

curl -X GET "http://myJenkins/job/testjob/api/json/lastBuild/buildNumber"

输出示例:

1159

说明:其中 testjob 为任务名。


查询 Jenkins 中指定任务 (job) 的配置信息

要获取指定 Jenkins 任务的配置信息,可以使用以下命令:

curl -X GET "http://myJenkins/job/testjob/api/json"

输出示例:

{
  "_class": "hudson.model.FreeStyleProject",
  "actions": [
    {
      "_class": "hudson.model.ParametersDefinitionProperty",
      "parameterDefinitions": []
    }
  ],
  "description": "",
  "displayName": "cms-autoDeployFE_VisualisationTest_tencent",
  "fullDisplayName": "cms-autoDeployFE_VisualisationTest_tencent",
  "url": "http://myJenkins/job/testjob/",
  "buildable": true,
  "builds": [
    {
      "_class": "hudson.model.FreeStyleBuild",
      "number": 1158,
      "url": "http://myJenkins/job/testjob/1158/"
    },
    {
      "_class": "hudson.model.FreeStyleBuild",
      "number": 1157,
      "url": "http://myJenkins/job/testjob/1157/"
    },
    {
      "_class": "hudson.model.FreeStyleBuild",
      "number": 1156,
      "url": "http://myJenkins/job/testjob/1156/"
    }
  ],
  "color": "blue",
  "healthReport": [
    {
      "description": "构建稳定性: 最近没有失败的构建。",
      "iconClassName": "icon-health-80plus",
      "score": 100
    }
  ],
  "lastBuild": {
    "_class": "hudson.model.FreeStyleBuild",
    "number": 1158,
    "url": "http://myJenkins/job/testjob/1158/"
  },
  ...
}

查询 Jenkins 中指定任务 (job) 的前次构建 (build) 的任务状态信息

要获取指定任务的上一次构建状态,可以使用以下命令:

curl -X GET "http://myJenkins/job/testjob/lastBuild/api/json"

输出示例:

{
  "_class": "hudson.model.FreeStyleBuild",
  "actions": [
    {
      "_class": "hudson.model.ParametersAction",
      "parameters": [
        {
          "_class": "hudson.model.TextParameterValue",
          "name": "uniqueId",
          "value": "1844283343493722112"
        },
        {
          "_class": "hudson.model.TextParameterValue",
          "name": "gitBranch",
          "value": "dev"
        },
        ...
      ]
    }
  ],
  "building": false,
  "displayName": "#1158",
  "duration": 1211167,
  "result": "SUCCESS",
  "timestamp": 1728546389281,
  "url": "http://myJenkins/job/testjob/1158/"
}

查询 Jenkins 中指定任务 (job) 的指定任务编号 (buildNum) 的任务状态信息

获取特定构建编号的任务状态信息的命令如下:

curl -X GET "http://myJenkins/job/testjob/1157/api/json"

输出示例:

{
  "_class": "hudson.model.FreeStyleBuild",
  ...
  "displayName": "#1157",
  "result": "SUCCESS",
  "timestamp": 1728541799259,
  "url": "http://myJenkins/job/testjob/1157/"
}

启动 Jenkins 中指定任务 (job) (无构建参数)

可以使用以下命令启动指定的 Jenkins 任务:

curl -X POST "http://myJenkins/job/testjob/build"

启动 Jenkins 中指定任务 (job) 按任务参数执行构建任务

如果需要通过参数启动构建任务,可以使用以下命令:

curl -X POST "http://myJenkins/job/testjob/buildWithParameters?uniqueId=xxx&gitBranch=dev&pageUrl=%2Fprd%2Fheizi&pageId=201&pageVersion=298&env=dev"

安全性: Jenkins 远程触发构建配置触发密钥

打开任务配置并找到构建触发器模块,勾选触发远程构建并配置一个秘钥,这个秘钥相当于密码,密码错误的话不会触发构建.

进行不登录触发构建:

首先先安装一个插件 Build Authorization Token Root Plugin,点击右上角的登录名->设置

输入生成 token 的字符串,并生成一串 token 秘钥,拷贝生成的 token 秘钥,然后回到项目的构建触发器模块,将生成的 token 秘钥填入身份验证令牌中即可.

参考链接

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant