Skip to content

Instantly share code, notes, and snippets.

@dsadhanala
Last active December 16, 2018 05:09
Show Gist options
  • Save dsadhanala/0157697871892caaf2793170a61e6ebd to your computer and use it in GitHub Desktop.
Save dsadhanala/0157697871892caaf2793170a61e6ebd to your computer and use it in GitHub Desktop.
Auto deploy dist folder to gh-pages

Auto deploy dist folder to gh-pages branch

This gist helps auto deploy build artifacts into gh-pages branch without needing for interupting git history on the master branch. By taking advantage of npx (if you haven't heard about npx yet, read this blog post) to avoid creating another npm repo :).

Follow the below instructions.

Option 1:

  1. In you module package.json file add new script target as
  "deploy": "npx https://gist.github.com/dsadhanala/0157697871892caaf2793170a61e6ebd"
  1. Assuming you have a task that transpiles your code and place the beployable artifacts into dist folder, please name this target as release like below.
  "release": "your bundling tasks"
  1. Run npm run deploy

Option 2:

If you have exsiting build setup that creates build artifacts and place in to dist then do below.

  1. add new script target package.json as "release": "echo 'skip release'"
  2. run npx https://gist.github.com/dsadhanala/0157697871892caaf2793170a61e6ebd
#!/usr/bin/env node
/**
* automated gh-pages deployment
*/
const path = require('path');
const shell = require('shelljs');
const { which, echo, exit, exec, cd, mkdir, rm } = shell;
const distRoot = 'dist';
const currentRoot = path.join(process.cwd());
// check for git
if (!which('git')) {
echo('This script requires git, please install git!');
exit(1);
}
exec('git worktree list', { async: false }, (code, stdout, stderr) => {
cd(currentRoot);
mkdir('-p', distRoot);
// check if dist is already exist in work tree or not
// if yes, checkout and pull in latest
if (stdout.includes(distRoot)) {
echo(`======== found existing worktree for "dist", so pulling latest code ========`);
exec('cd dist; git checkout gh-pages && git pull', { async: false }, () => {
bundleAssets();
commitAndPushChanges();
});
return;
}
echo(`======== worktree doesn't exist, so creating now ========`);
// remove files from the dist directory
rm('-rf', 'dist/*');
// create a work tree and checkout gh-pages
exec('git worktree add dist gh-pages');
bundleAssets();
commitAndPushChanges();
});
/**
* Initiate the bundling process
*/
function bundleAssets() {
echo('======== prepare bundle ========');
cd(currentRoot);
exec('npm i');
exec('npm run release');
echo('======== complete bundle ========');
}
/**
* Commit and publish changes to remote gh-pages
*/
function commitAndPushChanges() {
echo('======== commit new build artifacts ========');
cd(currentRoot);
exec('cd dist; git add .');
exec('git commit -m "Build output as of $(git log --pretty=tformat:%H master -1 master -1)"');
echo('======== push code to gh-pages ========');
exec('git push origin gh-pages');
}
{
"name": "auto-deploy-gh-pages",
"version": "0.0.1",
"bin": "./index.js",
"author": "dsadhanala@gmail.com",
"dependencies": {
"shelljs": "^0.8.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment