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

build: production builds need to be tagged as production #574

Merged
merged 2 commits into from
May 6, 2020
Merged
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
1 change: 1 addition & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ jobs:
env:
ALB_CERTIFICATE_ARN: ${{secrets.ALB_CERTIFICATE_ARN_PROD}}
CLOUDFRONT_CERTIFICATE_ARN: ${{secrets.CLOUDFRONT_CERTIFICATE_ARN_PROD}}
NODE_ENV: 'production'
23 changes: 15 additions & 8 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('FailedToFetch', { Bucket, Key }, e);
}
return null;
}

/**
* Deploy the built s3 assets into the Edge bucket
*
Expand All @@ -18,26 +28,23 @@ 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;
}
Expand Down