Jenkins-cli 自動(dòng)化構(gòu)建工具(1)

Jenkins
Build Status
Build Status

這是一個(gè) Node Jenkins 客戶端.

API 接口文檔列表

<a name="promise"></a>

Promise

在 Node 高版本中可以通過設(shè)置 promisifytrue 打開支持,更老版本可以通過包裝器(例如 bluebird.fromCallback)完成

<a name="common-options"></a>

通用選項(xiàng)

這些選項(xiàng)會(huì)在每次調(diào)用時(shí)生效,但并非所有 Jenkins 服務(wù)端都支持

  • depth (Number, default: 0): 返回?cái)?shù)據(jù)量控制 (參考 設(shè)置 depth 參數(shù))
  • tree (String, optional): 路徑描述符 (參考 Jenkins 官方 API )

<a name="init"></a>

jenkins([options])

初始化一個(gè) Jenkins 客戶端

選項(xiàng)

  • baseUrl (String): Jenkins 服務(wù)器 URL
  • crumbIssuer (Boolean, default: false): 開啟 CSRF 保護(hù)
  • headers (Object, optional): 每次請(qǐng)求頭部的設(shè)定
  • promisify (Boolean|Function, optional): 將回調(diào)轉(zhuǎn)為 Promise

用法

var jenkins = require('jenkins')({ baseUrl: 'http://user:pass@localhost:8080', crumbIssuer: true });

<a name="info"></a>

jenkins.info(callback)

獲取 Jenkins 服務(wù)端信息

用法

jenkins.info(function(err, data) {
  if (err) throw err;

  console.log('info', data);
});

樣例輸出

{
  "assignedLabels": [
    {}
  ],
  "description": null,
  "jobs": [
    {
      "color": "blue",
      "name": "example",
      "url": "http://localhost:8080/job/example/"
    }
  ],
  "mode": "NORMAL",
  "nodeDescription": "the master Jenkins node",
  "nodeName": "",
  "numExecutors": 2,
  "overallLoad": {},
  "primaryView": {
    "name": "All",
    "url": "http://localhost:8080/"
  },
  "quietingDown": false,
  "slaveAgentPort": 12345,
  "unlabeledLoad": {},
  "useCrumbs": false,
  "useSecurity": false,
  "views": [
    {
      "name": "All",
      "url": "http://localhost:8080/"
    }
  ]
}

<a name="build-get"></a>

jenkins.build.get(options, callback)

獲取構(gòu)建信息

傳參

  • name (String): 任務(wù)名稱
  • number (Integer): 構(gòu)建編號(hào)

用法

jenkins.build.get('example', 1, function(err, data) {
  if (err) throw err;

  console.log('build', data);
});

樣例輸出

{
  "actions": [],
  "buildable": true,
  "builds": [
    {
      "number": 1,
      "url": "http://localhost:8080/job/example/1/"
    }
  ],
  "color": "blue",
  "concurrentBuild": false,
  "description": "",
  "displayName": "example",
  "displayNameOrNull": null,
  "downstreamProjects": [],
  "firstBuild": {
    "number": 1,
    "url": "http://localhost:8080/job/example/1/"
  },
  "healthReport": [
    {
      "description": "Build stability: No recent builds failed.",
      "iconUrl": "health-80plus.png",
      "score": 100
    }
  ],
  "inQueue": false,
  "keepDependencies": false,
  "lastBuild": {
    "number": 1,
    "url": "http://localhost:8080/job/example/1/"
  },
  "lastCompletedBuild": {
    "number": 1,
    "url": "http://localhost:8080/job/example/1/"
  },
  "lastFailedBuild": null,
  "lastStableBuild": {
    "number": 1,
    "url": "http://localhost:8080/job/example/1/"
  },
  "lastSuccessfulBuild": {
    "number": 1,
    "url": "http://localhost:8080/job/example/1/"
  },
  "lastUnstableBuild": null,
  "lastUnsuccessfulBuild": null,
  "name": "example",
  "nextBuildNumber": 2,
  "property": [],
  "queueItem": null,
  "scm": {},
  "upstreamProjects": [],
  "url": "http://localhost:8080/job/example/"
}

<a name="build-log"></a>

jenkins.build.log(options, callback)

獲取構(gòu)建日志

傳參

  • name (String): 任務(wù)名稱
  • number (Integer): 構(gòu)建編號(hào)
  • start (Integer, 可選): start offset
  • type (String, enum: text, html, 默認(rèn): text): 指定輸出類型
  • meta (Boolean, default: false): return object with text (log data), more (boolean if there is more log data), and size (used with start to offset on subsequent calls)

