Skip to content

Commit

Permalink
build(bazel): fix AIO deployment script (#48188)
Browse files Browse the repository at this point in the history
Don't run the script under Bazel, except for the tests. Configure a
Bazel flag to select the AIO build configuration.

PR Close #48188
  • Loading branch information
kormide authored and josephperrott committed Nov 23, 2022
1 parent 332461b commit 6948f0f
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 45 deletions.
9 changes: 9 additions & 0 deletions .bazelrc
Expand Up @@ -61,6 +61,15 @@ build:aio_local_deps --workspace_status_command="yarn -s --cwd aio local-workspa
build:snapshot-build --workspace_status_command="yarn -s ng-dev release build-env-stamp --mode=snapshot"
build:snapshot-build --stamp

##########################################################
# AIO architect build configuration #
# See aio/angular.json for available configurations. #
# To build with a partiular configuration: #
# bazel build //aio:build --aio_build_config=<config> #
# Default config is `stable``. #
##########################################################
build --flag_alias=aio_build_config=//aio:flag_aio_build_config

####################################
# AIO first party dep substitution #
# Turn on with #
Expand Down
50 changes: 47 additions & 3 deletions aio/BUILD.bazel
Expand Up @@ -3,7 +3,7 @@ load("@build_bazel_rules_nodejs//:index.bzl", "copy_to_bin", "npm_package_bin")
load("//tools:defaults.bzl", "nodejs_binary")
load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
load(":local_packages_util.bzl", "link_local_packages", "substitute_local_package_deps")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag")
load("//aio/scripts:local_server_test.bzl", "local_server_test")
load("@aio_npm//@angular/build-tooling/bazel/remote-execution:index.bzl", "ENABLE_NETWORK")
load(":aio_targets.bzl", "aio_test")
Expand All @@ -29,6 +29,46 @@ config_setting(
},
)

# Config setting controlling the AIO architect configuration to build with
string_flag(
name = "flag_aio_build_config",
build_setting_default = "stable",
values = [
"stable",
"rc",
"next",
"archive",
],
)

config_setting(
name = "aio_build_config_stable",
flag_values = {
":flag_aio_build_config": "stable",
},
)

config_setting(
name = "aio_build_config_rc",
flag_values = {
":flag_aio_build_config": "rc",
},
)

config_setting(
name = "aio_build_config_next",
flag_values = {
":flag_aio_build_config": "next",
},
)

config_setting(
name = "aio_build_config_archive",
flag_values = {
":flag_aio_build_config": "archive",
},
)

# Run dgeni generation
npm_package_bin(
name = "dgeni",
Expand Down Expand Up @@ -186,8 +226,12 @@ copy_to_bin(

architect(
name = "build-app",
args = [
"site:build:stable",
args = select({
":aio_build_config_stable": ["site:build:stable"],
":aio_build_config_next": ["site:build:next"],
":aio_build_config_rc": ["site:build:rc"],
":aio_build_config_archive": ["site:build:archive"],
}) + [
"--output-path=build-app",
],
chdir = "$(RULEDIR)",
Expand Down
4 changes: 2 additions & 2 deletions aio/package.json
Expand Up @@ -30,7 +30,7 @@
"example-list-overrides": "bazel run //aio/tools/examples:example-boilerplate list-overrides",
"example-lint": "eslint content/examples",
"example-playground": "node ./tools/examples/create-example-playground-wrapper.mjs",
"deploy-production": "bazel run //aio/scripts/deploy-to-firebase --config=release",
"deploy-production": "node ./scripts/deploy-to-firebase/index.mjs",
"check-env": "yarn ~~check-env",
"payload-size": "scripts/payload.sh",
"predocs": "node scripts/contributors/validate-data && bazel build -- //aio:stackblitz //aio:example-zips",
Expand Down Expand Up @@ -169,4 +169,4 @@
"xregexp": "^5.0.2",
"yargs": "^17.0.0"
}
}
}
12 changes: 0 additions & 12 deletions aio/scripts/deploy-to-firebase/BUILD.bazel
@@ -1,7 +1,5 @@
load("@aio_npm//@bazel/jasmine:index.bzl", "jasmine_node_test")
load("//tools:defaults.bzl", "nodejs_binary")
load("@aio_npm//@angular/build-tooling/bazel/remote-execution:index.bzl", "ENABLE_NETWORK")
load("//:yarn.bzl", "YARN_LABEL")

DEPLOY_TO_FIREBASE_SOURCES = glob(
["**/*.mjs"],
Expand All @@ -12,18 +10,8 @@ DEPLOY_TO_FIREBASE_DEPS = [
"@aio_npm//shelljs",
"//aio:build",
"//:package.json",
YARN_LABEL,
]

nodejs_binary(
name = "deploy-to-firebase",
data = DEPLOY_TO_FIREBASE_SOURCES + DEPLOY_TO_FIREBASE_DEPS,
entry_point = "index.mjs",
env = {
"YARN_BIN": "$(rootpath %s)" % YARN_LABEL,
},
)

jasmine_node_test(
name = "test",
srcs = glob(["**/*.spec.mjs"]),
Expand Down
13 changes: 9 additions & 4 deletions aio/scripts/deploy-to-firebase/index.mjs
Expand Up @@ -50,6 +50,7 @@

import path from 'path';
import sh from 'shelljs';
import {fileURLToPath} from 'url';
import post from './post-deploy-actions.mjs';
import pre from './pre-deploy-actions.mjs';
import u from './utils.mjs';
Expand All @@ -58,9 +59,11 @@ sh.set('-e');


// Constants
const RUNFILES_ROOT = process.cwd();
const DIRNAME = path.join(RUNFILES_ROOT, 'aio', 'scripts', 'deploy-to-firebase');
const ROOT_PKG_PATH = path.join(RUNFILES_ROOT, 'package.json');
const inBazelTest = !!process.env.TEST_SRCDIR;
const DIRNAME = !inBazelTest
? u.getDirname(import.meta.url)
: path.join('.', 'aio', 'scripts', 'deploy-to-firebase');
const ROOT_PKG_PATH = `${DIRNAME}/../../../package.json`;

// Exports
export {
Expand All @@ -75,7 +78,9 @@ export {
// references the full file path (including the file extension).
// See https://stackoverflow.com/questions/45136831/node-js-require-main-module#answer-60309682 for
// more details.
if (path.resolve(DIRNAME, 'index.mjs') === process.argv[1]) {
const isMain = inBazelTest ||
(fileURLToPath(import.meta.url) === process.argv[1]);
if (isMain) {
const isDryRun = process.argv[2] === '--dry-run';
const inputVars = computeInputVars(process.env);
const deploymentsInfo = computeDeploymentsInfo(inputVars);
Expand Down
4 changes: 4 additions & 0 deletions aio/scripts/deploy-to-firebase/index.spec.mjs
Expand Up @@ -585,7 +585,11 @@ describe('deploy-to-firebase:', () => {
CI_BRANCH: '4.4.x',
CI_STABLE_BRANCH: mostRecentMinorBranch,
CI_COMMIT: latestCommits['4.4.x'],
// Pass along this Bazel test env var as it's used
// by the script to determine if it's in a test.
TEST_SRCDIR: process.env.TEST_SRCDIR,
};

const result = execSync(cmd, {encoding: 'utf8', env}).trim();
expect(result).toBe(
'Deployments (1): archive\n' +
Expand Down
4 changes: 2 additions & 2 deletions aio/scripts/deploy-to-firebase/pre-deploy-actions.mjs
Expand Up @@ -4,7 +4,7 @@ import u from './utils.mjs';


// Constants
const DIST_DIR = 'dist';
const DIST_DIR = 'dist/bin/aio/build';
const FIREBASE_JSON_PATH = 'firebase.json';
const NGSW_JSON_PATH = `${DIST_DIR}/ngsw.json`;
const NGSW_JSON_BAK_PATH = `${NGSW_JSON_PATH}.bak`;
Expand Down Expand Up @@ -34,7 +34,7 @@ export default exp;
// Helpers
function build({deployedUrl, deployEnv}) {
u.logSectionHeader('Build the AIO app.');
u.yarn(`build --configuration=${deployEnv} --progress=false`);
u.yarn(`build --aio_build_config=${deployEnv}`);

u.logSectionHeader('Add any mode-specific files into the AIO distribution.');
sh.cp('-rf', `src/extra-files/${deployEnv}/.`, DIST_DIR);
Expand Down
16 changes: 9 additions & 7 deletions aio/scripts/deploy-to-firebase/pre-deploy-actions.spec.mjs
Expand Up @@ -28,12 +28,12 @@ describe('deploy-to-firebase/pre-deploy-actions:', () => {

it('should build the app for the appropriate mode', () => {
pre.build({deployedUrl: 'http://example.com/foo/', deployEnv: 'bar'});
expect(yarnSpy).toHaveBeenCalledWith('build --configuration=bar --progress=false');
expect(yarnSpy).toHaveBeenCalledWith('build --aio_build_config=bar');
});

it('should add mode-specific files into the distribution', () => {
pre.build({deployedUrl: 'http://example.com/foo/', deployEnv: 'bar'});
expect(cpSpy).toHaveBeenCalledWith('-rf', 'src/extra-files/bar/.', 'dist');
expect(cpSpy).toHaveBeenCalledWith('-rf', 'src/extra-files/bar/.', 'dist/bin/aio/build');
});

it('should update the opensearch descriptor', () => {
Expand All @@ -56,8 +56,8 @@ describe('deploy-to-firebase/pre-deploy-actions:', () => {

pre.build({deployedUrl: 'http://example.com/foo/', deployEnv: 'bar'});
expect(logs).toEqual([
'yarn build --configuration=bar --progress=false',
'cp -rf src/extra-files/bar/. dist',
'yarn build --aio_build_config=bar',
'cp -rf src/extra-files/bar/. dist/bin/aio/build',
'yarn set-opensearch-url http://example.com/foo/',
]);
});
Expand All @@ -81,7 +81,8 @@ describe('deploy-to-firebase/pre-deploy-actions:', () => {

it('should disable the ServiceWorker by renaming the `ngsw.json` manifest', () => {
pre.disableServiceWorker();
expect(mvSpy).toHaveBeenCalledWith('dist/ngsw.json', 'dist/ngsw.json.bak');
expect(mvSpy).toHaveBeenCalledWith('dist/bin/aio/build/ngsw.json',
'dist/bin/aio/build/ngsw.json.bak');
});
});

Expand Down Expand Up @@ -148,7 +149,7 @@ describe('deploy-to-firebase/pre-deploy-actions:', () => {

it('should undo `build()`', () => {
pre.undo.build();
expect(rmSpy).toHaveBeenCalledWith('-rf', 'dist');
expect(rmSpy).toHaveBeenCalledWith('-rf', 'dist/bin/aio/build');
});
});

Expand All @@ -164,7 +165,8 @@ describe('deploy-to-firebase/pre-deploy-actions:', () => {

it('should undo `disableServiceWorker()`', () => {
pre.undo.disableServiceWorker();
expect(mvSpy).toHaveBeenCalledWith('dist/ngsw.json.bak', 'dist/ngsw.json');
expect(mvSpy).toHaveBeenCalledWith('dist/bin/aio/build/ngsw.json.bak',
'dist/bin/aio/build/ngsw.json');
});
});

Expand Down
2 changes: 1 addition & 1 deletion aio/scripts/deploy-to-firebase/utils.mjs
Expand Up @@ -105,5 +105,5 @@ function yarn(cmd) {
// This is not strictly necessary, since CircleCI will mask secret environment variables in the
// output (see https://circleci.com/docs/2.0/env-vars/#secrets-masking), but is an extra
// precaution.
return sh.exec(`${process.env.YARN_BIN} --silent ${cmd}`, {cwd: 'aio'});
return sh.exec(`yarn --silent ${cmd}`);
}
14 changes: 0 additions & 14 deletions aio/scripts/deploy-to-firebase/utils.spec.mjs
Expand Up @@ -284,27 +284,13 @@ describe('deploy-to-firebase/utils:', () => {

beforeEach(() => execSpy = spyOn(sh, 'exec'));

it('should execute the yarn binary in process.env.YARN_BIN', () => {
process.env.YARN_BIN = '/foo/yarn';
u.yarn('foo --bar');
const cmd = execSpy.calls.argsFor(0)[0];
expect(cmd.startsWith('/foo/yarn')).toEqual(true);
});

it('should execute yarn in silent mode', () => {
u.yarn('foo --bar');

const cmd = execSpy.calls.argsFor(0)[0];
expect(cmd.endsWith('--silent foo --bar')).toEqual(true);
});

it('should cd into aio', () => {
u.yarn('foo --bar');

const options = execSpy.calls.argsFor(0)[1];
expect(options.cwd).toEqual('aio');
});

it('should return the output from the command\'s execution', () => {
execSpy.and.returnValue('command output\n');
expect(u.yarn('foo --bar')).toBe('command output\n');
Expand Down

0 comments on commit 6948f0f

Please sign in to comment.