Created
February 10, 2018 22:36
-
-
Save derikwhittaker/a723b61ed0d716913239c78c52d8c898 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require("path"); | |
const gulp = require('gulp'); | |
const zip = require('gulp-zip'); | |
const run = require('gulp-run'); | |
const runSequence = require('run-sequence'); | |
const log = require('fancy-log'); | |
const AWS = require('aws-sdk'); | |
const s3 = new AWS.S3({apiVersion: '2006-03-01'}); | |
const s3Options = { | |
test: { | |
ebAppName: 'my-app-name', | |
ebEnvName: 'my-app-test', | |
bucketName: "my-bucket-name", | |
deployPackageName: "", | |
packageName: my-package.zip" | |
}, | |
active: null | |
}; | |
gulp.task('deploy-test', deployTest); | |
gulp.task('copy-to-bucket', copyToS3Bucket); | |
gulp.task('publish-version-to-eb', publishVersionToEB); | |
gulp.task('publish-environment-to-eb', publishEnvironmentToEB); | |
gulp.task('zip-build', zipBuild) | |
function copyToS3Bucket() { | |
const options = s3Options.active; | |
const packageNameAndLocation = "./deploy/" + options.deployPackageName; | |
const s3BucketPath = "s3://" + options.bucketName; | |
log.info("Copying package '" + packageNameAndLocation + "' to " + s3BucketPath); | |
return run("aws s3 cp " + packageNameAndLocation + " " + s3BucketPath ).exec(); | |
} | |
function buildDeploymentVersion(){ | |
const instance = new Date(); | |
const date = instance.getDate().toString().padStart("2", "0"); | |
const month = instance.getMonth().toString().padStart("2", "0"); | |
const year = instance.getFullYear().toString(); | |
const hours = instance.getHours().toString().padStart("2", "0"); | |
const minutes = instance.getMinutes().toString().padStart("2", "0"); | |
return date + month + year + hours + minutes; | |
} | |
function deployTest(done) { | |
const options = s3Options.test; | |
s3Options.active = options; | |
s3Options.active.deployPackageName = buildDeploymentVersion() + "-" + options.packageName; | |
log.info("Attempting build, package, and deploy for Test with - " + JSON.stringify(s3Options.active)); | |
runSequence('zip-build', | |
'copy-to-bucket', | |
'publish-version-to-eb', | |
'publish-environment-to-eb', | |
done); | |
} | |
function publishVersionToEB(){ | |
const options = s3Options.active; | |
log.info("Publishing product to App - '" + options.ebAppName + "' and Env - '" + options.ebEnvName + "'" ); | |
const createAppCommand = "aws elasticbeanstalk create-application-version --application-name " + options.ebAppName + " --version-label " + options.deployPackageName + " --source-bundle S3Bucket=" + options.bucketName + ",S3Key=" + options.deployPackageName + ""; | |
return run(createAppCommand).exec(); | |
} | |
function publishEnvironmentToEB(){ | |
const options = s3Options.active; | |
log.info("Publishing Environment to EB w/ App - '" + options.ebAppName + "' and Env - '" + options.ebEnvName + "'" ); | |
const updateEnvironmentCommand = "aws elasticbeanstalk update-environment --environment-name " + options.ebEnvName + " --version-label " + options.deployPackageName + ""; | |
return run(updateEnvironmentCommand).exec(); | |
} | |
function zipBuild(){ | |
const options = s3Options.active; | |
log.info("Packaging product to - " + options.deployPackageName); | |
// when zipping the files we want to grab all files and sub folders | |
// we want to omit our deploy and test files (no need for those in EB) | |
return gulp.src(["./**/*.*", | |
"!./node_modules/**/*.*", | |
"!./deploy/**/*.*", | |
"!./test/**/*.*" | |
]) | |
.pipe(zip(options.deployPackageName)) | |
.pipe(gulp.dest("deploy")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment