├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) The R Consortium (http://www.r-consortium.org) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # jenkins-log-stream 3 | 4 | > Stream the output of a Jenkins build 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install --save jenkins-log-stream 10 | ``` 11 | 12 | ## Usage 13 | 14 | ```js 15 | var JenkinsLogStream = require('jenkins-log-stream'); 16 | var stream = new JenkinsLogStream({ 17 | 'baseUrl': 'http://user:pass@jenkins.server:8080', 18 | 'job': 'jenkins-job', 19 | 'build': 'lastBuild', 20 | 'pollInterval': 1000 21 | }); 22 | 23 | stream.pipe(process.stdout); 24 | ``` 25 | 26 | ## Options 27 | 28 | * `baseUrl` The Jenkins base URL, including the username and password, and 29 | also the port if it is not 80. Required. 30 | * `job` The name of the Jenkins job. Required. 31 | * `build` The id of the build, a numeric value or `'lastBuild'` for the last build. 32 | Defaults to `'lastBuild'`. 33 | * `pollInterval` Integer to specify how often to poll the Jenkins server, in 34 | milliseconds. Defaults to 1000, i.e. one second. 35 | 36 | ## License 37 | 38 | MIT @ [R consortium](https://github.com/r-hub) 39 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | const Readable = require('stream').Readable; 3 | const util = require('util'); 4 | const got = require('got'); 5 | const url = require('url'); 6 | 7 | util.inherits(JenkinsOutput, Readable); 8 | 9 | function JenkinsOutput(options) { 10 | Readable.call(this, options); 11 | this._baseUrl = options.baseUrl; 12 | this._job = options.job; 13 | this._build = options.build || 'lastBuild'; 14 | this._url = url.parse( 15 | this._baseUrl + '/job/' + this._job + '/' + this._build + 16 | '/logText/progressiveText' 17 | ); 18 | this._n = 0; 19 | this._pollInterval = options.pollInterval || 1000; 20 | } 21 | 22 | JenkinsOutput.prototype._read = function() { 23 | var self = this; 24 | if (this._done) { self.push(null); } 25 | 26 | function get() { 27 | 28 | var url = self._url.protocol + '//' + self._url.host + 29 | self._url.path; 30 | 31 | got.get(url, { 32 | auth: self._url.auth, 33 | query: { 'start': self._n } }) 34 | 35 | .then(function(response) { 36 | self._n = response.headers['x-text-size']; 37 | 38 | if (response.body.length > 0) { 39 | // If there is data, then push it and we are done 40 | return self.push(response.body); 41 | 42 | } else if (response.headers['x-more-data'] !== 'true') { 43 | // If log if complete, then end the stream 44 | self.push(null); 45 | 46 | } else if (response.body.length == 0) { 47 | // If there will be more data in the future, 48 | // then try to poll again in three seconds. 49 | setTimeout(get, self._pollInterval); 50 | } 51 | }) 52 | } 53 | 54 | get(); 55 | } 56 | 57 | module.exports = JenkinsOutput; 58 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jenkins-log-stream", 3 | "version": "2.0.0", 4 | "description": "Stream the console output of a Jenkins job", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/r-hub/jenkins-log-stream.git" 12 | }, 13 | "keywords": [ 14 | "Jenkins", 15 | "console", 16 | "output", 17 | "log", 18 | "stream" 19 | ], 20 | "author": "Gábor Csárdi", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/r-hub/jenkins-log-stream/issues" 24 | }, 25 | "homepage": "https://github.com/r-hub/jenkins-log-stream#readme", 26 | "dependencies": { 27 | "got": "^6.3.0" 28 | } 29 | } 30 | --------------------------------------------------------------------------------