用法

jenkins.build.log('example', 1, function(err, data) {
  if (err) throw err;

  console.log('log', data);
});

<a name="build-log-stream"></a>

jenkins.build.logStream(options, callback)

獲取構(gòu)建日志流

傳參

  • name (String): 任務(wù)名稱
  • number (Integer): 構(gòu)建編號(hào)
  • type (String, enum: text, html, default: text): 指定輸出類型
  • delay (Integer, default: 1000): poll interval in milliseconds

用法

var log = jenkins.build.logStream('example', 1);

log.on('data', function(text) {
  process.stdout.write(text);
});

log.on('error', function(err) {
  console.log('error', err);
});

log.on('end', function() {
  console.log('end');
});

<a name="build-stop"></a>

jenkins.build.stop(options, callback)

中斷構(gòu)建

傳參

  • name (String): 任務(wù)名稱
  • number (Integer): 構(gòu)建編號(hào)

用法

jenkins.build.stop('example', 1, function(err) {
  if (err) throw err;
});

<a name="job-build"></a>

jenkins.job.build(options, callback)

開始構(gòu)建

傳參

  • name (String): 任務(wù)名稱
  • parameters (Object, 可選): 構(gòu)建參數(shù)
  • token (String, 可選): 鑒權(quán)標(biāo)識(shí)

用法

jenkins.job.build('example', function(err, data) {
  if (err) throw err;

  console.log('queue item number', data);
});
jenkins.job.build({ name: 'example', parameters: { name: 'value' } }, function(err) {
  if (err) throw err;
});

<a name="job-config-get"></a>

jenkins.job.config(options, callback)

獲取任務(wù)配置(XML形式)

傳參

  • name (String): 任務(wù)名稱

用法

jenkins.job.config('example', function(err, data) {
  if (err) throw err;

  console.log('xml', data);
});

<a name="job-config-set"></a>

jenkins.job.config(options, callback)

修改任務(wù)配置(XML形式)

傳參

  • name (String): 任務(wù)名稱
  • xml (String): 配置文件

用法

jenkins.job.config('example', xml, function(err) {
  if (err) throw err;
});

<a name="job-config-copy"></a>

jenkins.job.copy(options, callback)

從已有的項(xiàng)目中克隆新的任務(wù)

傳參

  • name (String): 新任務(wù)名稱
  • from (String): 源任務(wù)名稱

用法

jenkins.job.copy('fromJob', 'example', function(err) {
  if (err) throw err;
});

<a name="job-create"></a>

jenkins.job.create(options, callback)

從配置文件創(chuàng)建新的任務(wù)

傳參

  • name (String): 任務(wù)名稱
  • xml (String): 配置文件

用法

jenkins.job.create('example', xml, function(err) {
  if (err) throw err;
});

<a name="job-destroy"></a>

jenkins.job.destroy(options, callback)

刪除任務(wù)

傳參

  • name (String): 任務(wù)名稱

用法

jenkins.job.destroy('example', function(err) {
  if (err) throw err;
});

<a name="job-disable"></a>

jenkins.job.disable(options, callback)

停用一個(gè)任務(wù)

傳參

  • name (String): 任務(wù)名稱

用法

jenkins.job.disable('example', function(err) {
  if (err) throw err;
});

<a name="job-enable"></a>

jenkins.job.enable(options, callback)

啟動(dòng)一個(gè)任務(wù)

傳參

  • name (String): 任務(wù)名稱

用法

jenkins.job.enable('example', function(err) {
  if (err) throw err;
});

<a name="job-exists"></a>

jenkins.job.exists(options, callback)

監(jiān)測(cè)任務(wù)是否存在

傳參

  • name (String): 任務(wù)名稱

用法

jenkins.job.exists('example', function(err, exists) {
  if (err) throw err;

  console.log('exists', exists);
});

<a name="job-get"></a>

jenkins.job.get(options, callback)

獲取任務(wù)信息,得到 json 格式配置文件

傳參

  • name (String): 任務(wù)名稱

用法

jenkins.job.get('example', function(err, data) {
  if (err) throw err;

  console.log('job', data);
});

樣例輸出

{
  "actions": [],
  "buildable": true,
  "builds": [
    {
      "number": 1,
      "url": "http://localhost:8080/job/example/1/"
    }
  ],
  "color": "blue",
  "concurrentBuild": false,
  "description": "",
  "displayName": "example",
  "displayNameOrNull": null,
  "downstreamProjects": [],
  "firstBuild": {
    "number": 1,
    "url": "http://localhost:8080/job/example/1/"
  },
  "healthReport": [
    {
      "description": "Build stability: No recent builds failed.",
      "iconUrl": "health-80plus.png",
      "score": 100
    }
  ],
  "inQueue": false,
  "keepDependencies": false,
  "lastBuild": {
    "number": 1,
    "url": "http://localhost:8080/job/example/1/"
  },
  "lastCompletedBuild": {
    "number": 1,
    "url": "http://localhost:8080/job/example/1/"
  },
  "lastFailedBuild": null,
  "lastStableBuild": {
    "number": 1,
    "url": "http://localhost:8080/job/example/1/"
  },
  "lastSuccessfulBuild": {
    "number": 1,
    "url": "http://localhost:8080/job/example/1/"
  },
  "lastUnstableBuild": null,
  "lastUnsuccessfulBuild": null,
  "name": "example",
  "nextBuildNumber": 2,
  "property": [],
  "queueItem": null,
  "scm": {},
  "upstreamProjects": [],
  "url": "http://localhost:8080/job/example/"
}

<a name="job-list"></a>

jenkins.job.list(callback)

列出所有的任務(wù)

用法

jenkins.job.list(function(err, data) {
  if (err) throw err;

  console.log('jobs', data);
});

樣例輸出

[
  {
    "color": "blue",
    "name": "example",
    "url": "http://localhost:8080/job/example/"
  }
]

<a name="node-config-get"></a>

jenkins.node.config(options, callback)

獲取節(jié)點(diǎn)配置信息(XML 形式)

傳參

  • name (String): 節(jié)點(diǎn)名稱

用法

jenkins.node.config('example', function(err, data) {
  if (err) throw err;

  console.log('xml', data);
});

<a name="node-create"></a>

jenkins.node.create(options, callback)

創(chuàng)建一個(gè)新的節(jié)點(diǎn)

傳參

  • name (String): 節(jié)點(diǎn)名稱

用法

jenkins.node.create('slave', function(err) {
  if (err) throw err;
});

<a name="node-destroy"></a>

jenkins.node.destroy(options, callback)

刪除節(jié)點(diǎn)

傳參

  • name (String): 節(jié)點(diǎn)名稱

用法

jenkins.node.destroy('slave', function(err) {
  if (err) throw err;
});

<a name="node-disconnect"></a>

jenkins.node.disconnect(options, callback)

斷開與指定節(jié)點(diǎn)的鏈接

傳參

  • name (String): 節(jié)點(diǎn)名稱
  • message (String, optional): reason for being disconnected

用法

jenkins.node.disconnect('slave', 'no longer used', function(err) {
  if (err) throw err;
});

<a name="node-disable"></a>

jenkins.node.disable(options, callback)

停用一個(gè)節(jié)點(diǎn)

傳參

  • name (String): 節(jié)點(diǎn)名稱
  • message (String, optional): reason for being disabled

用法

jenkins.node.disable('slave', 'network failure', function(err) {
  if (err) throw err;
});

<a name="node-enable"></a>

jenkins.node.enable(options, callback)

啟用一個(gè)節(jié)點(diǎn)

傳參

  • name (String): 節(jié)點(diǎn)名稱

用法

jenkins.node.enable('slave', function(err) {
  if (err) throw err;
});

<a name="node-exists"></a>

jenkins.node.exists(options, callback)

檢查節(jié)點(diǎn)是否存在

傳參

  • name (String): 節(jié)點(diǎn)名稱

用法

jenkins.node.exists('slave', function(err, exists) {
  if (err) throw err;

  console.log('exists', exists);
});

<a name="node-get"></a>

jenkins.node.get(options, callback)

獲取節(jié)點(diǎn)信息

傳參

  • name (String): 節(jié)點(diǎn)名稱

用法

jenkins.node.get('slave', function(err, data) {
  if (err) throw err;

  console.log('node', data);
});

樣例輸出

{
  "actions": [],
  "displayName": "slave",
  "executors": [
    {},
    {}
  ],
  "icon": "computer-x.png",
  "idle": true,
  "jnlpAgent": true,
  "launchSupported": false,
  "loadStatistics": {},
  "manualLaunchAllowed": true,
  "monitorData": {
    "hudson.node_monitors.ArchitectureMonitor": null,
    "hudson.node_monitors.ClockMonitor": null,
    "hudson.node_monitors.DiskSpaceMonitor": null,
    "hudson.node_monitors.ResponseTimeMonitor": {
      "average": 5000
    },
    "hudson.node_monitors.SwapSpaceMonitor": null,
    "hudson.node_monitors.TemporarySpaceMonitor": null
  },
  "numExecutors": 2,
  "offline": true,
  "offlineCause": null,
  "offlineCauseReason": "",
  "oneOffExecutors": [],
  "temporarilyOffline": false
}

