-
Notifications
You must be signed in to change notification settings - Fork 41
/
Gulpfile.js
165 lines (136 loc) · 4.59 KB
/
Gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* eslint-disable */
const gulp = require('gulp');
const sri = require('gulp-sri');
const git = require('git-rev-sync');
const pkg = require('./package.json');
const { upload } = require('gulp-s3-publish');
const { S3 } = require('aws-sdk');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
// DEPLOYMENT CONFIGURATION OPTIONS
const pkgName = pkg.name;
const pkgCurrentVersion = pkg.version;
const source = ['dist/*.js', 'dist/*.js.map', 'dist/*.css', 'dist/*.json']; // source for deploy
const sourceSRI = ['dist/*.js', 'dist/*.css']; // source for sri generation
const bucket = process.env.DEPLOY_BUCKET || 'static.filestackapi.com'; // upload bucked
const betaBranch = process.env.BETA_BRANCH || 'develop';
const dryRun = process.env.DRY_RUN || false;
const putObjectParams = {
ACL: 'public-read'
};
const deployPath = pkgName; // upload path
// cache controll for each version
const cacheControl = {
latest: 1,
version: 30,
beta: 0,
};
// HELPERS
let currentTag;
const currentBranch = git.branch();
const isCi = process.env.CI || false;
const forceDeploy = process.env.FORCE_DEPLOY || false;
try {
currentTag = git.tag();
} catch(e) {
console.log('Current Git Tag not found. Beta will be released');
}
// Get major version for "version deploy" ie: 1.x.x
const getMajorVersion = (version) => version.split('.')[0];
// get current pkg version from npm (we dont need to update)
const getCurrentReleasedVersion = async () => {
const { stdout } = await exec(`npm view ${pkgName} version`);
return stdout.trim();
};
if (forceDeploy) {
console.info('!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
console.info('!!!!!!FORCE DEPLOYMENT!!!!!!');
console.info('!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
}
console.info('Current GIT Branch is', currentBranch);
console.info(`Current GIT Tag is`, currentTag);
console.info(`Is Continuous Integration Env`, isCi);
// S3
const S3Client = new S3();
const uploadFile = (version, CacheControl) => {
const options = {
bucket,
putObjectParams: {
...putObjectParams,
CacheControl: `max-age=${CacheControl * 86400}`,
},
uploadPath: `${deployPath}/${version}`,
dryRun
};
console.info('Upload files with option:', options);
return upload(S3Client, options);
}
/**
* Check if we can deploy code to production CDN
*
* - pgk should be released only from CI
* - pkg version should be different thant this on NPM repository
* - git tag should be set
* - current git tag should be equal to provided in package.json
*/
const canBeDeployedProd = async () => {
if (!forceDeploy) {
const currentVersion = await getCurrentReleasedVersion();
if (!isCi) {
console.info('Publish can be run only from CI. You can bypass it using FORCE flag');
return Promise.resolve(false);
}
if (currentVersion === pkgCurrentVersion) {
console.info(`Version ${pkgCurrentVersion} is already published (in npm). Skipping`);
return Promise.resolve(false);
}
if (!currentTag) {
console.info('Current tag is missing');
return Promise.resolve(false);
}
if (currentTag !== pkgCurrentVersion) {
console.info(`Package version ${pkgCurrentVersion} and GIT Tag (${currentTag}) are not equal. Skipping`);
return Promise.resolve(false);
}
}
return Promise.resolve(true);
}
// GENERATE SRI TAG
gulp.task('sri', () => {
return gulp.src(sourceSRI)
.pipe(sri({
fileName: 'dist/manifest.json',
transform: (o) => {
let newOb = {};
for (const el in o) {
newOb[el.replace('dist/', '')] = { sri: o[el] };
};
return newOb;
}
}))
.pipe(gulp.dest('.'));
});
// DEPLOYMENTS
gulp.task('publish:beta', (done) => {
// beta can be deployed only from provided branch
if (currentBranch !== betaBranch) {
console.warn(`Skipping publish:beta task. Incorrect branch ${currentBranch}. Beta can be released from ${betaBranch}`);
return done();
}
return gulp.src(source).pipe(uploadFile('beta', cacheControl.beta));
});
gulp.task('publish:latest', async () => {
if (!(await canBeDeployedProd())) {
console.warn('Skipping publish:latest task');
return Promise.resolve();
}
return gulp.src(source).pipe(uploadFile(`${getMajorVersion(pkgCurrentVersion)}.x.x`, cacheControl.latest));
});
gulp.task('publish:version', async () => {
if (!(await canBeDeployedProd())) {
console.warn('Skipping publish:version task');
return Promise.resolve();
}
return gulp.src(source).pipe(uploadFile(pkgCurrentVersion, cacheControl.version));
});
gulp.task('publish', gulp.series('sri', 'publish:beta', 'publish:version', 'publish:latest'));