forked from desktop/desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.ts
208 lines (181 loc) · 5.08 KB
/
publish.ts
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
const PUBLISH_CHANNELS = ['production', 'test', 'beta']
import * as distInfo from './dist-info'
import * as gitInfo from '../app/git-info'
import * as packageInfo from '../app/package-info'
import * as platforms from './build-platforms'
if (PUBLISH_CHANNELS.indexOf(distInfo.getReleaseChannel()) < 0) {
console.log('Not a publishable build. Skipping publish.')
process.exit(0)
}
const releaseSHA = distInfo.getReleaseSHA()
if (releaseSHA == null) {
console.log(`No release SHA found for build. Skipping publish.`)
process.exit(0)
}
const currentTipSHA = gitInfo.getSHA()
if (!currentTipSHA.toUpperCase().startsWith(releaseSHA!.toUpperCase())) {
console.log(
`Current tip '${currentTipSHA}' does not match release SHA '${releaseSHA}'. Skipping publish.`
)
process.exit(0)
}
import * as Fs from 'fs'
import { execSync } from 'child_process'
import * as AWS from 'aws-sdk'
import * as Crypto from 'crypto'
import * as request from 'request'
console.log('Packaging…')
execSync('yarn package')
const sha = platforms.getSha().substr(0, 8)
function getSecret() {
if (process.env.DEPLOYMENT_SECRET != null) {
return process.env.DEPLOYMENT_SECRET
}
throw new Error(
`Unable to get deployment seret environment variable. Deployment aborting...`
)
}
console.log('Uploading…')
let uploadPromise = null
if (process.platform === 'darwin') {
uploadPromise = uploadOSXAssets()
} else if (process.platform === 'win32') {
uploadPromise = uploadWindowsAssets()
} else {
console.error(`I dunno how to publish a release for ${process.platform} :(`)
process.exit(1)
}
uploadPromise!
.then(artifacts => {
const names = artifacts.map(function(item, index) {
return item.name
})
console.log(`Uploaded artifacts: ${names}`)
return updateDeploy(artifacts, getSecret())
})
.catch(e => {
console.error(`Publishing failed: ${e}`)
process.exit(1)
})
function uploadOSXAssets() {
const uploads = [upload(distInfo.getOSXZipName(), distInfo.getOSXZipPath())]
return Promise.all(uploads)
}
function uploadWindowsAssets() {
const uploads = [
upload(
distInfo.getWindowsInstallerName(),
distInfo.getWindowsInstallerPath()
),
upload(
distInfo.getWindowsStandaloneName(),
distInfo.getWindowsStandalonePath()
),
upload(
distInfo.getWindowsFullNugetPackageName(),
distInfo.getWindowsFullNugetPackagePath()
),
]
if (distInfo.shouldMakeDelta()) {
uploads.push(
upload(
distInfo.getWindowsDeltaNugetPackageName(),
distInfo.getWindowsDeltaNugetPackagePath()
)
)
}
return Promise.all(uploads)
}
interface IUploadResult {
name: string
url: string
size: number
sha: string
}
function upload(assetName: string, assetPath: string) {
const s3Info = {
accessKeyId: process.env.S3_KEY,
secretAccessKey: process.env.S3_SECRET,
}
const s3 = new AWS.S3(s3Info)
const bucket = process.env.S3_BUCKET || ''
const key = `releases/${packageInfo.getVersion()}-${sha}/${assetName.replace(
/ /g,
''
)}`
const url = `https://s3.amazonaws.com/${bucket}/${key}`
const uploadParams = {
Bucket: bucket,
ACL: 'public-read',
Key: key,
Body: Fs.createReadStream(assetPath),
}
return new Promise<IUploadResult>((resolve, reject) => {
s3.upload(
uploadParams,
(error: Error, data: AWS.S3.ManagedUpload.SendData) => {
if (error != null) {
reject(error)
} else {
// eslint-disable-next-line no-sync
const stats = Fs.statSync(assetPath)
const hash = Crypto.createHash('sha1')
hash.setEncoding('hex')
const input = Fs.createReadStream(assetPath)
hash.on('finish', () => {
const sha = hash.read() as string
resolve({ name: assetName, url, size: stats['size'], sha })
})
input.pipe(hash)
}
}
)
})
}
function createSignature(body: any, secret: string) {
const hmac = Crypto.createHmac('sha1', secret)
hmac.update(JSON.stringify(body))
return `sha1=${hmac.digest('hex')}`
}
function updateDeploy(artifacts: ReadonlyArray<IUploadResult>, secret: string) {
const { rendererSize, mainSize } = distInfo.getBundleSizes()
const body = {
context: process.platform,
branch_name: platforms.getReleaseBranchName(),
artifacts,
stats: {
platform: process.platform,
rendererBundleSize: rendererSize,
mainBundleSize: mainSize,
},
}
const signature = createSignature(body, secret)
const options = {
method: 'POST',
url: 'https://central.github.com/api/deploy_built',
headers: {
'X-Hub-Signature': signature,
},
json: true,
body,
}
return new Promise((resolve, reject) => {
request(options, (error, response, body) => {
if (error) {
reject(error)
return
}
if (response.statusCode !== 200) {
reject(
new Error(
`Received a non-200 response (${
response.statusCode
}): ${JSON.stringify(body)}`
)
)
return
}
resolve()
})
})
}