Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(assets): Only run the bundle step if the asset changed #31875

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
101 changes: 100 additions & 1 deletion packages/aws-cdk-lib/core/test/staging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from 'path';
import { testDeprecated } from '@aws-cdk/cdk-build-tools';
import * as fs from 'fs-extra';
import * as sinon from 'sinon';
import { FileAssetPackaging } from '../../cloud-assembly-schema';
import { FileAssetPackaging, Manifest, FileAsset, AssetManifest } from '../../cloud-assembly-schema';
import * as cxapi from '../../cx-api';
import { App, AssetHashType, AssetStaging, DockerImage, BundlingOptions, BundlingOutput, FileSystem, Stack, Stage, BundlingFileAccess } from '../lib';

Expand Down Expand Up @@ -1519,6 +1519,105 @@ describe('staging with docker cp', () => {
});
});

describe('staging with existing asset hash', () => {
let tempDir: string;
let app: App;
let stack: Stack;
let sourcePath: string;
let manifestFilePath: string;
let calculatedAssetHash: string;

beforeEach(() => {
// Create a temporary directory
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cdk-test-'));

// Create an App and Stack with the outdir set to the temporary directory
app = new App({ outdir: tempDir });
stack = new Stack(app, 'TestStack');

// Create a temporary source directory
sourcePath = path.join(tempDir, 'source');
fs.mkdirSync(sourcePath);
});

afterEach(() => {
// Clean up the temporary directory and source path
fs.removeSync(tempDir);
});

test('should skip bundling when asset hash already exists', () => {
let bundlingCalled = false;

// First, create an AssetStaging instance to calculate the asset hash
const stagingInitial = new AssetStaging(stack, 'AssetInitial', {
sourcePath,
bundling: {
image: DockerImage.fromRegistry('alpine'),
command: ['echo', 'Hello World'],
local: {
tryBundle(outputDir: string): boolean {
// Simulate bundling by writing a file to the output directory
fs.writeFileSync(path.join(outputDir, 'initial-dummy.txt'), 'dummy content');
return true;
},
},
},
});

calculatedAssetHash = stagingInitial.assetHash;

// Now, create the manifest content with the calculated asset hash
const fileAsset: FileAsset = {
source: {
path: stagingInitial.absoluteStagedPath, // Use the staged path from initial staging
packaging: FileAssetPackaging.ZIP_DIRECTORY,
},
destinations: {},
};
const assetManifest: AssetManifest = {
version: '0.0.0',
files: {
[calculatedAssetHash]: fileAsset,
},
};

// Write the manifest file to the expected location
const manifestFileName = `${app.synth().artifacts[0].id}.assets.json`;
manifestFilePath = path.join(tempDir, manifestFileName);
fs.writeJsonSync(manifestFilePath, assetManifest);

// WHEN
const staging = new AssetStaging(stack, 'Asset', {
sourcePath,
bundling: {
image: DockerImage.fromRegistry('alpine'),
command: ['echo', 'Hello World'],
local: {
tryBundle(outputDir: string): boolean {
bundlingCalled = true;
// Simulate bundling by writing a file to the output directory
fs.writeFileSync(path.join(outputDir, 'dummy.txt'), 'dummy content');
return true;
},
},
},
});

// THEN
expect(staging.assetHash).toEqual(calculatedAssetHash);

// Bundling should be skipped, so bundlingCalled should be false
expect(bundlingCalled).toEqual(false);

// Check that absoluteStagedPath is the same as initial staging's path
expect(staging.absoluteStagedPath).toEqual(stagingInitial.absoluteStagedPath);

// Packaging should be ZIP_DIRECTORY
expect(staging.packaging).toEqual(FileAssetPackaging.ZIP_DIRECTORY);
expect(staging.isArchive).toEqual(true);
});
});

// Reads a docker stub and cleans the volume paths out of the stub.
function readAndCleanDockerStubInput(file: string) {
return fs
Expand Down
Loading