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.

My website

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 MY_TOKEN

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 .github/workflows/ci.yml

Steps:

  1. we checkout the source code (line 15), notice we are providing path as code so our code will be checked out at ./code/.
  2. we checkout the site repository (line 19), here we are checking out master branch of <PUBLIC_REPO> (in my case it was kuros/kuros.github.io) in site directory, make sure you set the token here.
  3. I am performing a Jekyll build (line 26), if you are using other site builders, this step will change.
  4. Time to clean my old website files, so I go to site folder and delete everything (line 33).
  5. Copy the generated build to my site folder(line 37).
  6. 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.

Categories:

Updated:

Kumar Rohit
WRITTEN BY

Kumar Rohit

I like long drives, bike trip & good food. I have passion for coding, especially for Clean-Code.

Leave a comment