-
Notifications
You must be signed in to change notification settings - Fork 1
/
soajs.cd.js
executable file
·153 lines (129 loc) · 4.3 KB
/
soajs.cd.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"use strict";
var gitRepo = null;
var gitOwner = null;
var gitBranch = null;
var ciProvider = null;
var gitCommit = null;
var authKey = process.env.SOAJS_CD_AUTH_KEY;
var deployToken = process.env.SOAJS_CD_DEPLOY_TOKEN;
var dashboardDomain = process.env.SOAJS_CD_DASHBOARD_DOMAIN;
var dashboardProtocol = process.env.SOAJS_CD_DASHBOARD_PROTOCOL || 'https';
var dashboardPort = process.env.SOAJS_CD_DASHBOARD_PORT || '443';
var dashboardAPIRoute= process.env.SOAJS_CD_API_ROUTE;
var config = null;
var request = require("request");
var utils = {
"init": function (cb){
console.log("Initializing CD script");
//check the build environment
if(process.env.TRAVIS){
console.log("Travis build environment detected");
ciProvider = 'travis';
if(!process.env.TRAVIS_REPO_SLUG || !process.env.TRAVIS_BRANCH){
console.log("Could not find Travis environment variables (Repo Slug | branch). Aborting");
process.exit(0);
}
var repoSlug = process.env.TRAVIS_REPO_SLUG.split("/");
gitOwner = repoSlug[0].toLowerCase();
gitRepo = repoSlug[1].toLowerCase();
gitBranch = process.env.TRAVIS_BRANCH.toLowerCase();
gitCommit = process.env.TRAVIS_COMMIT;
}
else if(process.env.DRONE){
console.log("Drone build environment detected");
ciProvider = 'drone';
if(!process.env.DRONE_REPO_NAME || !process.env.DRONE_REPO_BRANCH){
console.log("Could not find Drone environment variables (Repo name | branch). Aborting");
process.exit(0);
}
gitOwner = process.env.DRONE_REPO_OWNER.toLowerCase();
gitRepo = process.env.DRONE_REPO_NAME.toLowerCase();
gitBranch = process.env.DRONE_REPO_BRANCH.toLowerCase();
gitCommit = process.env.DRONE_COMMIT;
}
else {
console.log("Could not find any build environment. Aborting...");
process.exit(0);
}
//Check if required envs are set
console.log("Checking if required environment variables are set")
//check auth env variables
if(!authKey || !deployToken){
console.log("Error: Missing AUTH env variables. Aborting...");
process.exit(0);
}
//check dashboard env variables
if(!dashboardDomain || !dashboardAPIRoute){
console.log("Error: Missing DASHBOARD environment variables. Aborting...");
process.exit(0);
}
console.log("Launching CD call...");
utils.createRequest(function(params){
console.log(params.uri);
console.log(JSON.stringify(params.body, null, 2));
request.post(params, cb);
});
},
"createRequest": function(cb) {
var params = {};
params.uri = dashboardProtocol + "://" + dashboardDomain + ":" + dashboardPort +
"/dashboard" + dashboardAPIRoute + "?deploy_token=" + deployToken;
params.headers = {
"key" : authKey,
"Content-Type": "application/json"
};
params.json = true;
try {
config = require("./soajs.json");
}
catch(e) {
console.log("Could not find a soajs.json file, searching for custom config file [config.js] ...");
try {
config = require('./config.js');
}
catch(e) {
console.log("Could not find a config.js file, repo does contain a service code...");
}
}
params.body = {
"repo" : gitRepo,
"branch": gitBranch,
"owner": gitOwner,
"ciProvider": ciProvider,
"commit": gitCommit
};
//if not a multi repo
if(config && config.type && config.type !== "multi" && config.serviceName){
params.body.services = [{"serviceName": config.serviceName}];
if(config.serviceVersion) {
params.body.services[0].serviceVersion = config.serviceVersion;
}
}
else if(config && config.type === "multi"){
//loop over each service to add its
var services = [];
config.folders.forEach(function(service){
var serviceConfigPath = "./" + service + "/config.js";
var serviceConfig = require(serviceConfigPath);
//construct each service option
var oneService = {
"serviceName": serviceConfig.serviceName
};
if(serviceConfig.serviceVersion){
oneService.serviceVersion = serviceConfig.serviceVersion;
}
services.push(oneService);
});
params.body.services = services;
}
return cb(params);
}
};
utils.init(function(err,response,body){
if (err) {
console.log(JSON.stringify (err, null, 2));
}
else {
console.log(JSON.stringify (body, null, 2));
}
});