Merge pull request #2299 from spotify/rugvip/db

cli: forward docker args to backend:build-image, add --build flag, and use by default
This commit is contained in:
Patrik Oldsberg
2020-09-07 13:48:37 +02:00
committed by GitHub
14 changed files with 103 additions and 40 deletions
@@ -15,28 +15,64 @@
*/
import fs from 'fs-extra';
import { join as joinPath, relative as relativePath } from 'path';
import { createDistWorkspace } from '../../lib/packager';
import { paths } from '../../lib/paths';
import { run } from '../../lib/run';
import { Command } from 'commander';
const PKG_PATH = 'package.json';
export default async (imageTag: string) => {
export default async (cmd: Command) => {
// Skip the preparation steps if we're being asked for help
if (cmd.args.includes('--help')) {
await run('docker', ['image', 'build', '--help']);
return;
}
const pkgPath = paths.resolveTarget(PKG_PATH);
const pkg = await fs.readJson(pkgPath);
const appConfigs = await findAppConfigs();
const tempDistWorkspace = await createDistWorkspace([pkg.name], {
buildDependencies: Boolean(cmd.build),
files: [
'package.json',
'yarn.lock',
'app-config.yaml',
...appConfigs,
{ src: paths.resolveTarget('Dockerfile'), dest: 'Dockerfile' },
],
});
console.log(`Dist workspace ready at ${tempDistWorkspace}`);
await run('docker', ['build', '.', '-t', imageTag], {
// all args are forwarded to docker build
await run('docker', ['image', 'build', '.', ...cmd.args], {
cwd: tempDistWorkspace,
});
await fs.remove(tempDistWorkspace);
};
/**
* Find all config files to copy into the image
*/
async function findAppConfigs(): Promise<string[]> {
const files = [];
for (const name of await fs.readdir(paths.targetRoot)) {
if (name.startsWith('app-config.') && name.endsWith('.yaml')) {
files.push(name);
}
}
if (paths.targetRoot !== paths.targetDir) {
const dirPath = relativePath(paths.targetRoot, paths.targetDir);
for (const name of await fs.readdir(paths.targetDir)) {
if (name.startsWith('app-config.') && name.endsWith('.yaml')) {
files.push(joinPath(dirPath, name));
}
}
}
return files;
}
+5 -2
View File
@@ -36,9 +36,12 @@ export function registerCommands(program: CommanderStatic) {
.action(lazy(() => import('./backend/build').then(m => m.default)));
program
.command('backend:build-image <image-tag>')
.command('backend:build-image')
.allowUnknownOption(true)
.helpOption(', --backstage-cli-help') // Let docker handle --help
.option('--build', 'Build packages before packing them into the image')
.description(
'Builds a docker image from the package, with all local deps included',
'Bundles the package into a docker image. All extra args are forwarded to docker image build',
)
.action(lazy(() => import('./backend/buildImage').then(m => m.default)));
+12
View File
@@ -48,6 +48,11 @@ type Options = {
* Defaults to ['yarn.lock', 'package.json'].
*/
files?: FileEntry[];
/**
* If set to true, the target packages are built before they are packaged into the workspace.
*/
buildDependencies?: boolean;
};
/**
@@ -68,6 +73,13 @@ export async function createDistWorkspace(
const targets = await findTargetPackages(packageNames);
if (options.buildDependencies) {
const scopeArgs = targets.flatMap(target => ['--scope', target.name]);
await run('yarn', ['lerna', 'run', ...scopeArgs, 'build'], {
cwd: paths.targetRoot,
});
}
await moveToDistWorkspace(targetDir, targets);
const files: FileEntry[] = options.files ?? ['yarn.lock', 'package.json'];