diff --git a/scripts/upload-source-maps.ts b/scripts/upload-source-maps.ts index d7ded06a05..96a204fff7 100644 --- a/scripts/upload-source-maps.ts +++ b/scripts/upload-source-maps.ts @@ -3,88 +3,49 @@ * SPDX-License-Identifier: MPL-2.0 */ import { execSync } from 'child_process' -import { readdirSync, unlinkSync } from 'fs' -import { join } from 'path' -const main = () => { - if (process.env.VERCEL_ENV === 'development') { - return - } - - const LATEST_SHA = process.env.VERCEL_GIT_COMMIT_SHA - // const PATH_PREFIX = 'https://developer.hashicorp.com/_next/static/' - // const SERVICE = 'developer.hashicorp.com' - const PATH_PREFIX = - 'https://dev-portal-git-rn-featbuild-source-maps-during-deploy-hashicorp.vercel.app/_next/static/' - const SERVICE = 'non-prod.developer.hashicorp.com' - - const DATADOG_API_KEY = process.env.DD_API_KEY +/** + * Set up constants based on environment variables + */ +// https://vercel.com/docs/projects/environment-variables/system-environment-variables +const COMMIT_SHA = process.env.VERCEL_GIT_COMMIT_SHA +const DEPLOYMENT_URL = `https://${process.env.VERCEL_PROJECT_PRODUCTION_URL}` +const IS_DEV = process.env.VERCEL_ENV === 'development' +const IS_PROD = process.env.VERCEL_ENV === 'production' +// Note that dd stands for DataDog, these constants are used for DataDog +const DD_API_KEY = process.env.DD_API_KEY +const DD_SERVICE = IS_PROD + ? 'developer.hashicorp.com' + : 'non-prod.developer.hashicorp.com' +const DD_PATH_PREFIX = `${DEPLOYMENT_URL}/_next/static/` +/** + * Upload source maps to DataDog using the `datadog-ci` CLI. + * + * The DataDog service to which we upload source maps will vary based on + * the environment we're in, to correspond with the DataDog setup we have + * in our `config` directory. + * - If we're in development, we'll execute a "dry run", no upload will occur. + * - If we're in preview, we'll use `non-prod.developer.hashicorp.com` + * - If we're in production, we'll use `developer.hashicorp.com` + */ +function main() { try { + // Log out git remotes (debugging related issue in datadog-ci use) + execSync('git remote -v', { stdio: 'inherit' }) + // Run the upload command execSync( - `DATADOG_API_KEY=${DATADOG_API_KEY} npx @datadog/datadog-ci sourcemaps upload .next/static/ --service=${SERVICE} --release-version=${LATEST_SHA} --minified-path-prefix=${PATH_PREFIX} --disable-git`, + `DATADOG_API_KEY=${DD_API_KEY} ./node_modules/.bin/datadog-ci sourcemaps upload .next/static/ --service=${DD_SERVICE} --release-version=${COMMIT_SHA} --minified-path-prefix=${DD_PATH_PREFIX} ${ + IS_DEV ? '--dry-run' : '' + }`, { stdio: 'inherit' } ) - - console.log('Source maps uploaded successfully') + // Remove source maps after uploading + execSync(`rm -f .next/static/**/*.map`) } catch (error) { + console.error('Error: Failed to upload and remove source maps.') console.error(error) - - console.log('Failed to upload source maps') } - - // delete sourcemaps from chunks dir - try { - // Read the directory and delete all *.js.map files - const dirPath = `.next/static/chunks` - const files = readdirSync(dirPath) - files.forEach((file) => { - if (file.endsWith('.js.map')) { - unlinkSync(join(dirPath, file)) - } - }) - } catch (error) { - console.error(error) - - console.log('Failed to delete source maps from chunks dir') - } - - // delete sourcemaps from pages dir - try { - // Read the directory and delete all *.js.map files - const dirPath = `.next/static/chunks/pages` - const files = readdirSync(dirPath) - files.forEach((file) => { - if (file.endsWith('.js.map')) { - unlinkSync(join(dirPath, file)) - } - }) - } catch (error) { - console.error(error) - - console.log('Failed to delete source maps from pages dir') - } - // https://github.com/DataDog/datadog-ci/tree/79c0edce658c54001327e8bbb3f1030e8f4ccc93/src/commands/deployment - // https://app.datadoghq.com/source-code/setup/apm?env=preview&service=developer.hashicorp.com&version= - // try { - // const DD_GIT_COMMIT_SHA = process.env.DD_GIT_COMMIT_SHA - // const DD_GIT_REPOSITORY_URL = process.env.DD_GIT_REPOSITORY_URL - - // const gitInfo = `--source-control-provider=git --source-control-repository-url=${DD_GIT_REPOSITORY_URL} --source-control-revision=${DD_GIT_COMMIT_SHA}` - - // const deployStatus = execSync( - // `DATADOG_API_KEY=${DATADOG_API_KEY} npx @datadog/datadog-ci deployments create ${gitInfo} --service=${SERVICE} --env=${process.env.VERCEL_ENV}` - // ) - - // deployStatus - // .toString() - // .split('\n') - // .forEach((line) => { - // console.log(line) - // }) - - // console.log('Deployment created successfully') - // } } main()