From 8075b67e64c7903e9e328f7242d18600fac2020b Mon Sep 17 00:00:00 2001 From: Axel Hecht Date: Mon, 6 Mar 2023 16:55:32 +0100 Subject: [PATCH 1/7] Pass configs from build:backend to app builds. Fixes #15274 When using `build:backend` with `--config` options, they're not passed down to the packaging of the FE app. Thus, that's picking up the default configs, which includes the `app-config.local.yaml`, but it's also missing the production yaml, if requested. Pass the given `configPaths` around for the backend build like the frontend build already does. Signed-off-by: Axel Hecht --- .changeset/clever-lizards-whisper.md | 5 +++++ .../cli/src/commands/build/buildBackend.ts | 4 +++- packages/cli/src/commands/build/command.ts | 1 + packages/cli/src/lib/config.ts | 2 +- .../src/lib/packager/createDistWorkspace.ts | 21 +++++++++++++++---- 5 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 .changeset/clever-lizards-whisper.md diff --git a/.changeset/clever-lizards-whisper.md b/.changeset/clever-lizards-whisper.md new file mode 100644 index 0000000000..5b5307af1f --- /dev/null +++ b/.changeset/clever-lizards-whisper.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Support building the frontend with the same set of configs as the backend. diff --git a/packages/cli/src/commands/build/buildBackend.ts b/packages/cli/src/commands/build/buildBackend.ts index 7e61f9ae85..5d42aa18d4 100644 --- a/packages/cli/src/commands/build/buildBackend.ts +++ b/packages/cli/src/commands/build/buildBackend.ts @@ -28,10 +28,11 @@ const SKELETON_FILE = 'skeleton.tar.gz'; interface BuildBackendOptions { targetDir: string; skipBuildDependencies: boolean; + configPaths?: string[]; } export async function buildBackend(options: BuildBackendOptions) { - const { targetDir, skipBuildDependencies } = options; + const { targetDir, skipBuildDependencies, configPaths } = options; const pkg = await fs.readJson(resolvePath(targetDir, 'package.json')); // We build the target package without generating type declarations. @@ -45,6 +46,7 @@ export async function buildBackend(options: BuildBackendOptions) { try { await createDistWorkspace([pkg.name], { targetDir: tmpDir, + configPaths, buildDependencies: !skipBuildDependencies, buildExcludes: [pkg.name], parallelism: getEnvironmentParallelism(), diff --git a/packages/cli/src/commands/build/command.ts b/packages/cli/src/commands/build/command.ts index fcaf58e1ee..7330d9891f 100644 --- a/packages/cli/src/commands/build/command.ts +++ b/packages/cli/src/commands/build/command.ts @@ -34,6 +34,7 @@ export async function command(opts: OptionValues): Promise { if (role === 'backend') { return buildBackend({ targetDir: paths.targetDir, + configPaths: opts.config as string[], skipBuildDependencies: Boolean(opts.skipBuildDependencies), }); } diff --git a/packages/cli/src/lib/config.ts b/packages/cli/src/lib/config.ts index a76571c3f6..c813ce59fd 100644 --- a/packages/cli/src/lib/config.ts +++ b/packages/cli/src/lib/config.ts @@ -38,7 +38,7 @@ export async function loadCliConfig(options: Options) { const configTargets: ConfigTarget[] = []; options.args.forEach(arg => { if (!isValidUrl(arg)) { - configTargets.push({ path: paths.resolveTarget(arg) }); + configTargets.push({ path: paths.resolveTargetRoot(arg) }); } }); diff --git a/packages/cli/src/lib/packager/createDistWorkspace.ts b/packages/cli/src/lib/packager/createDistWorkspace.ts index 2d2967fc44..601f442a73 100644 --- a/packages/cli/src/lib/packager/createDistWorkspace.ts +++ b/packages/cli/src/lib/packager/createDistWorkspace.ts @@ -60,6 +60,11 @@ type Options = { */ targetDir?: string; + /** + * Configuration files to load during packaging. + */ + configPaths?: string[]; + /** * Files to copy into the target workspace. * @@ -127,13 +132,18 @@ export async function createDistWorkspace( if (options.buildDependencies) { const exclude = options.buildExcludes ?? []; + const configPaths = options.configPaths ?? []; const toBuild = new Set( targets.map(_ => _.name).filter(name => !exclude.includes(name)), ); const standardBuilds = new Array(); - const customBuild = new Array<{ dir: string; name: string }>(); + const customBuild = new Array<{ + dir: string; + name: string; + args?: string[]; + }>(); for (const pkg of packages) { if (!toBuild.has(pkg.packageJson.name)) { @@ -166,7 +176,10 @@ export async function createDistWorkspace( console.warn( `Building ${pkg.packageJson.name} separately because it is a bundled package`, ); - customBuild.push({ dir: pkg.dir, name: pkg.packageJson.name }); + const args = buildScript.includes('--config') + ? [] + : configPaths.map(p => ['--config', p]).flat(); + customBuild.push({ dir: pkg.dir, name: pkg.packageJson.name, args }); continue; } @@ -193,8 +206,8 @@ export async function createDistWorkspace( if (customBuild.length > 0) { await runParallelWorkers({ items: customBuild, - worker: async ({ name, dir }) => { - await run('yarn', ['run', 'build'], { + worker: async ({ name, dir, args }) => { + await run('yarn', ['run', 'build', ...(args || [])], { cwd: dir, stdoutLogFunc: prefixLogFunc(`${name}: `, 'stdout'), stderrLogFunc: prefixLogFunc(`${name}: `, 'stderr'), From 559af0f44fbf0628047cb67d51e37b3ff36043fb Mon Sep 17 00:00:00 2001 From: Axel Hecht Date: Tue, 7 Mar 2023 10:19:42 +0100 Subject: [PATCH 2/7] Update the docs to suggest passing in the right configs. Remove old note from docker docs Signed-off-by: Axel Hecht --- docs/deployment/docker.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/docs/deployment/docker.md b/docs/deployment/docker.md index 88c22afb9b..f96204b413 100644 --- a/docs/deployment/docker.md +++ b/docs/deployment/docker.md @@ -37,10 +37,6 @@ The required steps in the host build are to install dependencies with `yarn install`, generate type definitions using `yarn tsc`, and build the backend package with `yarn build:backend`. -> NOTE: If you created your app prior to 2021-02-18, follow the -> [migration step](https://github.com/backstage/backstage/releases/tag/release-2021-02-18) -> to move from `backend:build` to `backend:bundle`. - In a CI workflow it might look something like this: ```bash @@ -50,7 +46,8 @@ yarn install --frozen-lockfile yarn tsc # Build the backend, which bundles it all up into the packages/backend/dist folder. -yarn build:backend +# The configuration files here should match the one you use inside the Dockerfile below. +yarn build:backend --config app-config.yaml ``` Once the host build is complete, we are ready to build our image. The following From 5896463fb225b6cf70d8f9e38ec25daf591f7604 Mon Sep 17 00:00:00 2001 From: Axel Hecht Date: Tue, 7 Mar 2023 10:49:42 +0100 Subject: [PATCH 3/7] Update .changeset/clever-lizards-whisper.md Co-authored-by: Patrik Oldsberg Signed-off-by: Axel Hecht --- .changeset/clever-lizards-whisper.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/clever-lizards-whisper.md b/.changeset/clever-lizards-whisper.md index 5b5307af1f..4f2820c347 100644 --- a/.changeset/clever-lizards-whisper.md +++ b/.changeset/clever-lizards-whisper.md @@ -2,4 +2,4 @@ '@backstage/cli': patch --- -Support building the frontend with the same set of configs as the backend. +When building a backend package with dependencies any `--config ` options will now be forwarded to any dependent app package builds, unless the build script in the app package already contains a `--config` option. From e9e9c7962f76131f3bf5025682612fd4b1a800e3 Mon Sep 17 00:00:00 2001 From: Axel Hecht Date: Wed, 8 Mar 2023 20:09:31 +0100 Subject: [PATCH 4/7] Resolve --config options against both current directory and monorepo root. Signed-off-by: Axel Hecht --- .changeset/chilled-bobcats-repeat.md | 5 +++++ packages/cli/src/lib/config.ts | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 .changeset/chilled-bobcats-repeat.md diff --git a/.changeset/chilled-bobcats-repeat.md b/.changeset/chilled-bobcats-repeat.md new file mode 100644 index 0000000000..572a82b589 --- /dev/null +++ b/.changeset/chilled-bobcats-repeat.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Resolve config options against both the current directory and the monorepo root directory. diff --git a/packages/cli/src/lib/config.ts b/packages/cli/src/lib/config.ts index c813ce59fd..9bb32fa481 100644 --- a/packages/cli/src/lib/config.ts +++ b/packages/cli/src/lib/config.ts @@ -24,6 +24,7 @@ import { paths } from './paths'; import { isValidUrl } from './urls'; import { getPackages } from '@manypkg/get-packages'; import { PackageGraph } from './monorepo'; +import { pathExistsSync } from 'fs-extra'; type Options = { args: string[]; @@ -38,7 +39,11 @@ export async function loadCliConfig(options: Options) { const configTargets: ConfigTarget[] = []; options.args.forEach(arg => { if (!isValidUrl(arg)) { - configTargets.push({ path: paths.resolveTargetRoot(arg) }); + let path = paths.resolveTarget(arg); + if (!pathExistsSync(path)) { + path = paths.resolveOwnRoot(arg); + } + configTargets.push({ path }); } }); From 3944ead310fc5e3291f75a4bd707e1438fd8e704 Mon Sep 17 00:00:00 2001 From: Axel Hecht Date: Fri, 31 Mar 2023 15:00:53 +0200 Subject: [PATCH 5/7] Resolve config paths early for backend builds Signed-off-by: Axel Hecht --- .changeset/chilled-bobcats-repeat.md | 5 ----- packages/cli/src/commands/build/buildBackend.ts | 10 +++++++++- packages/cli/src/lib/config.ts | 7 +------ 3 files changed, 10 insertions(+), 12 deletions(-) delete mode 100644 .changeset/chilled-bobcats-repeat.md diff --git a/.changeset/chilled-bobcats-repeat.md b/.changeset/chilled-bobcats-repeat.md deleted file mode 100644 index 572a82b589..0000000000 --- a/.changeset/chilled-bobcats-repeat.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@backstage/cli': patch ---- - -Resolve config options against both the current directory and the monorepo root directory. diff --git a/packages/cli/src/commands/build/buildBackend.ts b/packages/cli/src/commands/build/buildBackend.ts index 5d42aa18d4..f82567de3c 100644 --- a/packages/cli/src/commands/build/buildBackend.ts +++ b/packages/cli/src/commands/build/buildBackend.ts @@ -18,6 +18,7 @@ import os from 'os'; import fs from 'fs-extra'; import { resolve as resolvePath } from 'path'; import tar, { CreateOptions } from 'tar'; +import { paths } from '../../lib/paths'; import { createDistWorkspace } from '../../lib/packager'; import { getEnvironmentParallelism } from '../../lib/parallel'; import { buildPackage, Output } from '../../lib/builder'; @@ -42,11 +43,18 @@ export async function buildBackend(options: BuildBackendOptions) { outputs: new Set([Output.cjs]), }); + const resolvedConfigPaths = configPaths?.map(p => { + let path = paths.resolveTarget(p); + if (!fs.pathExistsSync(path)) { + path = paths.resolveOwnRoot(p); + } + return path; + }); const tmpDir = await fs.mkdtemp(resolvePath(os.tmpdir(), 'backstage-bundle')); try { await createDistWorkspace([pkg.name], { targetDir: tmpDir, - configPaths, + configPaths: resolvedConfigPaths, buildDependencies: !skipBuildDependencies, buildExcludes: [pkg.name], parallelism: getEnvironmentParallelism(), diff --git a/packages/cli/src/lib/config.ts b/packages/cli/src/lib/config.ts index 9bb32fa481..a76571c3f6 100644 --- a/packages/cli/src/lib/config.ts +++ b/packages/cli/src/lib/config.ts @@ -24,7 +24,6 @@ import { paths } from './paths'; import { isValidUrl } from './urls'; import { getPackages } from '@manypkg/get-packages'; import { PackageGraph } from './monorepo'; -import { pathExistsSync } from 'fs-extra'; type Options = { args: string[]; @@ -39,11 +38,7 @@ export async function loadCliConfig(options: Options) { const configTargets: ConfigTarget[] = []; options.args.forEach(arg => { if (!isValidUrl(arg)) { - let path = paths.resolveTarget(arg); - if (!pathExistsSync(path)) { - path = paths.resolveOwnRoot(arg); - } - configTargets.push({ path }); + configTargets.push({ path: paths.resolveTarget(arg) }); } }); From 22d8703a264849a996c1b8c31dd43431c86d8d61 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 4 Apr 2023 11:51:51 +0200 Subject: [PATCH 6/7] cli: resolve config paths upfront Signed-off-by: Patrik Oldsberg --- .../cli/src/commands/build/buildBackend.ts | 10 +----- packages/cli/src/commands/build/command.ts | 33 ++++++++++++------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/packages/cli/src/commands/build/buildBackend.ts b/packages/cli/src/commands/build/buildBackend.ts index f82567de3c..5d42aa18d4 100644 --- a/packages/cli/src/commands/build/buildBackend.ts +++ b/packages/cli/src/commands/build/buildBackend.ts @@ -18,7 +18,6 @@ import os from 'os'; import fs from 'fs-extra'; import { resolve as resolvePath } from 'path'; import tar, { CreateOptions } from 'tar'; -import { paths } from '../../lib/paths'; import { createDistWorkspace } from '../../lib/packager'; import { getEnvironmentParallelism } from '../../lib/parallel'; import { buildPackage, Output } from '../../lib/builder'; @@ -43,18 +42,11 @@ export async function buildBackend(options: BuildBackendOptions) { outputs: new Set([Output.cjs]), }); - const resolvedConfigPaths = configPaths?.map(p => { - let path = paths.resolveTarget(p); - if (!fs.pathExistsSync(path)) { - path = paths.resolveOwnRoot(p); - } - return path; - }); const tmpDir = await fs.mkdtemp(resolvePath(os.tmpdir(), 'backstage-bundle')); try { await createDistWorkspace([pkg.name], { targetDir: tmpDir, - configPaths: resolvedConfigPaths, + configPaths, buildDependencies: !skipBuildDependencies, buildExcludes: [pkg.name], parallelism: getEnvironmentParallelism(), diff --git a/packages/cli/src/commands/build/command.ts b/packages/cli/src/commands/build/command.ts index 7330d9891f..dcbf65331d 100644 --- a/packages/cli/src/commands/build/command.ts +++ b/packages/cli/src/commands/build/command.ts @@ -20,23 +20,32 @@ import { findRoleFromCommand, getRoleInfo } from '../../lib/role'; import { paths } from '../../lib/paths'; import { buildFrontend } from './buildFrontend'; import { buildBackend } from './buildBackend'; +import { isValidUrl } from '../../lib/urls'; export async function command(opts: OptionValues): Promise { const role = await findRoleFromCommand(opts); - if (role === 'frontend') { - return buildFrontend({ - targetDir: paths.targetDir, - configPaths: opts.config as string[], - writeStats: Boolean(opts.stats), - }); - } - if (role === 'backend') { - return buildBackend({ - targetDir: paths.targetDir, - configPaths: opts.config as string[], - skipBuildDependencies: Boolean(opts.skipBuildDependencies), + if (role === 'frontend' || role === 'backend') { + const configPaths = (opts.config as string[]).map(arg => { + if (isValidUrl(arg)) { + return arg; + } + return paths.resolveTarget(arg); }); + + if (role === 'frontend') { + return buildFrontend({ + targetDir: paths.targetDir, + configPaths, + writeStats: Boolean(opts.stats), + }); + } else { + return buildBackend({ + targetDir: paths.targetDir, + configPaths, + skipBuildDependencies: Boolean(opts.skipBuildDependencies), + }); + } } const roleInfo = getRoleInfo(role); From 5786b18dca576f23710fc07ce1dbcc4ca477b0ff Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 4 Apr 2023 12:36:34 +0200 Subject: [PATCH 7/7] cli: lint fix Signed-off-by: Patrik Oldsberg --- packages/cli/src/commands/build/command.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/cli/src/commands/build/command.ts b/packages/cli/src/commands/build/command.ts index dcbf65331d..90b76545ce 100644 --- a/packages/cli/src/commands/build/command.ts +++ b/packages/cli/src/commands/build/command.ts @@ -39,13 +39,12 @@ export async function command(opts: OptionValues): Promise { configPaths, writeStats: Boolean(opts.stats), }); - } else { - return buildBackend({ - targetDir: paths.targetDir, - configPaths, - skipBuildDependencies: Boolean(opts.skipBuildDependencies), - }); } + return buildBackend({ + targetDir: paths.targetDir, + configPaths, + skipBuildDependencies: Boolean(opts.skipBuildDependencies), + }); } const roleInfo = getRoleInfo(role);