Skip to content

Commit

Permalink
feat(toolkit): ensure consistent zip files
Browse files Browse the repository at this point in the history
Zip files were not consistent across deploys resulting in unnecessary S3 uploads and stack updates.

Ensure consistency by appending files in series (guarantees file ordering in the zip) and reseting
dates (guarantess same hash for same content).

Closes aws#1997, Closes aws#2759
  • Loading branch information
jogold committed Jun 19, 2019
1 parent 83eee09 commit 6469782
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
26 changes: 21 additions & 5 deletions packages/aws-cdk/lib/archive.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
import archiver = require('archiver');
import crypto = require('crypto');
import fs = require('fs-extra');
import glob = require('glob');
import path = require('path');

export function zipDirectory(directory: string, outputFile: string): Promise<void> {
return new Promise((ok, fail) => {
const output = fs.createWriteStream(outputFile);
const archive = archiver('zip');

// The below options are needed to support following symlinks when building zip files:
// - nodir: This will prevent symlinks themselves from being copied into the zip.
// - nodir: This will prevent symlinks themselves from being copied into the zip.
// - follow: This will follow symlinks and copy the files within.
const globOptions = {
dot: true,
nodir: true,
follow: true,
cwd: directory
cwd: directory,
};
archive.glob('**', globOptions);
archive.pipe(output);
archive.finalize();
const files = glob.sync('**', globOptions); // The output here is already sorted

output.on('open', async () => {
archive.pipe(output);

await files.reduce(async (acc, file) => { // Append files serially to ensure file order
await acc;
const data = await fs.readFile(path.join(directory, file));
archive.append(data, {
name: file,
date: new Date(0), // reset dates to get the same hash for the same content
});
}, Promise.resolve());

archive.finalize();
});

archive.on('warning', fail);
archive.on('error', fail);
Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"colors": "^1.3.3",
"decamelize": "^3.2.0",
"fs-extra": "^8.0.1",
"glob": "^7.1.4",
"json-diff": "^0.5.4",
"minimatch": ">=3.0",
"promptly": "^3.0.3",
Expand Down

0 comments on commit 6469782

Please sign in to comment.