Skip to content

Commit

Permalink
ci(gh-actions): cleanup deployment gulp, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pcholuj committed Dec 11, 2020
1 parent 997c8b7 commit 6949bde
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 69 deletions.
25 changes: 12 additions & 13 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@ jobs:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}

- name: Publush to NPM
if: startsWith(github.ref, 'refs/tags/')
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.npm_token }}
- name: Deploy docs
uses: crazy-max/ghaction-github-pages@v2
with:
target_branch: gh-pages
build_dir: example/build
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# - name: Publush to NPM
# if: startsWith(github.ref, 'refs/tags/')
# run: npm publish
# env:
# NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
# - name: Deploy docs
# uses: crazy-max/ghaction-github-pages@v2
# with:
# target_branch: gh-pages
# build_dir: example/build
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

176 changes: 126 additions & 50 deletions Gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,119 @@
const gulp = require('gulp');
const sri = require('gulp-sri');
const branch = require('git-branch');
const pkgVersion = require('./package.json').version;
const path = require('path');

// S3 CLIENT CONFIG
const s3 = require('gulp-s3-upload')({ useIAM:true });
const git = require('git-rev-sync');
const package = 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 source = ['dist/*.js', 'dist/*.map', 'dist/*.json']; // source for deploy
const sourceSRI = ['dist/*.js', 'dist/*.map']; // source for sri generation
const Bucket = 'static.filestackapi.com' // upload bucked
const ACL = 'public-read'; // upload acl
const deployPath = 'filestack-react'; // upload path
const deploymentBranch = 'master'; // branch for upload production version
const cacheControll = { // cache controll for each version
const pkgName = package.name;
const pkgCurrentVersion = package.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
const cacheControl = { // cache controll for each version
latest: 1,
version: 30,
beta: 0,
};

// HELPERS
const getDeployPath = (inputPath, version) => `${deployPath}/${version}/${path.basename(inputPath)}`;
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)
Expand All @@ -42,46 +132,32 @@ gulp.task('sri', () => {
});

// DEPLOYMENTS
gulp.task('publish:beta', () => {
return gulp.src(source)
.pipe(s3({
Bucket,
ACL,
CacheControl: `max-age=${cacheControll.beta * 86400}`,
keyTransform: (path) => getDeployPath(path, 'beta'),
}));
});
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();
}

gulp.task('publish:latest', () => {
return gulp.src(source)
.pipe(s3({
Bucket,
ACL,
CacheControl: `max-age=${cacheControll.latest * 86400}`,
keyTransform: (path) => getDeployPath(path, `${getMajorVersion(pkgVersion)}.x.x`),
}));
return gulp.src(source).pipe(uploadFile('beta', cacheControl.beta))
});

gulp.task('publish', gulp.series(() => {
const currentBranch = branch.sync();
console.info(`Current branch is "${currentBranch}"`)

if (currentBranch !== deploymentBranch) {
return gulp.start('publish:beta');
gulp.task('publish:latest', async () => {
if (!(await canBeDeployedProd())) {
console.warn('Skipping publish:latest task');
return Promise.resolve();
}

return gulp.src(source)
.pipe(s3({
Bucket,
ACL,
CacheControl: `max-age=${cacheControll.version * 86400}`,
keyTransform: (path) => getDeployPath(path, pkgVersion),
}));
}, () => {
const currentBranch = branch.sync();
if (currentBranch !== deploymentBranch) {
return;
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.start('publish:latest');
}));
return gulp.src(source).pipe(uploadFile(pkgCurrentVersion, cacheControl.version));
});

gulp.task('publish', gulp.series('sri', 'publish:beta', 'publish:version', 'publish:latest'));
70 changes: 67 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"devDependencies": {
"@testing-library/react": "^11.2.2",
"@testing-library/react-hooks": "^3.4.2",
"aws-sdk": "^2.808.0",
"babel-eslint": "^10.0.3",
"cross-env": "^7.0.2",
"eslint": "^6.8.0",
Expand All @@ -59,10 +60,9 @@
"eslint-plugin-react": "^7.17.0",
"eslint-plugin-standard": "^4.0.1",
"gh-pages": "^2.2.0",
"git-branch": "^2.0.1",
"git-rev-sync": "^3.0.1",
"gulp": "^4.0.2",
"gulp-s3-upload": "^1.7.3",
"gulp-serve": "^1.4.0",
"gulp-s3-publish": "^3.0.0",
"gulp-sri": "^0.3.1",
"microbundle-crl": "^0.13.10",
"npm-run-all": "^4.1.5",
Expand Down

0 comments on commit 6949bde

Please sign in to comment.