Pacharapol Withayasakpunt Pacharapol Withayasakpunt
Sat 17 August 2019
Publish static websites to GitHub Pages (or any Git) cleanly with gh-pages

Publish static websites to GitHub Pages (or any Git) cleanly with gh-pages

This is as simple as npm run build && gh-pages -d dist, and there will be no pollution from build remnants. You can safely as /dist/ to .gitignore.

The new branch called gh-pages will be created, with only built elements, no other code.

See the package here

gh-pages

gh-pages

Publish to a gh-pages branch on GitHub (or any other branch on any other remote)

To publish elsewhere, e.g. Heroku, it can be more complex. I resorted to a Node script, rather than CLI.

const ghPages = require("gh-pages");
const fs = require("fs");
const {spawnSync} = require("child_process");
const rimraf = require("rimraf");

rimraf.sync("node_modules/gh-pages/.cache")

spawnSync("npm", ["run", "build"], {
  stdio: "inherit"
});

fs.writeFileSync("public/index.php", "<?php header( 'Location: /index.html' ) ;  ?>");

ghPages.publish("public", {
  branch: "master",
  repo: "https://git.heroku.com/<BLAHBLAH>.git"
}, (err) => {
  if (err) {
    console.error(err);
    return;
  }

  console.log("Posted to https://<BLAHBLAH>.herokuapp.com")
});

One of the reasons is that Heroku requires a buildpack. At least a PHP file (index.php) with <?php header( 'Location: /index.html' ) ; ?> is sufficient to trigger this.

In reality, what I am using this for? -- https://zhdiary.herokuapp.com, with Cogear.js