Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: introduce support for NPM_DEPLOY config for MFEs wrt uploading JS source maps to Datadog #4

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions tubular/scripts/frontend_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def install_requirements(self):
self.install_requirements_npm_private()

def install_requirements_npm_aliases(self):
""" Install npm alias requirements for app to build """
""" Install NPM alias requirements for app to build """
npm_aliases = self.get_npm_aliases_config()
if npm_aliases:
# Install and pin NPM to latest npm@8 version
Expand All @@ -126,7 +126,7 @@ def install_requirements_npm_aliases(self):
))

def install_requirements_npm_private(self):
""" Install npm private requirements for app to build """
""" Install NPM private requirements for app to build """
npm_private = self.get_npm_private_config()
if npm_private:
install_list = ' '.join(npm_private)
Expand Down Expand Up @@ -218,8 +218,32 @@ def _deploy_to_s3(self, bucket_name, app_path):
if return_code != 0:
self.FAIL(1, 'Could not sync app {} with S3 bucket {}.'.format(self.app_name, bucket_uri))

def _upload_js_sourcemaps(self, app_path):
""" Upload JavaScript sourcemaps to Datadog. """
def _get_npm_deploy_config(self):
""" Combines the common and environment configs NPM_DEPLOY packages """
npm_deploy_config = self.common_cfg.get('NPM_DEPLOY', [])
npm_deploy_config.extend(self.env_cfg.get('NPM_DEPLOY', []))
if not npm_deploy_config:
self.LOG('No NPM packages defined for deployment in config.')
return list(set(npm_deploy_config))

def _install_requirements_npm_deploy(self):
""" Install NPM requirements for app to deploy """
npm_deploy = self._get_npm_deploy_config()
if npm_deploy:
install_list = ' '.join(npm_deploy)
install_private_proc = subprocess.Popen(
[f'npm install {install_list} --no-save'],
cwd=self.app_name,
shell=True
)
install_private_proc_return_code = install_private_proc.wait()
if install_private_proc_return_code != 0:
self.FAIL(1, 'Could not run `npm install {}` for app {}.'.format(
install_list, self.app_name
))

def _upload_js_sourcemaps_config(self):
""" Retrieve config related to uploading JS sourcemaps """
app_config = self.get_app_config()
datadog_api_key = os.environ.get('DATADOG_API_KEY')
if not datadog_api_key:
Expand All @@ -232,13 +256,25 @@ def _upload_js_sourcemaps(self, app_path):
service = app_config.get('DATADOG_SERVICE')
if not service:
self.LOG('Could not find DATADOG_SERVICE for app {} while uploading source maps.'.format(self.app_name))
return

# Prioritize app-specific version override, if any, before default APP_VERSION commit SHA version
# Determine version for deployment, prioritizing app-specific version override, if any, before
# default APP_VERSION commit SHA version.
version = app_config.get('DATADOG_VERSION') or app_config.get('APP_VERSION')
if not version:
self.LOG('Could not find version for app {} while uploading source maps.'.format(self.app_name))
return

# Successfully determined service and version for the app deployment
return service, version

def _upload_js_sourcemaps(self, app_path):
""" Upload JavaScript sourcemaps to Datadog. """
service, version = self._upload_js_sourcemaps_config()
if not service or not version:
# Could not determine appropriate service or version for app; skipping.
return

command_args = ' '.join([
f'--service="{service}"',
f'--release-version="{version}"'
Expand All @@ -262,6 +298,7 @@ def _upload_js_sourcemaps(self, app_path):
def deploy_site(self, bucket_name, app_path):
""" Deploy files to bucket. """
self._deploy_to_s3(bucket_name, app_path)
self._install_requirements_npm_deploy()
self._upload_js_sourcemaps(app_path)
self.LOG('Frontend application {} successfully deployed to {}.'.format(self.app_name, bucket_name))

Expand Down
Loading