Skip to content

Commit

Permalink
Update the deploy script to deploy chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed Nov 29, 2024
1 parent d499b68 commit 79035cb
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 44 deletions.
60 changes: 41 additions & 19 deletions scripts/deploy/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
const { printLog, runMain } = require('../lib/executionUtils')
const { fetchPR, LOCAL_BRANCH } = require('../lib/gitUtils')
const { command } = require('../lib/command')
const { forEachFile } = require('../lib/filesUtils')

const {
buildRootUploadPath,
buildDatacenterUploadPath,
buildBundleFolder,
buildBundleFileName,
buildPullRequestUploadPath,
packages,
} = require('./lib/deploymentUtils')
Expand Down Expand Up @@ -40,29 +40,51 @@ runMain(async () => {
const awsConfig = AWS_CONFIG[env]
let cloudfrontPathsToInvalidate = []
for (const { packageName } of packages) {
const bundleFolder = buildBundleFolder(packageName)
for (const uploadPathType of uploadPathTypes) {
let uploadPath
if (uploadPathType === 'pull-request') {
const pr = await fetchPR(LOCAL_BRANCH)
if (!pr) {
console.log('No pull requests found for the branch')
return
}
uploadPath = buildPullRequestUploadPath(packageName, pr.number)
} else if (uploadPathType === 'root') {
uploadPath = buildRootUploadPath(packageName, version)
} else {
uploadPath = buildDatacenterUploadPath(uploadPathType, packageName, version)
const pathsToInvalidate = await uploadPackage(awsConfig, packageName)
cloudfrontPathsToInvalidate.push(...pathsToInvalidate)
}
invalidateCloudfront(awsConfig, cloudfrontPathsToInvalidate)
})

async function uploadPackage(awsConfig, packageName) {
const cloudfrontPathsToInvalidate = []
const bundleFolder = buildBundleFolder(packageName)

for (const uploadPathType of uploadPathTypes) {
await forEachFile(bundleFolder, async (bundlePath) => {
if (!bundlePath.endsWith('.js')) {
return
}
const bundlePath = `${bundleFolder}/${buildBundleFileName(packageName)}`

const relativeBundlePath = bundlePath.replace(`${bundleFolder}/`, '')
const uploadPath = await generateUploadPath(uploadPathType, relativeBundlePath, version)

uploadToS3(awsConfig, bundlePath, uploadPath)
cloudfrontPathsToInvalidate.push(`/${uploadPath}`)
cloudfrontPathsToInvalidate.push(uploadPath)
})
}

return cloudfrontPathsToInvalidate
}

async function generateUploadPath(uploadPathType, filePath, version) {
let uploadPath

if (uploadPathType === 'pull-request') {
const pr = await fetchPR(LOCAL_BRANCH)
if (!pr) {
console.log('No pull requests found for the branch')
process.exit(0)
}
uploadPath = buildPullRequestUploadPath(filePath, pr.number)
} else if (uploadPathType === 'root') {
uploadPath = buildRootUploadPath(filePath, version)
} else {
uploadPath = buildDatacenterUploadPath(uploadPathType, filePath, version)
}
invalidateCloudfront(awsConfig, cloudfrontPathsToInvalidate)
})

return uploadPath
}

