-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathindex.ts
More file actions
350 lines (300 loc) · 10 KB
/
index.ts
File metadata and controls
350 lines (300 loc) · 10 KB
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
import {
actionIsTestable,
createAppStoreConnectApiKeyFile,
createKeychain,
createProvisioningProfiles,
deleteAppStoreConnectApiKeyFile,
deleteKeychain,
deleteProvisioningProfiles,
getAction,
getConfiguration,
getDestination,
getIdentity,
getSchemeFromPackage,
spawn,
verbosity,
xcselect,
} from './lib'
import type { Arch, Platform } from './lib'
import xcodebuildX from './xcodebuild'
import { DefaultArtifactClient } from '@actions/artifact'
import * as core from '@actions/core'
import * as fs from 'fs'
import * as path from 'path'
import semver, { Range } from 'semver'
//TODO we also need to set the right flags for other languages
const warningsAsErrorsFlags = 'OTHER_SWIFT_FLAGS=-warnings-as-errors'
async function main() {
const cwd = core.getInput('working-directory')
if (cwd) {
process.chdir(cwd)
}
const swiftPM = fs.existsSync('Package.swift')
const platform = getPlatformInput('platform')
const platformVersion = getRangeInput('platform-version')
const arch = getArchInput('arch')
const selected = await xcselect(
getRangeInput('xcode'),
getRangeInput('swift')
)
const action = getAction(selected, platform)
const configuration = getConfiguration()
const warningsAsErrors = core.getBooleanInput('warnings-as-errors')
const destination = await getDestination(selected, platform, platformVersion)
const identity = getIdentity(core.getInput('code-sign-identity'), platform)
const currentVerbosity = verbosity()
const workspace = core.getInput('workspace')
core.info(`» Selected Xcode ${selected}`)
const reason: string | false = shouldGenerateXcodeproj()
if (reason) {
generateXcodeproj(reason)
}
const apiKey = await getAppStoreConnectApiKey()
await configureKeychain()
await configureProvisioningProfiles()
await build(await getScheme(workspace), workspace, arch)
if (core.getInput('upload-logs') == 'always') {
await uploadLogs()
}
//// immediate funcs
function getPlatformInput(input: string): Platform | undefined {
const value = core.getInput(input)
if (!value) return undefined
return value as Platform
}
function getArchInput(input: string): Arch | undefined {
const value = core.getInput(input)
if (!value) return undefined
return value as Arch
}
function getRangeInput(input: string): Range | undefined {
const value = core.getInput(input)
if (!value) return undefined
try {
return new Range(value)
} catch (error) {
throw new Error(
`failed to parse semantic version range from '${value}': ${error}`
)
}
}
function shouldGenerateXcodeproj(): string | false {
if (!swiftPM) return false
if (platform == 'watchOS' && semver.lt(selected, '12.5.0')) {
// watchOS prior to 12.4 will fail to `xcodebuild` a SwiftPM project
// failing trying to build the test modules, so we generate a project
return 'Xcode <12.5 fails to build Swift Packages for watchOS if tests exist'
} else if (semver.lt(selected, '11.0.0')) {
return 'Xcode <11 cannot build'
} else if (warningsAsErrors) {
// `build` with SwiftPM projects will build the tests too, and if there are warnings in the
// tests we will then fail to build (it's common that the tests may have ok warnings)
//TODO only do this if there are test targets
return '`warningsAsErrors` is set'
}
return false
}
function generateXcodeproj(reason: string) {
core.startGroup('Generating `.xcodeproj`')
try {
core.info(`Generating \`.xcodeproj\` ∵ ${reason}`)
spawn('swift', ['package', 'generate-xcodeproj'])
} finally {
core.endGroup()
}
}
async function getAppStoreConnectApiKey(): Promise<string[] | undefined> {
const key = core.getInput('authentication-key-base64')
if (!key) return
if (semver.lt(selected, '13.0.0')) {
core.notice(
'Ignoring authentication-key-base64 because it requires Xcode 13 or later.'
)
return
}
const keyId = core.getInput('authentication-key-id')
const keyIssuerId = core.getInput('authentication-key-issuer-id')
if (!keyId || !keyIssuerId) {
throw new Error(
'authentication-key-base64 requires authentication-key-id and authentication-key-issuer-id.'
)
}
// The user should have already stored these as encrypted secrets, but we'll
// be paranoid on their behalf.
core.setSecret(key)
core.setSecret(keyId)
core.setSecret(keyIssuerId)
const keyPath = await createAppStoreConnectApiKeyFile(key)
return [
'-allowProvisioningDeviceRegistration',
'-allowProvisioningUpdates',
'-authenticationKeyPath',
keyPath,
'-authenticationKeyID',
keyId,
'-authenticationKeyIssuerID',
keyIssuerId,
]
}
async function configureKeychain() {
const certificate = core.getInput('code-sign-certificate')
if (!certificate) return
if (process.env.RUNNER_OS != 'macOS') {
throw new Error('code-sign-certificate requires macOS.')
}
const passphrase = core.getInput('code-sign-certificate-passphrase')
if (!passphrase) {
throw new Error(
'code-sign-certificate requires code-sign-certificate-passphrase.'
)
}
await core.group('Configuring code signing', async () => {
await createKeychain(certificate, passphrase)
})
}
async function configureProvisioningProfiles() {
const mobileProfiles = core.getMultilineInput(
'mobile-provisioning-profiles-base64'
)
const profiles = core.getMultilineInput('provisioning-profiles-base64')
if (!mobileProfiles || !profiles) return
await createProvisioningProfiles(mobileProfiles, profiles)
}
async function build(scheme?: string, workspace?: string, arch?: Arch) {
if (warningsAsErrors && actionIsTestable(action)) {
await xcodebuild('build', scheme, workspace, arch)
}
await xcodebuild(action, scheme, workspace, arch)
}
//// helper funcs
async function xcodebuild(
action?: string,
scheme?: string,
workspace?: string,
arch?: Arch
) {
if (action === 'none') return
const title = ['xcodebuild', action].filter((x) => x).join(' ')
await core.group(title, async () => {
let args = destination
if (scheme) args = args.concat(['-scheme', scheme])
if (arch) args = args.concat([`-arch=${arch}`])
if (workspace) args = args.concat(['-workspace', workspace])
if (identity) args = args.concat(identity)
if (currentVerbosity == 'quiet') args.push('-quiet')
if (configuration) args = args.concat(['-configuration', configuration])
if (apiKey) args = args.concat(apiKey)
args = args.concat([
'-resultBundlePath',
`${action ?? 'xcodebuild'}.xcresult`,
])
switch (action) {
case 'build':
if (warningsAsErrors) args.push(warningsAsErrorsFlags)
break
case 'test':
case 'build-for-testing': {
if (core.getBooleanInput('code-coverage')) {
args = args.concat(['-enableCodeCoverage', 'YES'])
}
// Optional test timeouts support
const testTimeouts = core.getInput('test-timeouts')
if (testTimeouts) {
args = args.concat([
'-default-test-execution-time-allowance',
testTimeouts,
'-test-timeouts-enabled',
'YES',
])
}
break
}
}
if (core.getBooleanInput('trust-plugins')) {
args.push('-skipPackagePluginValidation')
}
if (action) args.push(action)
await xcodebuildX(args, currentVerbosity)
})
}
//NOTE this is not nearly clever enough I think
async function getScheme(workspace?: string): Promise<string | undefined> {
const scheme = core.getInput('scheme')
if (scheme) {
return scheme
}
if (swiftPM) {
return getSchemeFromPackage(workspace)
}
}
}
function post() {
deleteAppStoreConnectApiKeyFile()
deleteKeychain()
deleteProvisioningProfiles()
}
async function run() {
// We use the same entry point for `main` and `post` in action.yml in order to
// avoid duplicating common logic. To differentiate at runtime, we set some
// state in `main` for `post` to read.
const isPost = Boolean(core.getState('isPost'))
if (isPost) {
post()
return
} else {
core.saveState('isPost', true)
}
try {
await main()
} catch (error) {
await uploadLogs()
const id = `${process.env.GITHUB_RUN_ID}`
const slug = process.env.GITHUB_REPOSITORY
const href = `https://github.com/${slug}/actions/runs/${id}#artifact`
core.warning(
`
We feel you.
CI failures suck.
Download the \`.xcresult\` files we just artifact’d.
They *really* help diagnose what went wrong!
${href}
`.replace(/\s+/g, ' ')
)
throw error
}
}
run().catch((e) => {
core.setFailed(e)
if (e instanceof SyntaxError && e.stack) {
core.error(e.stack)
}
})
async function uploadLogs() {
const getFiles: (directory: string) => string[] = (directory) =>
fs
.readdirSync(directory)
.map((entry) => path.join(directory, entry))
.flatMap((entry) =>
fs.lstatSync(entry).isDirectory() ? getFiles(entry) : [entry]
)
await core.group('Uploading Logs', async () => {
const xcresults = fs
.readdirSync('.')
.filter((entry) => path.extname(entry) == '.xcresult')
if (xcresults.length === 0) {
core.warning('strange… no `.xcresult` bundles found')
}
const artifact = new DefaultArtifactClient()
for (const xcresult of xcresults) {
// random part because GitHub doesn’t yet expose any kind of per-job, per-matrix ID
// https://github.community/t/add-build-number/16149/17
const nonce = Math.random()
.toString(36)
.replace(/[^a-zA-Z0-9]+/g, '')
.substr(0, 6)
const base = path.basename(xcresult, '.xcresult')
const name = `${base}-${process.env.GITHUB_RUN_NUMBER}.${nonce}.xcresult`
await artifact.uploadArtifact(name, getFiles(xcresult), '.')
}
})
}