Skip to content

Commit 4712132

Browse files
committed
ci(gh-actions): cleanup deployment gulp, tests
1 parent 997c8b7 commit 4712132

File tree

4 files changed

+208
-69
lines changed

4 files changed

+208
-69
lines changed

.github/workflows/release.yml

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,16 @@ jobs:
2828
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
2929
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
3030
AWS_REGION: ${{ secrets.AWS_REGION }}
31-
32-
- name: Publush to NPM
33-
if: startsWith(github.ref, 'refs/tags/')
34-
run: npm publish
35-
env:
36-
NODE_AUTH_TOKEN: ${{ secrets.npm_token }}
37-
- name: Deploy docs
38-
uses: crazy-max/ghaction-github-pages@v2
39-
with:
40-
target_branch: gh-pages
41-
build_dir: example/build
42-
env:
43-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
# - name: Publush to NPM
32+
# if: startsWith(github.ref, 'refs/tags/')
33+
# run: npm publish
34+
# env:
35+
# NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
36+
# - name: Deploy docs
37+
# uses: crazy-max/ghaction-github-pages@v2
38+
# with:
39+
# target_branch: gh-pages
40+
# build_dir: example/build
41+
# env:
42+
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4443

Gulpfile.js

Lines changed: 126 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,119 @@
11
const gulp = require('gulp');
22
const sri = require('gulp-sri');
3-
const branch = require('git-branch');
4-
const pkgVersion = require('./package.json').version;
5-
const path = require('path');
6-
7-
// S3 CLIENT CONFIG
8-
const s3 = require('gulp-s3-upload')({ useIAM:true });
3+
const git = require('git-rev-sync');
4+
const pkg = require('./package.json');
5+
const { upload } = require('gulp-s3-publish');
6+
const { S3 } = require('aws-sdk');
7+
const util = require('util');
8+
const exec = util.promisify(require('child_process').exec);
99