function uploadToS3(awsConfig, bundlePath, uploadPath) {
const accessToS3 = generateEnvironmentForRole(awsConfig.accountId, 'build-stable-browser-agent-artifacts-s3-write')
Expand Down
25 changes: 14 additions & 11 deletions scripts/deploy/lib/deploymentUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ const packages = [
{ packageName: 'rum-slim', service: 'browser-rum-sdk' },
]

// ex: datadog-rum-v4.js
const buildRootUploadPath = (packageName, version, extension = 'js') => `datadog-${packageName}-${version}.${extension}`
// ex: datadog-rum-v4.js, chunks/recorder-8d8a8dfab6958424038f-datadog-rum-v4.js
const buildRootUploadPath = (filePath, version) => {
const [basePath, ...extensions] = filePath.split('.')
const ext = extensions.join('.') // allow to handle multiple extensions like `.js.map`

// ex: us1/v4/datadog-rum.js
const buildDatacenterUploadPath = (datacenter, packageName, version, extension = 'js') =>
`${datacenter}/${version}/datadog-${packageName}.${extension}`
return `${basePath}-${version}.${ext}`
}

// ex: datadog-rum.js
const buildBundleFileName = (packageName, extension = 'js') => `datadog-${packageName}.${extension}`
// ex: us1/v4/datadog-rum.js, eu1/v4/chunks/recorder-8d8a8dfab6958424038f-datadog-rum.js
const buildDatacenterUploadPath = (datacenter, filePath, version) => `${datacenter}/${version}/${filePath}`

// ex: datadog-rum.js, chunks/recorder-8d8a8dfab6958424038f-datadog-rum.js
const buildBundleFileName = (filePath) => `${filePath}`

// ex: pull-request/2781/datadog-rum.js, pull-request/2781/chunks/recorder-8d8a8dfab6958424038f-datadog-rum.js
const buildPullRequestUploadPath = (filePath, version) => `pull-request/${version}/${filePath}`

// ex: pull-request/2781/datadog-rum.js
function buildPullRequestUploadPath(packageName, version, extension = 'js') {
return `pull-request/${version}/datadog-${packageName}.${extension}`
}
// ex: packages/rum/bundle
const buildBundleFolder = (packageName) => `packages/${packageName}/bundle`

Expand Down
43 changes: 29 additions & 14 deletions scripts/deploy/upload-source-maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { command } = require('../lib/command')
const { getBuildEnvValue } = require('../lib/buildEnv')
const { getTelemetryOrgApiKey } = require('../lib/secrets')
const { siteByDatacenter } = require('../lib/datadogSites')
const { forEachFile } = require('../lib/filesUtils')
const {
buildRootUploadPath,
buildDatacenterUploadPath,
Expand Down Expand Up @@ -33,39 +34,53 @@ function getSitesByVersion(version) {
}
}

runMain(() => {
runMain(async () => {
for (const { packageName, service } of packages) {
const bundleFolder = buildBundleFolder(packageName)
for (const uploadPathType of uploadPathTypes) {
await uploadSourceMaps(packageName, service)
}
printLog('Source maps upload done.')
})

async function uploadSourceMaps(packageName, service) {
const bundleFolder = buildBundleFolder(packageName)

for (const uploadPathType of uploadPathTypes) {
await forEachFile(bundleFolder, (bundlePath) => {
if (!bundlePath.endsWith('.js.map')) {
return
}
const relativeBundlePath = bundlePath.replace(`${bundleFolder}/`, '')

let sites
let uploadPath
if (uploadPathType === 'root') {
sites = getSitesByVersion(version)
uploadPath = buildRootUploadPath(packageName, version)
renameFilesWithVersionSuffix(packageName, bundleFolder)
uploadPath = buildRootUploadPath(relativeBundlePath, version)
renameFilesWithVersionSuffix(relativeBundlePath, bundleFolder)
} else {
sites = [siteByDatacenter[uploadPathType]]
uploadPath = buildDatacenterUploadPath(uploadPathType, packageName, version)
}
const prefix = path.dirname(`/${uploadPath}`)
uploadSourceMaps(packageName, service, prefix, bundleFolder, sites)
}
uploadToDatadog(packageName, service, prefix, bundleFolder, sites)
})
}
printLog('Source maps upload done.')
})
}

function renameFilesWithVersionSuffix(packageName, bundleFolder) {
function renameFilesWithVersionSuffix(filePath, bundleFolder) {
// The datadog-ci CLI is taking a directory as an argument. It will scan every source map files in
// it and upload those along with the minified bundle. The file names must match the one from the
// CDN, thus we need to rename the bundles with the right suffix.
for (const extension of ['js', 'js.map']) {
const bundlePath = `${bundleFolder}/${buildBundleFileName(packageName, extension)}`
const uploadPath = `${bundleFolder}/${buildRootUploadPath(packageName, version, extension)}`
for (const extension of ['.js', '.js.map']) {
const temp = filePath.replace('.js.map', extension)
const bundlePath = `${bundleFolder}/${buildBundleFileName(temp)}`
const uploadPath = `${bundleFolder}/${buildRootUploadPath(temp, version)}`

command`mv ${bundlePath} ${uploadPath}`.run()
}
}

function uploadSourceMaps(packageName, service, prefix, bundleFolder, sites) {
function uploadToDatadog(packageName, service, prefix, bundleFolder, sites) {
for (const site of sites) {
printLog(`Uploading ${packageName} source maps with prefix ${prefix} for ${site}...`)

Expand Down
12 changes: 12 additions & 0 deletions scripts/lib/filesUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ function readCiFileVariable(variableName) {
return regexp.exec(ciFileContent)?.[1]
}

async function forEachFile(directoryPath, callback) {
for (const entry of fs.readdirSync(directoryPath, { withFileTypes: true })) {
const entryPath = `${directoryPath}/${entry.name}`
if (entry.isFile()) {
await callback(entryPath)
} else if (entry.isDirectory()) {
await forEachFile(entryPath, callback)
}
}
}

async function replaceCiFileVariable(variableName, value) {
await modifyFile(CI_FILE, (content) =>
content.replace(new RegExp(`${variableName}: .*`), `${variableName}: ${value}`)
Expand Down Expand Up @@ -53,4 +64,5 @@ module.exports = {
replaceCiFileVariable,
modifyFile,
findBrowserSdkPackageJsonFiles,
forEachFile,
}
1 change: 1 addition & 0 deletions webpack.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = ({ entry, mode, filename, types, keepBuildEnvVariables, plugins
mode,
output: {
filename,
chunkFilename: `chunks/[name]-[contenthash]-${filename}`,
path: path.resolve('./bundle'),
},
target: ['web', 'es2018'],
Expand Down

0 comments on commit 79035cb

Please sign in to comment.