-
Notifications
You must be signed in to change notification settings - Fork 82
/
deploy.js
257 lines (231 loc) · 7.11 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
const Octokit = require('@octokit/rest')
const fs = require('fs')
const mime = require('mime')
const path = require('path')
const glob = require('glob-fs')({ gitignore: false })
var NightlyDeploy = {
release: {},
uploadedAssets: [],
filteredAssets: [],
config: {
owner: null,
repo: null,
branch: null,
tag: null,
assets: [],
dir: null,
token: ''
},
// Initialize NightlyDeploy.
init (config) {
this.config = config
// Makes sure of assets that will be uploaded.
this.filteredAssets = this.config.assets.filter(asset => {
return fs.existsSync(path.join(this.config.dir, asset))
})
if (this.filteredAssets.length === 0) {
console.log('There are no assets to upload...')
return
}
// Authenticating user token.
if (!this.config.token) {
throw new Error('Token is not provided')
}
console.log('Authenticating...')
this.octo = new Octokit({ auth: `token ${this.config.token}` })
this.getRelease()
},
// Tries to check whether a release exist or not
// If it exists, delete it. Otherwise create new one.
getRelease () {
console.log('Getting relesae info...')
this.octo.repos.getReleaseByTag({
owner: this.config.owner,
repo: this.config.repo,
tag: this.config.tag
}).then(result => {
// Release is already created.
console.log(this.config.tag + ' already exist')
this.release = result.data
this.getAssets(this.release.id)
}).catch(e => {
console.log('Unable to get release info...')
if (e.status === 404) {
// Create the release as it does not exist.
this.createRelease('nightly builds',
'This release is produced automatically for latest changes, ' +
'so that people can help us testing the features that ' +
'have just been added')
} else {
throw new Error('Unhandled response for getReleaseByTag: ' + e)
}
})
},
// Deletes release with releaseId, then creates new one.
deleteRelease (releaseId) {
console.log('Deleting release...')
this.octo.repos.deleteRelease({
owner: this.config.owner,
repo: this.config.repo,
release_id: releaseId
}).then(result => {
console.log('Release was deleted successfully...')
// Use previous name and body.
const name = this.release.name
const body = this.release.body
// Free release object.
this.release = null
this.createRelease(name, body)
}).catch(e => {
throw new Error('Unhandled response for deleteRelease: ' + e)
})
},
// Creates release with name and body using provided configs, then it calls
// uploadAsset.
createRelease (name, body) {
console.log('Creating a new release...')
this.octo.repos.createRelease({
owner: this.config.owner,
repo: this.config.repo,
tag_name: this.config.tag,
name: name,
body: body,
target_commitish: this.config.branch,
draft: false,
prerelease: true
}).then(result => {
console.log('Release was created successfully...')
this.release = result.data
this.uploadAsset(0)
}).catch(e => {
throw new Error('Unhandled response for createRelease: ' + e)
})
},
// Gets a list for assets of a release to avoid conflicts while uploading
// new assets by deleting them, then it calls uploadAllAssets.
getAssets (releaseId) {
console.log('Getting assets...')
this.octo.repos.listAssetsForRelease({
owner: this.config.owner,
repo: this.config.repo,
release_id: releaseId,
per_page: 100
}).then(result => {
this.uploadedAssets = result.data.map(asset => {
return { name: asset.name, id: asset.id }
})
this.uploadAsset(0)
}).catch(function (e) {
throw new Error('Unhandled response for listAssetsForRelease: ' + e)
})
},
// Uploads asset for a release if it's not already uploaded, Otherwise
// calls deleteAsset.
uploadAsset (assetIndex) {
if (assetIndex >= this.filteredAssets.length) {
console.log('Assets uploaded successfully...')
return
}
const asset = this.filteredAssets[assetIndex]
console.log('Uploading ' + asset)
// Check if it's uploaded.
const assetId = this.getAssetId(assetIndex)
if (assetId !== -1) {
console.log(asset + ' is existing, so it will be deleted')
// Asset exists, so we need to delete it first.
this.deleteAsset(assetId, assetIndex)
return
}
const assetUrl = path.join(this.config.dir, asset)
this.octo.repos.uploadReleaseAsset({
url: this.release.upload_url,
file: fs.readFileSync(assetUrl),
name: asset,
headers: {
'content-type': mime.getType(assetUrl),
'content-length': fs.statSync(assetUrl).size
}
}).then(result => {
console.log('Uploaded successfully...')
this.uploadAsset(assetIndex + 1)
}).catch(function (e) {
throw new Error('Unhandled response for uploadAsset: ' + e)
})
},
// Deletes old asset with assetId, then it calls uploadAsset to upload new
// one.
deleteAsset (assetId, assetIndex) {
console.log('Deleting ' + this.filteredAssets[assetIndex])
this.octo.repos.deleteReleaseAsset({
owner: this.config.owner,
repo: this.config.repo,
asset_id: assetId
}).then(result => {
console.log('Deleted successfully...')
this.deleteAssetId(assetId)
this.uploadAsset(assetIndex)
}).catch(function (e) {
throw new Error('Unhandled response for deleteAsset: ' + e)
})
},
// Returns id for asset to be used in deleting it.
getAssetId (index) {
const newAsset = this.filteredAssets[index]
const result = this.uploadedAssets.find(asset => asset.name === newAsset)
if (result && result.id) {
return result.id
}
return -1
},
// Removes asset from uploadedAssets list.
deleteAssetId (assetId) {
this.uploadedAssets =
this.uploadedAssets.filter(asset => asset.id !== assetId)
}
}
// Handles errors.
process.on('unhandledRejection', error => {
console.log('Failed to deploy')
console.log('unhandledRejection', error)
process.exit(1)
})
// Uses glob to get fileNames and returns array of them.
function getAssetNames (patterns = []) {
let matched = []
patterns.forEach(pattern => {
matched = glob.readdirSync(pattern)
})
return matched.map(file => path.basename(file))
}
const assets = [
'./dist/*.deb',
'./dist/*.rpm',
'./dist/*.zip',
'./dist/*.dmg',
'./dist/*.exe'
]
const repoSlug = process.env.TRAVIS_REPO_SLUG
if (repoSlug !== 'abahmed/Deer') {
console.log('Deployment is only done for abahmed/Deer')
process.exit()
}
const isPullRequest = process.env.TRAVIS_PULL_REQUEST !== 'false'
if (isPullRequest) {
console.log('Deployment is not done for Pull Requests')
process.exit()
}
const branch = process.env.TRAVIS_BRANCH
if (branch === 'master') {
NightlyDeploy.init({
owner: 'abahmed',
repo: 'Deer',
branch: branch,
tag: 'nightly',
assets: getAssetNames(assets),
dir: './dist',
token: process.env.GH_TOKEN
})
} else {
console.log('No deployments for ' + branch)
process.exit()
}