<a name="node-list"></a>

jenkins.node.list(callback)

列出所有可用節(jié)點(diǎn)

傳參

  • full (Boolean, default: false): 包含節(jié)點(diǎn)的執(zhí)行次數(shù)

用法

jenkins.node.list(function(err, data) {
  if (err) throw err;

  console.log('nodes', data);
});

樣例輸出

{
  "busyExecutors": 0,
  "computer": [
    {
      "actions": [],
      "displayName": "master",
      "executors": [
        {},
        {}
      ],
      "icon": "computer.png",
      "idle": true,
      "jnlpAgent": false,
      "launchSupported": true,
      "loadStatistics": {},
      "manualLaunchAllowed": true,
      "monitorData": {
        "hudson.node_monitors.ArchitectureMonitor": "Linux (amd64)",
        "hudson.node_monitors.ClockMonitor": {
          "diff": 0
        },
        "hudson.node_monitors.DiskSpaceMonitor": {
          "path": "/var/lib/jenkins",
          "size": 77620142080
        },
        "hudson.node_monitors.ResponseTimeMonitor": {
          "average": 0
        },
        "hudson.node_monitors.SwapSpaceMonitor": {
          "availablePhysicalMemory": 22761472,
          "availableSwapSpace": 794497024,
          "totalPhysicalMemory": 515358720,
          "totalSwapSpace": 805302272
        },
        "hudson.node_monitors.TemporarySpaceMonitor": {
          "path": "/tmp",
          "size": 77620142080
        }
      },
      "numExecutors": 2,
      "offline": false,
      "offlineCause": null,
      "offlineCauseReason": "",
      "oneOffExecutors": [],
      "temporarilyOffline": false
    },
    {
      "actions": [],
      "displayName": "slave",
      "executors": [
        {},
        {}
      ],
      "icon": "computer-x.png",
      "idle": true,
      "jnlpAgent": true,
      "launchSupported": false,
      "loadStatistics": {},
      "manualLaunchAllowed": true,
      "monitorData": {
        "hudson.node_monitors.ArchitectureMonitor": null,
        "hudson.node_monitors.ClockMonitor": null,
        "hudson.node_monitors.DiskSpaceMonitor": null,
        "hudson.node_monitors.ResponseTimeMonitor": {
          "average": 5000
        },
        "hudson.node_monitors.SwapSpaceMonitor": null,
        "hudson.node_monitors.TemporarySpaceMonitor": null
      },
      "numExecutors": 2,
      "offline": true,
      "offlineCause": null,
      "offlineCauseReason": "",
      "oneOffExecutors": [],
      "temporarilyOffline": false
    }
  ],
  "displayName": "nodes",
  "totalExecutors": 2
}

<a name="queue-list"></a>

jenkins.queue.list(callback)

列出節(jié)點(diǎn)中當(dāng)前任務(wù)列表

用法

jenkins.queue.list(function(err, data) {
  if (err) throw err;

  console.log('queues', data);
});

樣例輸出

{
  "items": [
    {
      "actions": [
        {
          "causes": [
            {
              "shortDescription": "Started by user anonymous",
              "userId": null,
              "userName": "anonymous"
            }
          ]
        }
      ],
      "blocked": true,
      "buildable": false,
      "buildableStartMilliseconds": 1389418977387,
      "id": 20,
      "inQueueSince": 1389418977358,
      "params": "",
      "stuck": false,
      "task": {
        "color": "blue_anime",
        "name": "example",
        "url": "http://localhost:8080/job/example/"
      },
      "url": "queue/item/20/",
      "why": "Build #2 is already in progress (ETA:N/A)"
    }
  ]
}

<a name="queue-item"></a>

jenkins.queue.item(options, callback)

查看任務(wù)列表中的項(xiàng)目

傳參

  • number (Integer): 任務(wù)列表周某項(xiàng)的 ID

用法

jenkins.queue.item(130, function(err, data) {
  if (err) throw err;

  console.log('item', data);
});

樣例輸出

{
  "actions": [
    {
      "causes": [
        {
          "shortDescription": "Started by user anonymous",
          "userId": null,
          "userName": "anonymous"
        }
      ]
    }
  ],
  "blocked": false,
  "buildable": false,
  "id": 130,
  "inQueueSince": 1406363479853,
  "params": "",
  "stuck": false,
  "task": {
    "name": "test-job-b7ef0845-6515-444c-96a1-d2266d5e0f18",
    "url": "http://localhost:8080/job/test-job-b7ef0845-6515-444c-96a1-d2266d5e0f18/",
    "color": "blue"
  },
  "url": "queue/item/130/",
  "why": null,
  "executable" : {
    "number" : 28,
    "url" : "http://localhost:8080/job/test-job-b7ef0845-6515-444c-96a1-d2266d5e0f18/28/"
  }
}

<a name="queue-cancel"></a>

jenkins.queue.cancel(options, callback)

將執(zhí)行隊(duì)列中某一項(xiàng)停掉

傳參

  • number (Integer): 任務(wù)列表周某項(xiàng)的 ID

用法

jenkins.queue.cancel(23, function(err) {
  if (err) throw err;
});

<a name="view-config-get"></a>

jenkins.view.config(options, callback)

獲取視圖的配置(XML 形式)

傳參

  • name (String): 任務(wù)名稱

用法

jenkins.view.config('example', function(err, data) {
  if (err) throw err;

  console.log('xml', data);
});

<a name="view-config-set"></a>

jenkins.job.config(options, callback)

更新視圖配置(XML 形式)

傳參

  • name (String): 任務(wù)名稱
  • xml (String): 配置文件

用法

jenkins.view.config('example', xml, function(err) {
  if (err) throw err;
});

<a name="view-create"></a>

jenkins.view.create(options, callback)

創(chuàng)建視圖

傳參

  • name (String): 視圖名稱
  • type (String, enum: list, my): 視圖類型

用法

jenkins.view.create('example', 'list', function(err) {
  if (err) throw err;
});

<a name="view-destroy"></a>

jenkins.view.destroy(options, callback)

刪除視圖

傳參

  • name (String): view name

用法

jenkins.view.destroy('example', function(err) {
  if (err) throw err;
});

<a name="view-exists"></a>

jenkins.view.exists(options, callback)

檢查視圖是否存在

傳參

  • name (String): view name

用法

jenkins.view.exists('example', function(err, exists) {
  if (err) throw err;

  console.log('exists', exists);
});

<a name="view-get"></a>

jenkins.view.get(options, callback)

獲取視圖信息,json 返回

傳參

  • name (String): view name

用法

jenkins.view.get('example', function(err, data) {
  if (err) throw err;

  console.log('view', data);
});

樣例輸出

{
  "description": null,
  "jobs": [
    {
      "name": "test",
      "url": "http://localhost:8080/job/example/",
      "color": "blue"
    }
  ],
  "name": "example",
  "property": [],
  "url": "http://localhost:8080/view/example/"
}

<a name="view-list"></a>

jenkins.view.list(callback)

列出所有視圖

用法

jenkins.view.list(function(err, data) {
  if (err) throw err;

  console.log('views', data);
});

樣例輸出

{
  "views": [
    {
      "url": "http://localhost:8080/",
      "name": "All"
    },
    {
      "url": "http://localhost:8080/view/example/",
      "name": "Test"
    }
  ],
  "useSecurity": false,
  "useCrumbs": false,
  "unlabeledLoad": {},
  "slaveAgentPort": 0,
  "quietingDown": false,
  "primaryView": {
    "url": "http://localhost:8080/",
    "name": "All"
  },
  "assignedLabels": [
    {}
  ],
  "mode": "NORMAL",
  "nodeDescription": "the master Jenkins node",
  "nodeName": "",
  "numExecutors": 2,
  "description": null,
  "jobs": [
    {
      "color": "notbuilt",
      "url": "http://localhost:8080/job/example/",
      "name": "test"
    }
  ],
  "overallLoad": {}
}

<a name="view-add"></a>

jenkins.view.add(options, callback)

將任務(wù)添加到視圖

傳參

  • name (String): 視圖名稱
  • job (String): 任務(wù)名稱

用法

jenkins.view.add('example', 'jobExample', function(err) {
  if (err) throw err;
});

<a name="view-remove"></a>

jenkins.view.remove(options, callback)

將任務(wù)移除視圖

傳參

  • name (String): 視圖名稱
  • job (String): 任務(wù)名稱

用法

jenkins.view.remove('example', 'jobExample', function(err) {
  if (err) throw err;
});

License

本項(xiàng)目遵循 MIT License (see the LICENSE file).

Notes

python-jenkins (BSD License, see NOTES)
為本項(xiàng)目提供了大量思路。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容