Skip to content

Commit

Permalink
build: dont fail to deploy if old landing page does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
blacha committed May 6, 2020
1 parent 437be67 commit 194ba23
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions packages/landing/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ const HashKey = 'linz-hash';
const s3 = new AWS.S3({ region: 'us-east-1' });
const cf = new AWS.CloudFormation({ region: 'us-east-1' });

async function getHash(Bucket, Key) {
try {
const obj = await s3.getObject({ Bucket, Key }).promise();
return obj.Metadata[HashKey];
} catch (e) {
console.log(e);
}
return null;
}

/**
* Deploy the built s3 assets into the Edge bucket
*
Expand All @@ -18,30 +28,28 @@ const cf = new AWS.CloudFormation({ region: 'us-east-1' });
async function deploy() {
// Since the bucket is generated inside of CDK lets look up the bucket name
const stackInfo = await cf.describeStacks({ StackName: 'Edge' }).promise();
const bucket = stackInfo.Stacks[0].Outputs.find(f => f.OutputKey == 'CloudFrontBucket');
const bucket = stackInfo.Stacks[0].Outputs.find((f) => f.OutputKey == 'CloudFrontBucket');
if (bucket == null) throw new Error('Failed to find EdgeBucket');

const s3BucketName = bucket.OutputValue;

const allBuckets = await s3.listBuckets().promise();
const s3Bucket = allBuckets.Buckets.find(f => f.Name == s3BucketName);
const s3Bucket = allBuckets.Buckets.find((f) => f.Name == s3BucketName);
if (s3Bucket == null) throw new Error('Failed to locate edge bucket in current account');

const files = await fs.readdir(DistDir);
for (const fileName of files) {
const fileData = await fs.readFile(`${DistDir}/${fileName}`);
const hash = crypto
.createHash('sha512')
.update(fileData)
.digest('base64');
const hash = crypto.createHash('sha512').update(fileData).digest('base64');

const obj = await s3.getObject({ Bucket: s3BucketName, Key: fileName }).promise();
const oldHash = await getHash(s3BucketName, fileName);
// Only upload files if they have changed
if (obj.Metadata[HashKey] == hash) {
if (oldHash == hash) {
console.log('Skipped', fileName, `${(fileData.byteLength / 1024).toFixed(2)}Kb`, hash);
continue;
}

console.log(oldHash);
break;
console.log('Uploading', fileName, `${(fileData.byteLength / 1024).toFixed(2)}Kb`, hash);
const res = await s3
.putObject({
Expand Down

0 comments on commit 194ba23

Please sign in to comment.