This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdeploy.js
186 lines (156 loc) · 4.89 KB
/
deploy.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* globals exec */
var async = require('async')
var _ = require('lodash')
var dir = require('node-dir')
var fs = require('fs')
var heroku = require('./deploy/heroku')
var mime = require('mime')
var request = require('request')
var util = require('util')
require('shelljs/global')
var _command = function (cmd) {
return exec(cmd, {silent: true}).stdout.replace(/\n/, '')
}
var username = function () {
var user = _command('git config user.email')
if (user === '') user = process.env.DEPLOY_USER
return user
}
var Deployer = function (app, done, log) {
this.app = app
this.done = done
this.bundleExts = ['js', 'css']
this.bundlePaths = {}
this.log = log
this.version = process.env.BUNDLE_VERSION
this.awsContentUrlPrefix = process.env.AWS_S3_CONTENT_URL + '/'
}
Deployer.prototype.fail = function (err) {
this.log.errorlns(err)
this.done(false)
}
Deployer.prototype.upload = function (done) {
this.log.subhead('uploading to S3')
var s3 = new (require('aws-sdk')).S3()
var upload = function (path, contents, type, done) {
this.log.writeln(path)
s3.putObject({
Bucket: process.env.AWS_S3_BUCKET,
Key: path,
Body: contents,
ACL: 'public-read',
ContentType: type
}, done)
}.bind(this)
var version = this.version
dir.files('dist/deploy/pages', function (err, files) {
if (err) throw err
async.each(files, function (filename, next) {
var contents = fs.readFileSync(filename)
var path = filename.replace(/^dist\/deploy\/pages/, 'assets/' + version)
upload(path, contents, mime.lookup(filename), next)
}, done)
})
}
Deployer.prototype.uploadSourceMap = function (callback) {
this.log.subhead('uploading source map to Rollbar')
var sourceMapFile = 'dist/deploy/pages/bundle.min.js.map'
var formData = {
// Pass a simple key-value pair
access_token: process.env.ROLLBAR_SERVER_TOKEN,
// Pass data via Buffers
version: this.version,
minified_url: this.awsContentUrlPrefix + this.bundlePaths.js,
// Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
// See the `form-data` README for more information about options: https://github.com/felixge/node-form-data
source_map: {
value: fs.createReadStream(sourceMapFile),
options: {
filename: sourceMapFile,
contentType: 'application/octet-stream'
}
}
}
request.post({
url: 'https://api.rollbar.com/api/1/sourcemap',
formData: formData
}, function (err, httpResponse, body) {
if (err || body.err > 0) {
console.error('upload failed:', err)
this.fail(err.message)
}
callback()
}.bind(this))
}
Deployer.prototype.notifyRollbar = function (callback) {
this.log.subhead('notifying Rollbar of deploy')
var formData = {
access_token: process.env.ROLLBAR_SERVER_TOKEN,
revision: require('./deploy/version')(),
environment: process.env.NODE_ENV,
local_username: username()
}
request.post({
url: 'https://api.rollbar.com/api/1/deploy/',
formData: formData
}, function (err, httpResponse, body) {
if (err || body.err > 0) {
this.fail(err)
}
callback()
}.bind(this))
}
Deployer.prototype.updateEnv = function (callback) {
this.log.subhead(util.format('updating BUNDLE_VERSION on %s: %s', this.app, this.version))
heroku.config(this.app).update({BUNDLE_VERSION: this.version}, callback)
}
var api = {
appName: function (target) {
var name
if (target === 'staging') {
name = 'hylo-node-staging'
} else if (target === 'production') {
name = 'hylo-node'
}
return name
},
setupEnv: function (target, log, done) {
log.subhead('setting up environment variables for deploy')
process.env.BUNDLE_VERSION = require('./deploy/version')(8)
log.writeln('BUNDLE_VERSION: ' + process.env.BUNDLE_VERSION)
var appName = api.appName(target)
var keys = [
'NODE_ENV',
'ASSET_HOST_URL',
'AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'AWS_S3_BUCKET', 'AWS_S3_CONTENT_URL',
'SLACK_APP_CLIENT_ID',
'BRANCH_API_KEY',
'FACEBOOK_APP_ID',
'FILEPICKER_API_KEY',
'GOOGLE_BROWSER_KEY', 'GOOGLE_CLIENT_ID',
'ROLLBAR_CLIENT_TOKEN', 'ROLLBAR_SERVER_TOKEN',
'SEGMENT_KEY', 'STRIPE_PUBLISHABLE_KEY'
]
log.writeln(util.format('fetching from %s: %s', appName, keys.join(', ')))
heroku.config(appName).info(function (err, vars) {
_.extend(process.env, _.pick(vars, keys))
done(err)
})
},
run: function (target, done, log) {
var deployer = new Deployer(api.appName(target), done, log)
async.series([
deployer.upload.bind(deployer),
deployer.uploadSourceMap.bind(deployer),
deployer.notifyRollbar.bind(deployer),
deployer.updateEnv.bind(deployer)
], function (err) {
if (err) {
deployer.fail(err)
} else {
done()
}
})
}
}
module.exports = api