Skip to content

Commit

Permalink
fix(assets): Only run the bundle step if the asset changed
Browse files Browse the repository at this point in the history
  • Loading branch information
florianPat committed Oct 24, 2024
1 parent d884c7c commit fa80e1c
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/aws-cdk-lib/core/lib/asset-staging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AssetBundlingVolumeCopy, AssetBundlingBindMount } from './private/asset
import { Cache } from './private/cache';
import { Stack } from './stack';
import { Stage } from './stage';
import * as assets from '../../cloud-assembly-schema';
import * as cxapi from '../../cx-api';

const ARCHIVE_EXTENSIONS = ['.tar.gz', '.zip', '.jar', '.tar', '.tgz'];
Expand Down Expand Up @@ -328,6 +329,15 @@ export class AssetStaging extends Construct {
? this.calculateHash(this.hashType, bundling)
: undefined;

if (undefined !== assetHash && this.assetHashAlreadyExists()) {
return {
assetHash: assetHash,
stagedPath: this.sourcePath,
packaging: FileAssetPackaging.ZIP_DIRECTORY,
isArchive: true,
};
}

const bundleDir = this.determineBundleDir(this.assetOutdir, assetHash);
this.bundle(bundling, bundleDir);

Expand Down Expand Up @@ -363,6 +373,40 @@ export class AssetStaging extends Construct {
};
}

private assetHashAlreadyExists(): boolean {
let stack = Stack.of(this);

while (undefined !== stack) {
if (!fs.existsSync(this.getAssetsManifestFilename(stack))) {
if (undefined !== stack.nestedStackParent) {
stack = stack.nestedStackParent;
}

// We are the root stack and there is no asset manifest.
// So we cannot do a lookup and will just bundle then...
return false;
}
}

const manifest = assets.Manifest.loadAssetManifest(this.getAssetsManifestFilename(stack));
for (const key of Object.keys(manifest.files ?? {})) {
if (key === this.assetHash) {
return true;
}
}

return false;
}

private getAssetsManifestFilename(stack: Stack): string {
return path.join(
process.cwd(),
Stage.of(stack)!.outdir,
path.sep,
stack.templateFile.replace('.template.json', '.assets.json'),
);
}

/**
* Whether staging has been disabled
*/
Expand Down

0 comments on commit fa80e1c

Please sign in to comment.