1010
// DEPLOYMENT CONFIGURATION OPTIONS
11-
const source = ['dist/*.js', 'dist/*.map', 'dist/*.json']; // source for deploy
12-
const sourceSRI = ['dist/*.js', 'dist/*.map']; // source for sri generation
13-
const Bucket = 'static.filestackapi.com' // upload bucked
14-
const ACL = 'public-read'; // upload acl
15-
const deployPath = 'filestack-react'; // upload path
16-
const deploymentBranch = 'master'; // branch for upload production version
17-
const cacheControll = { // cache controll for each version
11+
const pkgName = pkg.name;
12+
const pkgCurrentVersion = pkg.version;
13+
14+
const source = ['dist/*.js', 'dist/*.js.map', 'dist/*.css', 'dist/*.json']; // source for deploy
15+
const sourceSRI = ['dist/*.js', 'dist/*.css']; // source for sri generation
16+
const bucket = process.env.DEPLOY_BUCKET || 'static.filestackapi.com' // upload bucked
17+
const betaBranch = process.env.BETA_BRANCH || 'develop';
18+
const dryRun = process.env.DRY_RUN || false;
19+
20+
const putObjectParams = {
21+
ACL: 'public-read'
22+
};
23+
const deployPath = pkgName; // upload path
24+
const cacheControl = { // cache controll for each version
1825
latest: 1,
1926
version: 30,
2027
beta: 0,
2128
};
2229

2330
// HELPERS
24-
const getDeployPath = (inputPath, version) => `${deployPath}/${version}/${path.basename(inputPath)}`;
31+
let currentTag;
32+
const currentBranch = git.branch();
33+
const isCi = process.env.CI || false;
34+
const forceDeploy = process.env.FORCE_DEPLOY || false;
35+
36+
try {
37+
currentTag = git.tag();
38+
} catch(e) {
39+
console.log('Current Git Tag not found. Beta will be released');
40+
}
41+
42+
// Get major version for "version deploy" ie: 1.x.x
2543
const getMajorVersion = (version) => version.split('.')[0];
2644

45+
// get current pkg version from npm (we dont need to update)
46+
const getCurrentReleasedVersion = async () => {
47+
const { stdout } = await exec(`npm view ${pkgName} version`);
48+
return stdout.trim();
49+
};
50+
51+
if (forceDeploy) {
52+
console.info('!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
53+
console.info('!!!!!!FORCE DEPLOYMENT!!!!!!');
54+
console.info('!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
55+
}
56+
57+
console.info('Current GIT Branch is', currentBranch);
58+
console.info(`Current GIT Tag is`, currentTag);
59+
console.info(`Is Continuous Integration Env`, isCi);
60+
61+
// S3
62+
const S3Client = new S3();
63+
64+
const uploadFile = (version, CacheControl) => {
65+
const options = {
66+
bucket,
67+
putObjectParams: {
68+
...putObjectParams,
69+
CacheControl: `max-age=${CacheControl * 86400}`,
70+
},
71+
uploadPath: `${deployPath}/${version}`,
72+
dryRun,
73+
};
74+
75+
console.info('Upload files with option:', options)
76+
77+
return upload(S3Client, options);
78+
}
79+
80+
/**
81+
* Check if we can deploy code to production CDN
82+
*
83+
* - pgk should be released only from CI
84+
* - pkg version should be different thant this on NPM repository
85+
* - git tag should be set
86+
* - current git tag should be equal to provided in package.json
87+
*/
88+
const canBeDeployedProd = async () => {
89+
if (!forceDeploy) {
90+
const currentVersion = await getCurrentReleasedVersion();
91+
92+
if (!isCi) {
93+
console.info('Publish can be run only from CI. You can bypass it using FORCE flag');
94+
return Promise.resolve(false);
95+
}
96+
97+
if (currentVersion === pkgCurrentVersion) {
98+
console.info(`Version ${pkgCurrentVersion} is already published (in npm). Skipping`);
99+
return Promise.resolve(false);
100+
}
101+
102+
if (!currentTag) {
103+
console.info('Current tag is missing');
104+
return Promise.resolve(false);
105+
}
106+
107+
if (currentTag !== pkgCurrentVersion) {
108+
console.info(`Package version ${pkgCurrentVersion} and GIT Tag (${currentTag}) are not equal. Skipping`);
109+
return Promise.resolve(false);
110+
}
111+
112+
}
113+
114+
return Promise.resolve(true);
115+
}
116+
27117
// GENERATE SRI TAG
28118
gulp.task('sri', () => {
29119
return gulp.src(sourceSRI)
@@ -42,46 +132,32 @@ gulp.task('sri', () => {
42132
});
43133

44134
// DEPLOYMENTS
45-
gulp.task('publish:beta', () => {
46-
return gulp.src(source)
47-
.pipe(s3({
48-
Bucket,
49-
ACL,
50-
CacheControl: `max-age=${cacheControll.beta * 86400}`,
51-
keyTransform: (path) => getDeployPath(path, 'beta'),
52-
}));
53-
});
135+
gulp.task('publish:beta', (done) => {
136+
// beta can be deployed only from provided branch
137+
if (currentBranch !== betaBranch) {
138+
console.warn(`Skipping publish:beta task. Incorrect branch ${currentBranch}. Beta can be released from ${betaBranch}`);
139+
return done();
140+
}
54141

55-
gulp.task('publish:latest', () => {
56-
return gulp.src(source)
57-
.pipe(s3({
58-
Bucket,
59-
ACL,
60-
CacheControl: `max-age=${cacheControll.latest * 86400}`,
61-
keyTransform: (path) => getDeployPath(path, `${getMajorVersion(pkgVersion)}.x.x`),
62-
}));
142+
return gulp.src(source).pipe(uploadFile('beta', cacheControl.beta))
63143
});
64144

65-
gulp.task('publish', gulp.series(() => {
66-
const currentBranch = branch.sync();
67-
console.info(`Current branch is "${currentBranch}"`)
68-
69-
if (currentBranch !== deploymentBranch) {
70-
return gulp.start('publish:beta');
145+
gulp.task('publish:latest', async () => {
146+
if (!(await canBeDeployedProd())) {
147+
console.warn('Skipping publish:latest task');
148+
return Promise.resolve();
71149
}
72150

73-
return gulp.src(source)
74-
.pipe(s3({
75-
Bucket,
76-
ACL,
77-
CacheControl: `max-age=${cacheControll.version * 86400}`,
78-
keyTransform: (path) => getDeployPath(path, pkgVersion),
79-
}));
80-
}, () => {
81-
const currentBranch = branch.sync();
82-
if (currentBranch !== deploymentBranch) {
83-
return;
151+
return gulp.src(source).pipe(uploadFile(`${getMajorVersion(pkgCurrentVersion)}.x.x`, cacheControl.latest));
152+
});
153+
154+
gulp.task('publish:version', async () => {
155+
if (!(await canBeDeployedProd())) {
156+
console.warn('Skipping publish:version task');
157+
return Promise.resolve();
84158
}
85159

86-
return gulp.start('publish:latest');
87-
}));
160+
return gulp.src(source).pipe(uploadFile(pkgCurrentVersion, cacheControl.version));
161+
});
162+
163+
gulp.task('publish', gulp.series('sri', 'publish:beta', 'publish:version', 'publish:latest'));

package-lock.json

Lines changed: 67 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"devDependencies": {
4747
"@testing-library/react": "^11.2.2",
4848
"@testing-library/react-hooks": "^3.4.2",
49+
"aws-sdk": "^2.808.0",
4950
"babel-eslint": "^10.0.3",
5051
"cross-env": "^7.0.2",
5152
"eslint": "^6.8.0",
@@ -59,10 +60,9 @@
5960
"eslint-plugin-react": "^7.17.0",
6061
"eslint-plugin-standard": "^4.0.1",
6162
"gh-pages": "^2.2.0",
62-
"git-branch": "^2.0.1",
63+
"git-rev-sync": "^3.0.1",
6364
"gulp": "^4.0.2",
64-
"gulp-s3-upload": "^1.7.3",
65-
"gulp-serve": "^1.4.0",
65+
"gulp-s3-publish": "^3.0.0",
6666
"gulp-sri": "^0.3.1",
6767
"microbundle-crl": "^0.13.10",
6868
"npm-run-all": "^4.1.5",

0 commit comments

Comments
 (0)