Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Add cdn task to push static assets to S3
  • Loading branch information
jedfoster committed Feb 27, 2016
1 parent d348e4c commit 2e69f62
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -15,3 +15,4 @@ s3_website.yml
node_modules/
config/config.json
public/rev-manifest.json
.awspublish-static.sassmeister.com
7 changes: 6 additions & 1 deletion gulp/config.js
Expand Up @@ -54,7 +54,12 @@ module.exports = {
cssSrc: dest + '/css/*.css',
jsSrc: dest + '/js/*.js',
delSrc: [dest + '/js/*', dest + '/css/*'],
dest: dest
dest: dest,
cdn: {
headers: {
'Cache-Control': 'max-age=315360000, no-transform, public'
}
}
}
};

49 changes: 49 additions & 0 deletions gulp/tasks/cdn.js
@@ -0,0 +1,49 @@
var gulp = require('gulp');
var awspublish = require('gulp-awspublish');
var cloudfront = require('gulp-cloudfront-invalidate-aws-publish');
var gzip = require('gulp-gzip');
var config = require('../config').production

gulp.task('cdn', function(callback) {
if(! (process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY && process.env.AWS_S3_BUCKET && process.env.AWS_CLOUDFRONT_DISTRIBUTION_ID) ) {
return callback();
}

var aws = {
region: 'us-west-2',
key: process.env.AWS_ACCESS_KEY_ID,
secret: process.env.AWS_SECRET_ACCESS_KEY,
bucket: process.env.AWS_S3_BUCKET,
distribution: process.env.AWS_CLOUDFRONT_DISTRIBUTION_ID
};

var publisher = awspublish.create({
region: aws.region,
accessKeyId: aws.key,
secretAccessKey: aws.secret,
params: {
Bucket: aws.bucket
}
});

var cfSettings = {
distribution: aws.distribution,
accessKeyId: aws.key,
secretAccessKey: aws.secret
}

return gulp.src(config.dest + '/**')
.pipe(awspublish.gzip())

// publisher will add Content-Length, Content-Type and headers specified above
// If not specified it will set x-amz-acl to public-read by default
.pipe(publisher.publish(config.cdn.headers))

// print upload updates to console
.pipe(awspublish.reporter({
states: ['create', 'update']
}))

.pipe(cloudfront(cfSettings));
});

4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -40,9 +40,11 @@
"github-api": "https://github.com/jedfoster/github.git",
"gulp": "^3.8.11",
"gulp-autoprefixer": "^2.1.0",
"gulp-awspublish": "^3.0.1",
"gulp-batch-replace": "0.0.0",
"gulp-coffee": "^2.3.1",
"gulp-cloudfront-invalidate-aws-publish": "0.0.1",
"gulp-filesize": "0.0.6",
"gulp-gzip": "^1.2.0",
"gulp-jade": "^1.0.0",
"gulp-minify-css": "^0.4.6",
"gulp-mustache": "^2.2.0",
Expand Down

0 comments on commit 2e69f62

Please sign in to comment.