-
Notifications
You must be signed in to change notification settings - Fork 52
/
index.ts
109 lines (92 loc) · 3 KB
/
index.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
import { version as nodeVersion } from 'node:process'
import { getDeployStore } from '@netlify/blobs'
import pMap from 'p-map'
import semver from 'semver'
import { log, logError } from '../../log/logger.js'
import { getFileWithMetadata, getKeysToUpload, scanForBlobs } from '../../utils/blobs.js'
import { type CoreStep, type CoreStepCondition, type CoreStepFunction } from '../types.js'
const coreStep: CoreStepFunction = async function ({
debug,
logs,
deployId,
buildDir,
quiet,
packagePath,
constants: { SITE_ID, NETLIFY_API_TOKEN, NETLIFY_API_HOST },
}) {
// This should never happen due to the condition check
if (!deployId || !NETLIFY_API_TOKEN) {
return {}
}
// for cli deploys with `netlify deploy --build` the `NETLIFY_API_HOST` is undefined
const apiHost = NETLIFY_API_HOST || 'api.netlify.com'
const storeOpts: Parameters<typeof getDeployStore>[0] = {
siteID: SITE_ID,
deployID: deployId,
token: NETLIFY_API_TOKEN,
apiURL: `https://${apiHost}`,
}
// If we don't have native `fetch` in the global scope, add a polyfill.
if (semver.lt(nodeVersion, '18.0.0')) {
const nodeFetch = (await import('node-fetch')).default as unknown as typeof fetch
storeOpts.fetch = nodeFetch
}
const blobs = await scanForBlobs(buildDir, packagePath)
// We checked earlier, but let's be extra safe
if (blobs === null) {
if (!quiet) {
log(logs, 'No blobs to upload to deploy store.')
}
return {}
}
// If using the deploy config API, configure the store to use the region that
// was configured for the deploy.
if (!blobs.isLegacyDirectory) {
storeOpts.experimentalRegion = 'auto'
}
const blobStore = getDeployStore(storeOpts)
const keys = await getKeysToUpload(blobs.directory)
if (keys.length === 0) {
if (!quiet) {
log(logs, 'No blobs to upload to deploy store.')
}
return {}
}
if (!quiet) {
log(logs, `Uploading ${keys.length} blobs to deploy store...`)
}
try {
await pMap(
keys,
async (key: string) => {
if (debug && !quiet) {
log(logs, `- Uploading blob ${key}`, { indent: true })
}
const { data, metadata } = await getFileWithMetadata(blobs.directory, key)
await blobStore.set(key, data, { metadata })
},
{ concurrency: 10 },
)
} catch (err) {
logError(logs, `Error uploading blobs to deploy store: ${err.message}`)
throw new Error(`Failed while uploading blobs to deploy store`)
}
if (!quiet) {
log(logs, `Done uploading blobs to deploy store.`)
}
return {}
}
const deployAndBlobsPresent: CoreStepCondition = async ({
deployId,
buildDir,
packagePath,
constants: { NETLIFY_API_TOKEN },
}) => Boolean(NETLIFY_API_TOKEN && deployId && (await scanForBlobs(buildDir, packagePath)))
export const uploadBlobs: CoreStep = {
event: 'onPostBuild',
coreStep,
coreStepId: 'blobs_upload',
coreStepName: 'Uploading blobs',
coreStepDescription: () => 'Uploading blobs to deploy store',
condition: deployAndBlobsPresent,
}