{"componentChunkName":"component---src-templates-post-tsx","path":"/posts/2019/08/gh-pages/","result":{"data":{"markdownRemark":{"fields":{"slug":"/2019/08/gh-pages/"},"frontmatter":{"title":"Publish static websites to GitHub Pages (or any Git) cleanly with gh-pages","tag":["gh-pages","GitHub Pages","heroku"],"image":"https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQFKi5a1hzYykg3TJiYcMvOJa2mjhRwSXxX2LCRPC-TfoUIc5TB"},"correctedDateEpoch":1565974800000,"html":"<p>This is as simple as <code>npm run build &#x26;&#x26; gh-pages -d dist</code>, and there will be no pollution from build remnants. You can safely as <code>/dist/</code> to <code>.gitignore</code>.</p>\n<p>The new branch called <code>gh-pages</code> will be created, with only built elements, no other code.</p>\n<p>See the package here <a href=\"https://www.npmjs.com/package/gh-pages\">https://www.npmjs.com/package/gh-pages</a></p>\n<!-- excerpt_separator -->\n<p>To publish elsewhere, e.g. <a href=\"https://www.heroku.com/\">Heroku</a>, it can be more complex. I resorted to a Node script, rather than CLI.</p>\n<pre><code class=\"language-js\">const ghPages = require(\"gh-pages\");\nconst fs = require(\"fs\");\nconst {spawnSync} = require(\"child_process\");\nconst rimraf = require(\"rimraf\");\n\nrimraf.sync(\"node_modules/gh-pages/.cache\")\n\nspawnSync(\"npm\", [\"run\", \"build\"], {\n  stdio: \"inherit\"\n});\n\nfs.writeFileSync(\"public/index.php\", \"&#x3C;?php header( 'Location: /index.html' ) ;  ?>\");\n\nghPages.publish(\"public\", {\n  branch: \"master\",\n  repo: \"https://git.heroku.com/&#x3C;BLAHBLAH>.git\"\n}, (err) => {\n  if (err) {\n    console.error(err);\n    return;\n  }\n\n  console.log(\"Posted to https://&#x3C;BLAHBLAH>.herokuapp.com\")\n});\n</code></pre>\n<p>One of the reasons is that Heroku requires a buildpack. At least a PHP file (<code>index.php</code>) with <code>&#x3C;?php header( 'Location: /index.html' ) ; ?></code> is sufficient to trigger this.</p>\n<p>In reality, what I am using this for? -- <a href=\"https://zhdiary.herokuapp.com\">https://zhdiary.herokuapp.com</a>, with <a href=\"https://cogearjs.org/docs\">Cogear.js</a></p>"}},"pageContext":{"slug":"/2019/08/gh-pages/"}}}