通過 Drone Rest API 獲取構建記錄日志



Drone是一款CICD工具,提供rest API,簡單介紹下如何使用API 獲取構建日志。

獲取token

登錄進入drone,點頭像,在菜單里選擇token

enter description here

復制token即可

API 介紹

Drone的api分為幾大類

  • Builds 構建
  • Cron 定時任務
  • Repos 倉庫
  • Secrets
  • User 用戶
  • Users

調用舉例:

enter description here

Build API

構建列表(Build List)

獲取倉庫的最新構建:

GET /api/repos/{owner}/{repo}/builds

curl -i http://drone.YOUR_HOST.cn/api/repos/jqpeng/springboot-rest-demo/builds -H "Authorization: Bearer TOKEN"

響應正文示例:

[
  {
      "id": 100207,
      "repo_id": 296163,
      "number": 42,
      "status": "success",
      "event": "pull_request",
      "action": "sync",
      "link": "https://github.com/octoat/hello-world/compare/e3320539a4c0...9fc1ad6ebf12",
      "message": "updated README",
      "before": "e3320539a4c03ccfda992641646deb67d8bf98f3",
      "after": "9fc1ad6ebf12462f3f9773003e26b4c6f54a772e",
      "ref": "refs/heads/master",
      "source_repo": "spaceghost/hello-world",
      "source": "develop",
      "target": "master",
      "author_login": "octocat",
      "author_name": "The Octocat",
      "author_email": "octocat@github.com",
      "author_avatar": "http://www.gravatar.com/avatar/7194e8d48fa1d2b689f99443b767316c",
      "sender": "bradrydzewski",
      "started": 1564085874,
      "finished": 1564086343,
      "created": 1564085874,
      "updated": 1564085874,
      "version": 3
  }
]

構建詳情

通過該接口獲取構建詳情,返回構建狀態等信息,{build} 為上面列表里的number,既構建序號。

GET /api/repos/{owner}/{repo}/builds/{build}

Example Response Body:

{
    "id": 39862,
    "number": 20,
    "parent": 0,
    "event": "push",
    "status": "success",
    "error": "",
    "enqueued_at": 1576636849,
    "created_at": 1576636849,
    "started_at": 1576636850,
    "finished_at": 1576639053,
    "deploy_to": "",
    "commit": "7729006bfe11933da6c564101acaf8c7f78c5f62",
    "branch": "master",
    "ref": "refs/heads/master",
    "refspec": "",
    "remote": "",
    "title": "",
    "message": "通過update更新\n",
    "timestamp": 0,
    "sender": "",
    "author": "jqpeng",
    "author_avatar": "https://www.gravatar.com/avatar/4ab53b564545f18efc4079c30a2d35cf.jpg?s=128",
    "author_email": "jqpeng@iflytek.com",
    "link_url": "",
    "signed": false,
    "verified": true,
    "reviewed_by": "",
    "reviewed_at": 0,
    "procs": [
        {
            "id": 247912,
            "build_id": 39862,
            "pid": 1,
            "ppid": 0,
            "pgid": 1,
            "name": "",
            "state": "success",
            "exit_code": 0,
            "start_time": 1576636850,
            "end_time": 1576639053,
            "machine": "21e73ce43038",
            "children": [
                {
                    "id": 247913,
                    "build_id": 39862,
                    "pid": 2,
                    "ppid": 1,
                    "pgid": 2,
                    "name": "clone",
                    "state": "success",
                    "exit_code": 0,
                    "start_time": 1576636853,
                    "end_time": 1576636933,
                    "machine": "21e73ce43038"
},
                {
                    "id": 247914,
                    "build_id": 39862,
                    "pid": 3,
                    "ppid": 1,
                    "pgid": 3,
                    "name": "build",
                    "state": "success",
                    "exit_code": 0,
                    "start_time": 1576636933,
                    "end_time": 1576636998,
                    "machine": "21e73ce43038"
}
]
}
]
}

procs 是構建的步驟,記住pid,獲取構建日志有用

構建日志

獲取構建日志,需要傳入{log} 和 {pid}, log是上面的{build},{pid}是上一步返回的pid

GET /api/repos/{owner}/{repo}/logs/{log}/{pid}

響應正文示例:

[
  {
    "proc": "clone",
    "pos": 0,
    "out": "+ git init\n"
  },
  {
    "proc": "clone",
    "pos": 1,
    "out": "Initialized empty Git repository in /drone/src/github.com/octocat/hello-world/.git/\n"
  },
  {
    "proc": "clone",
    "pos": 2,
    "out": "+ git remote add origin https://github.com/octocat/hello-world.git\n"
  },
  {
    "proc": "clone",
    "pos": 3,
    "out": "+ git fetch --no-tags origin +refs/heads/master:\n"
  },
  {
    "proc": "clone",
    "pos": 4,
    "out": "From https://github.com/octocat/hello-world\n"
  },
  {
    "proc": "clone",
    "pos": 5,
    "out": " * branch            master     -> FETCH_HEAD\n"
  },
  {
    "proc": "clone",
    "pos": 6,
    "out": " * [new branch]      master     -> origin/master\n"
  },
  {
    "proc": "clone",
    "pos": 7,
    "out": "+ git reset --hard -q 62126a02ffea3dabd7789e5c5407553490973665\n"
  },
  {
    "proc": "clone",
    "pos": 8,
    "out": "+ git submodule update --init --recursive\n"
  }
]

作者:Jadepeng
出處:jqpeng的技術記事本--http://www.cnblogs.com/xiaoqi
您的支持是對博主最大的鼓勵,感謝您的認真閱讀。
本文版權歸作者所有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM