Skip to content

Commit 597b5cf

Browse files
committed
0 parents  commit 597b5cf

File tree

8 files changed

+4901
-0
lines changed

8 files changed

+4901
-0
lines changed

.babelrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"presets": [ [ "@textpress/babel-preset-textpress", {
3+
"targets": {
4+
"node": "6.10.2"
5+
}
6+
} ] ]
7+
}

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@textpress/eslint-config-textpress"
3+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib/

.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.*
2+
*.log
3+
src/
4+
gulpfile.js
5+
yalc.lock
6+
yarn.lock

gulpfile.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
require( "@textpress/gulp-bump-version" ).registerTask();

package.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "@textpress/gulp-lambda-deploy-from-s3",
3+
"version": "0.1.0",
4+
"description": "Initiates a CI deployment of a specified S3 build to AWS Lambda",
5+
"main": "lib/index.js",
6+
"repository": "github:textpress/gulp-lambda-deploy-from-s3",
7+
"license": "UNLICENSED",
8+
"scripts": {
9+
"bump": "gulp bump --part",
10+
"build": "yarn run build:clean && yarn run build:lib",
11+
"build:clean": "rimraf lib",
12+
"build:lib": "babel -d lib src --ignore '**/__tests__/**'",
13+
"build:flow": "flow-copy-source -v -i '**/__tests__/**' src lib",
14+
"ci-build": "yarn run build",
15+
"ci-release-notes": "conventional-github-releaser -p eslint"
16+
},
17+
"peerDependencies": {
18+
"gulp": "*"
19+
},
20+
"dependencies": {
21+
"aws-sdk": "*",
22+
"axios": "*",
23+
"bluebird": "*",
24+
"crypto": "*",
25+
"gulp-confirm": "*",
26+
"gulp-exec": "*",
27+
"gulp-print": "*",
28+
"gulp-util": "*",
29+
"lodash": "*",
30+
"through2": "*",
31+
"yargs": "*"
32+
},
33+
"devDependencies": {
34+
"@textpress/babel-preset-textpress": "*",
35+
"@textpress/eslint-config-textpress": "*",
36+
"@textpress/gulp-bump-version": "*",
37+
"babel-register": "^6.26.0",
38+
"conventional-github-releaser": "^2.0.0",
39+
"eslint": "^4.15.0",
40+
"flow-bin": "^0.63.1",
41+
"gulp": "^3.9.1",
42+
"rimraf": ">=2.6.1",
43+
"yargs": "^10.0.3"
44+
}
45+
}

src/index.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import AWS from "aws-sdk";
2+
import axios from "axios";
3+
import through from "through2";
4+
import Promise from "bluebird";
5+
import crypto from "crypto";
6+
import confirm from "gulp-confirm";
7+
import gulp from "gulp";
8+
import isString from "lodash/isString";
9+
import fs from "fs";
10+
import path from "path";
11+
import yargs from "yargs";
12+
13+
14+
export function readPackageJson() {
15+
return JSON.parse( fs.readFileSync( path.join( process.cwd(), "package.json" ) ) );
16+
}
17+
18+
19+
export function getDeploySignature( body ) {
20+
const ssm = new AWS.SSM( { region: "us-east-1" } );
21+
const getParameter = Promise.promisify( ssm.getParameter, { context: ssm } );
22+
return getParameter( { Name: "/textpress-ci/signatureSecret", WithDecryption: true } )
23+
.then( result => {
24+
const signatureSecret = result.Parameter.Value;
25+
if ( !signatureSecret )
26+
throw new Error( "Could not obtain signature secret" );
27+
28+
const hmac = "sha1";
29+
const hash = crypto.createHmac( hmac, signatureSecret )
30+
.update( body )
31+
.digest( "hex" );
32+
return `${hmac}=${hash}`;
33+
} );
34+
}
35+
36+
37+
export function makeDeployRequest( body ) {
38+
return getDeploySignature( body )
39+
.then( singature => {
40+
const client = axios.create( {
41+
baseURL: "https://zp1v6aciyk.execute-api.us-east-1.amazonaws.com",
42+
headers: { "X-Hub-Signature": singature }
43+
} );
44+
45+
return client.post( "/production/deploy-from-s3", body );
46+
} );
47+
}
48+
49+
50+
export function deploy( argv ) {
51+
const version = argv.version;
52+
if ( !isString( version ) )
53+
throw new Error( "Version is missing, usage: yarn deploy -v 0.0.1 -s staging" );
54+
55+
const stage = argv.stage === true ? "staging" : argv.stage;
56+
if ( [ "development", "staging", "production" ].indexOf( stage ) === -1 )
57+
throw new Error( "Stage is missing, usage: yarn deploy -v 0.0.1 -s staging" );
58+
59+
const packageJson = readPackageJson();
60+
return gulp.src( "" )
61+
.pipe( confirm( {
62+
question: `\x1B[37mDeploy version \x1B[4m\x1B[36m${version}\x1B[24m\x1B[37m to \x1B[4m\x1B[36m${stage}\x1B[24m\x1B[37m?\x1B[22m`,
63+
input: "_key:y"
64+
} ) )
65+
.pipe( through.obj( function ( chunk, enc, cb ) {
66+
const _this = this;
67+
68+
const body = JSON.stringify( {
69+
repository: `${packageJson.author}/${packageJson.name}`,
70+
version,
71+
stage
72+
} );
73+
74+
makeDeployRequest( body )
75+
.then( () => { cb(); } )
76+
.catch( x => {
77+
_this.emit( "error", x );
78+
cb();
79+
} );
80+
} ) );
81+
}
82+
83+
84+
export function registerTask() {
85+
gulp.task( "deploy", () => deploy( yargs.alias( "v", "version" ).alias( "s", "stage" ).argv ) );
86+
}
87+
88+
export default deploy;

0 commit comments

Comments
 (0)