Skip to content

Commit 234f3c7

Browse files
Add script to deploy artifacts to S3 and run on CI
1 parent c8d86cc commit 234f3c7

File tree

5 files changed

+242
-22
lines changed

5 files changed

+242
-22
lines changed

.drone.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@ pipeline:
44
commands:
55
- yarn
66
- yarn build
7+
8+
deploy:
9+
image: node:8.9.4
10+
commands:
11+
- yarn deploy
12+
when:
13+
branch: master

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"sass": "src/csh-material-bootstrap.scss",
2020
"scripts": {
2121
"build": "node tools/build.js",
22-
"start": "node tools/build.js --watch"
22+
"start": "node tools/build.js --watch",
23+
"deploy": "node tools/deploy.js"
2324
},
2425
"dependencies": {},
2526
"peerDependencies": {
@@ -28,15 +29,18 @@
2829
},
2930
"devDependencies": {
3031
"autoprefixer": "^7.2.5",
32+
"aws-sdk": "^2.183.0",
3133
"chalk": "^2.3.0",
3234
"chokidar": "^2.0.0",
3335
"cssnano": "^3.10.0",
36+
"fs-extra": "^5.0.0",
37+
"globby": "^7.1.1",
38+
"mime-types": "^2.1.17",
3439
"node-sass": "^4.7.2",
3540
"ora": "^1.3.0",
3641
"postcss": "^6.0.16",
3742
"postcss-cli": "^4.1.1",
3843
"postcss-flexbugs-fixes": "^3.2.0",
39-
"rimraf": "^2.6.2",
4044
"shelljs": "^0.8.0",
4145
"yargs": "^10.1.1",
4246
"bootstrap": "4.0.0-beta.3",

tools/build.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const path = require('path');
2+
const fs = require('fs-extra');
23
const shell = require('shelljs');
34
const chalk = require('chalk');
45
const chokidar = require('chokidar');
5-
const rimraf = require('rimraf');
66
const spinner = require('ora')();
77
const version = require('../package.json').version;
88

@@ -38,15 +38,6 @@ function exec(command) {
3838
));
3939
}
4040

41-
function clean(glob) {
42-
return new Promise((resolve, reject) => rimraf(glob, {}, (error) => {
43-
if (error) {
44-
return reject(error);
45-
}
46-
resolve();
47-
}));
48-
}
49-
5041
function fail(error, message) {
5142
spinner.fail(chalk.bold.red(`${message}\n${error}`));
5243
shell.exit(1);
@@ -55,7 +46,7 @@ function fail(error, message) {
5546
async function build() {
5647
try {
5748
spinner.start('Clean Build Artifacts');
58-
await clean(config.dist);
49+
await fs.remove(config.dist);
5950
spinner.succeed(chalk.green(spinner.text));
6051

6152
spinner.start('Compile SCSS to CSS');

tools/deploy.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const path = require('path');
2+
const mime = require('mime-types');
3+
const fs = require('fs-extra');
4+
const shell = require('shelljs');
5+
const aws = require('aws-sdk');
6+
const globby = require('globby');
7+
const chalk = require('chalk');
8+
const spinner = require('ora')();
9+
const metadata = require('../package.json');
10+
11+
aws.config.update({
12+
s3ForcePathStyle: true,
13+
endpoint: 'https://s3.csh.rit.edu'
14+
});
15+
const s3 = new aws.S3();
16+
17+
const config = {
18+
root: path.resolve(__dirname, '..'),
19+
artifacts: [
20+
'index.html',
21+
'dist/*',
22+
'src/*'
23+
],
24+
bucket: metadata.name,
25+
dest: metadata.version,
26+
acl: 'public-read'
27+
};
28+
29+
function upload(srcPath, destPath) {
30+
return fs.readFile(srcPath)
31+
.then(data => {
32+
const params = {
33+
Bucket: config.bucket,
34+
Key: destPath,
35+
Body: data,
36+
ACL: config.acl
37+
};
38+
39+
const mimeType = mime.lookup(srcPath);
40+
if (mimeType) {
41+
params['ContentType'] = mimeType;
42+
}
43+
44+
return s3.upload(params).promise();
45+
});
46+
}
47+
48+
async function deploy() {
49+
const uploaders = [];
50+
51+
for (let artifact of config.artifacts) {
52+
const filenames = await globby(path.resolve(config.root, artifact));
53+
54+
for (let filename of filenames) {
55+
let destPath = `${config.dest}/${path.dirname(artifact)}/${path.basename(filename)}`;
56+
57+
if (path.dirname(artifact) === '.') {
58+
destPath = `${config.dest}/${path.basename(filename)}`;
59+
}
60+
61+
uploaders.push(
62+
upload(filename, destPath)
63+
);
64+
}
65+
}
66+
67+
return Promise.all(uploaders);
68+
}
69+
70+
function fail(error, message) {
71+
if (typeof error === 'string') {
72+
spinner.fail(chalk.bold.red(error));
73+
} else {
74+
spinner.fail(chalk.bold.red(`${message}\n${error}`))
75+
}
76+
77+
shell.exit(1);
78+
}
79+
80+
Promise.resolve()
81+
.then(() => fs.pathExists(path.resolve(config.root, 'dist')))
82+
.then(exists => {
83+
if (!exists) {
84+
fail('You must build the project before deploying');
85+
}
86+
})
87+
.then(() => {
88+
spinner.start('Upload Artifacts to S3');
89+
return deploy();
90+
})
91+
.then(() => {
92+
spinner.succeed(chalk.green(spinner.text));
93+
})
94+
.catch(error => {
95+
fail(error, 'Failed to deploy artifacts to S3')
96+
});

0 commit comments

Comments
 (0)