-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·123 lines (104 loc) · 3.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env node
var Promise = require('bluebird'),
_ = require('underscore'),
rp = require('request-promise'),
argv = require('yargs').argv;
require('dotenv-safe').config({
path: `${process.cwd()}/.env`,
example: `${__dirname}/.env.example`
});
function crash(e) {
console.log(e);
return process.exit(1);
}
var Pipeline = function({ user, token, url, jobName, parameters }) {
this.url = `https://${user}:${token}@${url}/`;
this.jenkins = require('jenkins')({
baseUrl: this.url,
crumbIssuer: true,
promisify: true
});
this.options = {
name: jobName,
parameters: parameters
};
}
Pipeline.prototype.getLatestBuildNumber = function() {
return this.jenkins.job.get(this.options)
.then(res => this.options.number = res.lastBuild.number)
.catch(crash);
}
Pipeline.prototype.build = function() {
return this.jenkins.job.build(this.options)
.then(() => this.getLatestBuildNumber())
.catch(crash)
}
Pipeline.prototype.stop = function() {
return this.getLatestBuildNumber()
.then(() => {
console.log(`NOTICE: Stopping build #${this.options.number}`);
return this.jenkins.build.stop(this.options);
});
}
Pipeline.prototype.log = function() {
var done = false,
log = this.jenkins.build.logStream(this.options);
log.on('data', function(text) {
console.log(text);
});
log.on('end', function(res) {
done = true;
});
log.on('error', function(err) {
return Promise.reject(err);
});
(function resolver() {
if (done) return Promise.resolve();
setTimeout(resolver, 30);
})();
}
function parseParams(params) {
return _((params instanceof Array ? params : [params]))
.reduce((memo, param) => {
var p = param.split('=');
memo[p[0]] = p[1];
return memo;
}, {});
}
module.exports = () => {
var options = {
user: process.env.JENKINS_USERNAME,
token: process.env.JENKINS_USER_TOKEN,
url: process.env.JENKINS_URL,
jobName: process.env.JENKINS_JOB_NAME
};
if (argv.p && argv.p.length) {
options.parameters = parseParams(argv.p);
}
var pipeline = new Pipeline(options);
if (argv.stop) {
return pipeline.stop()
.then(() => pipeline.log)
.then(() => {
return process.exit();
})
.catch(e => {
console.error(e);
return process.exit(1);
});
}
return pipeline.build(pipeline.options)
.then(() => pipeline.log())
// filter for dotenv-safe error where errors are missing
// overwrite it's error message one that uses the correct path of user's .env file
.catch((e) => { return e.name === 'MissingEnvVarsError'; },
e => {
var message = `Missing the following variables in ${process.cwd()}/.env:`;
_(e.missing).each((key, i) => { message += `\n ${i+1}: ${key}`; });
return Promise.reject(new Error(message));
})
.catch(e => {
console.error(e);
});
};
module.exports();