-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnotarize-action.js
370 lines (295 loc) · 10 KB
/
notarize-action.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
'use strict';
const fs = require('fs');
const core = require('@actions/core');
const execa = require('execa');
const plist = require('plist');
const supportedTools = ['notarytool', 'altool'];
const sleep = ms => {
return new Promise(res => setTimeout(res, ms));
};
const parseConfiguration = () => {
const configuration = {
productPath: core.getInput('product-path', {required: true}),
username: core.getInput('appstore-connect-username', {required: true}),
password: core.getInput('appstore-connect-password', {required: true}),
primaryBundleId: core.getInput('primary-bundle-id'),
tool: core.getInput('tool'),
verbose: core.getInput('verbose') === 'true',
};
// require the team-id for notarytool
configuration.team = core.getInput('appstore-connect-team-id', {required: configuration.tool === 'notarytool'});
if (!fs.existsSync(configuration.productPath)) {
throw Error(`Product path ${configuration.productPath} does not exist.`);
}
return configuration;
};
const archive = async ({productPath}) => {
const archivePath = '/tmp/archive.zip'; // TODO Temporary file
const args = [
'-c', // Create an archive at the destination path
'-k', // Create a PKZip archive
'--keepParent', // Embed the parent directory name src in dst_archive.
productPath, // Source
archivePath, // Destination
];
try {
await execa('ditto', args);
} catch (error) {
core.error(error);
return null;
}
return archivePath;
};
const submitNotaryTool = async ({productPath, archivePath, primaryBundleId, username, password, team, verbose}) => {
//
// Make sure the product exists.
//
if (!fs.existsSync(productPath)) {
throw Error(`No product could be found at ${productPath}`);
}
//
// The notarization process requires us to submit a 'primary
// bundle id' - this is just a unique identifier for notarizing
// this specific product. If it is not provided then we simply
// use the actual bundle identifier from the Info.plist
//
if (primaryBundleId === '') {
const path = productPath + '/Contents/Info.plist';
if (fs.existsSync(path)) {
const info = plist.parse(fs.readFileSync(path, 'utf8'));
primaryBundleId = info.CFBundleIdentifier;
}
}
if (primaryBundleId === null) {
throw Error('No primary-bundle-id set and could not determine bundle identifier from product.');
}
//
// Run altool to notarize this application. This only submits the
// application to the queue on Apple's server side. It does not
// actually tell us if the notarization was succesdful or not, for
// that we need to poll using the request UUID that is returned.
//
// xcrun notarytool submit YourFile.dmg --apple-id yourmail@yourmail.com --team-id YOURTEAMID --password yourpassword --wait
const args = [
'notarytool',
'submit',
archivePath,
'--apple-id', username,
'--password', password,
'--team-id', team,
'--output-format', 'json',
'--wait',
];
if (verbose === true) {
args.push('--verbose');
}
const xcrun = execa('xcrun', args, {reject: false});
if (verbose == true) {
xcrun.stdout.pipe(process.stdout);
xcrun.stderr.pipe(process.stderr);
}
const {exitCode, stdout} = await xcrun;
if (exitCode === undefined) {
throw Error('Unknown failure - notarytool did not run at all?');
}
if (exitCode !== 0) {
const response = JSON.parse(stdout);
if (verbose === true) {
console.log(response);
}
core.error(`${response.status} - ${response.message}`);
return false;
}
// presumably we are good if we get here
// @NOTE: do we need to worry about non-accepted statuses?
const response = JSON.parse(stdout);
if (verbose === true) {
console.log(response);
}
core.info(`Notarization ${response.status} - ${response.message}`);
return true;
};
const submitAltool = async ({productPath, archivePath, primaryBundleId, username, password, verbose}) => {
//
// Make sure the product exists.
//
if (!fs.existsSync(productPath)) {
throw Error(`No product could be found at ${productPath}`);
}
//
// The notarization process requires us to submit a 'primary
// bundle id' - this is just a unique identifier for notarizing
// this specific product. If it is not provided then we simply
// use the actual bundle identifier from the Info.plist
//
if (primaryBundleId === '') {
const path = productPath + '/Contents/Info.plist';
if (fs.existsSync(path)) {
const info = plist.parse(fs.readFileSync(path, 'utf8'));
primaryBundleId = info.CFBundleIdentifier;
}
}
if (primaryBundleId === null) {
throw Error('No primary-bundle-id set and could not determine bundle identifier from product.');
}
//
// Run altool to notarize this application. This only submits the
// application to the queue on Apple's server side. It does not
// actually tell us if the notarization was succesdful or not, for
// that we need to poll using the request UUID that is returned.
//
const args = [
'altool',
'--output-format', 'json',
'--notarize-app',
'-f', archivePath,
'--primary-bundle-id', primaryBundleId,
'-u', username,
'-p', password,
];
if (verbose === true) {
args.push('--verbose');
}
const xcrun = execa('xcrun', args, {reject: false});
if (verbose == true) {
xcrun.stdout.pipe(process.stdout);
xcrun.stderr.pipe(process.stderr);
}
const {exitCode, stdout} = await xcrun;
if (exitCode === undefined) {
// TODO Command did not run at all
throw Error('Unknown failure - altool did not run at all?');
}
if (exitCode !== 0) {
// TODO Maybe print stderr - see where that ends up in the output? console.log("STDERR", stderr);
const response = JSON.parse(stdout);
if (verbose === true) {
console.log(response);
}
for (const productError of response['product-errors']) {
core.error(`${productError.code} - ${productError.message}`);
}
return null;
}
const response = JSON.parse(stdout);
if (verbose === true) {
console.log(response);
}
return response['notarization-upload']['RequestUUID'];
};
const waitAltool = async ({uuid, username, password, verbose}) => {
const args = [
'altool',
'--output-format', 'json',
'--notarization-info',
uuid,
'-u', username,
'-p', password,
];
if (verbose === true) {
args.push('--verbose');
}
for (let i = 0; i < 10; i++) {
const xcrun = execa('xcrun', args, {reject: false});
if (verbose == true) {
xcrun.stdout.pipe(process.stdout);
xcrun.stderr.pipe(process.stderr);
}
const {exitCode, stdout} = await xcrun;
if (exitCode === undefined) {
// TODO Command did not run at all
throw Error('Unknown failure - altool did not run at all?');
}
if (exitCode !== 0) {
// TODO Maye print stderr - see where that ends up in the output? console.log("STDERR", stderr);
const response = JSON.parse(stdout);
if (verbose === true) {
console.log(response);
}
for (const productError of response['product-errors']) {
core.error(`${productError.code} - ${productError.message}`);
}
return false;
}
const response = JSON.parse(stdout);
if (verbose === true) {
console.log(response);
}
const notarizationInfo = response['notarization-info'];
switch (notarizationInfo['Status']) {
case 'in progress':
core.info(`Notarization status <in progress>`);
break;
case 'invalid':
core.error(`Notarization status <invalid> - ${notarizationInfo['Status Message']}`);
return false;
case 'success':
core.info(`Notarization status <success>`);
return true;
default:
core.error(`Notarization status <${notarizationInfo['Status']}> - TODO`);
return false;
}
await sleep(30000);
}
core.error('Failed to get final notarization status on time.');
return false;
};
const main = async () => {
try {
const configuration = parseConfiguration();
// check if the tool is supported
if (!supportedTools.includes(configuration.tool)) {
core.setFailed(`${configuration.tool} is not a supported tool. Please choose one of ${supportedTools.join(', ')}`);
return;
}
const archivePath = await core.group('Archiving Application', async () => {
const archivePath = await archive(configuration);
if (archivePath !== null) {
core.info(`Created application archive at ${archivePath}`);
}
return archivePath;
});
if (archivePath == null) {
core.setFailed('Notarization failed');
return;
}
// altool logix
if (configuration.tool === 'altool') {
const uuid = await core.group('Notarizing with altool', async () => {
const uuid = await submitAltool({archivePath: archivePath, ...configuration});
if (uuid !== null) {
core.info(`Submitted package for notarization. Request UUID is ${uuid}`);
}
return uuid;
});
if (uuid == null) {
core.setFailed('Notarization failed');
return;
}
await sleep(15000); // TODO On a busy day, it can take a while before the build can be checked?
const success = await core.group('Waiting for Notarization Status', async () => {
return await waitAltool({uuid: uuid, archivePath: archivePath, ...configuration});
});
if (success == false) {
core.setFailed('Notarization failed');
return;
}
// notarytool logix
} else if (configuration.tool === 'notarytool') {
const success = await core.group('Notarizing with notarytool', async () => {
return await submitNotaryTool({archivePath: archivePath, ...configuration});
});
if (success == false) {
core.setFailed('Notarization failed');
return;
}
}
// finish by setting outputs
core.setOutput('product-path', configuration.productPath);
// catch unexpected
} catch (error) {
core.setFailed(`Notarization failed with an unexpected error: ${error.message}`);
}
};
main();