Publish your Gatsby website for free to github pages
Posted
Updated
Europe’s developer-focused job platform
Let companies apply to you
Developer-focused, salary and tech stack upfront.
Just one profile, no job applications!
GitHub Pages is a service offered by GitHub, which allows you to host websites straight from the repository. With a few configurations a Gatsby site can be hosted on GitHub Pages.
There are several ways to publish to Github Pages:
username.github.io/reponame/
or /build
username.github.io
or orgname.github.io
💰 The Pragmatic Programmer: journey to mastery. 💰 One of the best books in software development, sold over 200,000 times.
You have to select which branch will be deployed from your repository in Github. Navigate to your site’s repository and update the branch in the Github Pages Section under settings.
Select main
for publishing to the root subdomain or gh-pages
for publishing to a path.
The easiest way to publish a Gatsby app to Github pages is with the package gh-pages
, see npm gh-pages.
npm install gh-pages --save-dev
A custom script in your package.json makes it easier to build your site and move the contents of the built files to the proper branch for GitHub Pages. This also helps to automate that process in the future in a CI/CD pipeline.
If you choose to deploy at a path like username.github.io/reponame/
, you have to use the ---prefix-paths
flag, because the website will end up in a folder username.github.io/reponame/
.
In this case the path has to be prefixed with /reponame
in the gatsby-config.js
.
Add path prefix in gatsby-config.js
.
module.exports = {
pathPrefix: '/reponame',
};
Then add a deploy script to package.json
:
{
"scripts": {
"deploy": "gatsby build --prefix-paths && gh-pages -d public"
}
}
When you run npm run deploy
all contents of the public folder will be moved to your repository’s gh-pages branch.
If you choose to deploy to a repository like username.github.io
, you don’t have to prefix the path and your website needs to be pushed to the main branch.
GitHub Pages forces deployment of user/organization pages to the main branch.
So if you use main for development you need to do one of these:
source
with the command git checkout -b source main
and,
change the default branch from main to source.Then add a deploy script to package.json
:
{
"scripts": {
"deploy": "gatsby build && gh-pages -d public -b main"
}
}
After running npm run deploy
you should see your website at username.github.io
(it could take a few minutes).
If you use a custom domain, prefixing the path is not required. Path prefixing is only necessary when the site is not at the root of the domain.
Add a file named CNAME
to the static directory.
WWW.DOMAINNAME.COM
Then add a deploy script to package.json
:
{
"scripts": {
"deploy": "gatsby build --prefix-paths && gh-pages -d public"
}
}
gh-pages
package to make deploying simple.Thanks for reading and if you have any questions, use the comment function or send me a message @mariokandut.
If you want to know more about Gatsby, have a look at these Gatsby Tutorials.
Never miss an article.