Skip to content

Commit

Permalink
feat(cli): add option to ignore no stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
msambol committed Dec 15, 2023
1 parent 7c62d68 commit 0f16f7a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 6 deletions.
11 changes: 9 additions & 2 deletions packages/aws-cdk/lib/api/cxapp/cloud-assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,16 @@ export interface SelectStacksOptions {
extend?: ExtendedStackSelection;

/**
* The behavior if if no selectors are privided.
* The behavior if if no selectors are provided.
*/
defaultBehavior: DefaultSelection;

/**
* Ignore the error message if the app contains no stacks.
*
* @default false
*/
ignoreNoStacks?: boolean;
}

/**
Expand Down Expand Up @@ -99,7 +106,7 @@ export class CloudAssembly {
const allTopLevel = selector.allTopLevel ?? false;
const patterns = sanitizePatterns(selector.patterns);

if (stacks.length === 0) {
if (stacks.length === 0 && !options.ignoreNoStacks) {
throw new Error('This app contains no stacks');
}

Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk/lib/api/deploy-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ export interface DeployStackOptions {
* @default true To remain backward compatible.
*/
readonly assetParallelism?: boolean;

/**
* Ignore the error message if the app contains no stacks.
*
* @default false
*/
ignoreNoStacks?: boolean;
}

export type DeploymentMethod =
Expand Down
7 changes: 7 additions & 0 deletions packages/aws-cdk/lib/api/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ export interface DeployStackOptions {
* @default true To remain backward compatible.
*/
readonly assetParallelism?: boolean;

/**
* Ignore the error message if the app contains no stacks.
*
* @default false
*/
ignoreNoStacks?: boolean;
}

interface AssetOptions {
Expand Down
17 changes: 14 additions & 3 deletions packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ export class CdkToolkit {
}

const startSynthTime = new Date().getTime();
const stackCollection = await this.selectStacksForDeploy(options.selector, options.exclusively, options.cacheCloudAssembly);
// eslint-disable-next-line max-len
const stackCollection = await this.selectStacksForDeploy(options.selector, options.exclusively, options.cacheCloudAssembly, options.ignoreNoStacks);
const elapsedSynthTime = new Date().getTime() - startSynthTime;
print('\n✨ Synthesis time: %ss\n', formatTime(elapsedSynthTime));

Expand Down Expand Up @@ -305,6 +306,7 @@ export class CdkToolkit {
hotswap: options.hotswap,
extraUserAgent: options.extraUserAgent,
assetParallelism: options.assetParallelism,
ignoreNoStacks: options.ignoreNoStacks,
});

const message = result.noOp
Expand Down Expand Up @@ -479,7 +481,7 @@ export class CdkToolkit {
}

public async import(options: ImportOptions) {
const stacks = await this.selectStacksForDeploy(options.selector, true, true);
const stacks = await this.selectStacksForDeploy(options.selector, true, true, false);

if (stacks.stackCount > 1) {
throw new Error(`Stack selection is ambiguous, please choose a specific stack for import [${stacks.stackArtifacts.map(x => x.id).join(', ')}]`);
Expand Down Expand Up @@ -729,11 +731,13 @@ export class CdkToolkit {
return stacks;
}

private async selectStacksForDeploy(selector: StackSelector, exclusively?: boolean, cacheCloudAssembly?: boolean): Promise<StackCollection> {
// eslint-disable-next-line max-len
private async selectStacksForDeploy(selector: StackSelector, exclusively?: boolean, cacheCloudAssembly?: boolean, ignoreNoStacks?: boolean): Promise<StackCollection> {
const assembly = await this.assembly(cacheCloudAssembly);
const stacks = await assembly.selectStacks(selector, {
extend: exclusively ? ExtendedStackSelection.None : ExtendedStackSelection.Upstream,
defaultBehavior: DefaultSelection.OnlySingle,
ignoreNoStacks,
});

this.validateStacksSelected(stacks, selector.patterns);
Expand Down Expand Up @@ -1134,6 +1138,13 @@ export interface DeployOptions extends CfnDeployOptions, WatchOptions {
* @default AssetBuildTime.ALL_BEFORE_DEPLOY
*/
readonly assetBuildTime?: AssetBuildTime;

/**
* Ignore the error message if the app contains no stacks.
*
* @default false
*/
readonly ignoreNoStacks?: boolean;
}

export interface ImportOptions extends CfnDeployOptions {
Expand Down
4 changes: 3 additions & 1 deletion packages/aws-cdk/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ async function parseCommandLineArguments(args: string[]) {
})
.option('concurrency', { type: 'number', desc: 'Maximum number of simultaneous deployments (dependency permitting) to execute.', default: 1, requiresArg: true })
.option('asset-parallelism', { type: 'boolean', desc: 'Whether to build/publish assets in parallel' })
.option('asset-prebuild', { type: 'boolean', desc: 'Whether to build all assets before deploying the first stack (useful for failing Docker builds)', default: true }),
.option('asset-prebuild', { type: 'boolean', desc: 'Whether to build all assets before deploying the first stack (useful for failing Docker builds)', default: true })
.option('ignore-no-stacks', { type: 'boolean', desc: 'Ignore the error message if the app contains no stacks', default: false }),
)
.command('import [STACK]', 'Import existing resource(s) into the given STACK', (yargs: Argv) => yargs
.option('execute', { type: 'boolean', desc: 'Whether to execute ChangeSet (--no-execute will NOT execute the ChangeSet)', default: true })
Expand Down Expand Up @@ -583,6 +584,7 @@ export async function exec(args: string[], synthesizer?: Synthesizer): Promise<n
concurrency: args.concurrency,
assetParallelism: configuration.settings.get(['assetParallelism']),
assetBuildTime: configuration.settings.get(['assetPrebuild']) ? AssetBuildTime.ALL_BEFORE_DEPLOY : AssetBuildTime.JUST_IN_TIME,
ignoreNoStacks: args.ignoreNoStacks,
});

case 'import':
Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ export class Settings {
notices: argv.notices,
assetParallelism: argv['asset-parallelism'],
assetPrebuild: argv['asset-prebuild'],
ignoreNoStacks: argv['ignore-no-stacks'],
});
}

Expand Down
8 changes: 8 additions & 0 deletions packages/aws-cdk/test/api/cloud-assembly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ async function testCloudAssembly({ env }: { env?: string, versionReporting?: boo
return cloudExec.synthesize();
}

async function testCloudAssemblyNoStacks() {
const cloudExec = new MockCloudExecutable({
stacks: [],
});

return cloudExec.synthesize();
}

async function testNestedCloudAssembly({ env }: { env?: string, versionReporting?: boolean } = {}) {
const cloudExec = new MockCloudExecutable({
stacks: [{
Expand Down

0 comments on commit 0f16f7a

Please sign in to comment.