How to host Hugo site from private repository

Background
Usually, in order to utilize GitHub Pages for free hosting my Hugo site, I have to make the repository public. However, this also implies that even my draft posts will become visible in the public site repository. That is definitely not the way I prefer to do this.
Solution
After some searching, I found out that I can hide my draft posts and publish only the static site to the public repository by using GitHub Actions:
Prerequisites
- A private repository(Just turn the present public repository of Hugo site into private repository)
- A newly created public repository to publish Hugo site
- GitHub Personal token
Create a GitHub personal token
Click on your GitHub avatar in top-right and open
Settings
page.Click on
Developer settings
(at the bottom of the page).Click on
Personal access tokens
->Tokens(classic)
->Generate new token
->Generate new token(classic)
.Use personal tokenPersonal token is required because we need the permission to access to a external repository.Add a note for token. Select expiration.
Select scopes:
- ✅ workflow
- ✅ write:packages
Click on
Generate toke
.Copy the token start with
ghp_
Add GitHub personal token to private repository
Open the
Settings
of private repository.Click on
Secrets and variables
->Actions
Click on
New repository secret
Add name then paste the token to the
Secret
area.Click on
Add secret
GitHug workflow configurations
Here is the workflow configuration file I made for my Hugo site:
|
|
Replace the configuration file under .github/workflows
with it. Then push the changes to private repository and check whether the workflow runs successfully.
Conclusion
This way, I can push all the content, including draft posts, drawio files, and other files related to my site, to my private repository without worrying about they being made public.
References
If you have any questions or advice, leave me a comment below and I will try my best to respond!