Github pages are great, it provides a free static page hosting, but the only caveat is the repository has to be public repository. And, if you want to keep your source private, you will have to opt for premium plans to host pages from private repository.
Here is what I have done, github allows unlimited private repositories, so I created a new private repo where I kept my source code and another repo where my site is hosted.
I am also hosting this website on github. I am using Jekyll as my website generator, but you can use any static web generator e.g. Next, Gatsby. I am assuming you have your static web already in place.
In this article, we will automate the process of deployment, we will build the website in private repo and then publish it to the public repo from where it will be served. I will be going through Jekyll site action.
Setting up personal token
The first step is to set up a personal access token.
Go to your profile and click to settings.
Go to developer.
Go to Personal access token.
Now you need to
Generate new token. with repo permissions.
Once you are done copy the generated token, we will need to set this token during our build.
Create secret in private repo
Go to your private repo and click the settings:
Go to secrets and create a new secret with token we have earlier generated, I have named it
Create github action in private repo
This is where the magic begins, we will build a github action in our private repo. You will need to create a file at
- we checkout the source code (line 15), notice we are providing path as
codeso our code will be checked out at
- we checkout the site repository (line 19), here we are checking out
<PUBLIC_REPO>(in my case it was
sitedirectory, make sure you set the token here.
- I am performing a Jekyll build (line 26), if you are using other site builders, this step will change.
- Time to clean my old website files, so I go to site folder and delete everything (line 33).
- Copy the generated build to my site folder(line 37).
- Finally, commit the changes to publish your site(line 42).
So here you have it, when ever you push changes to your private repository. The github action will be fired which will build and publish your site to your public repo without exposing your source code.
I would love to know what you think.
If you liked this article, you can buy me a coffee