Access private modules with an access token

Learn how to use GitHub's access tokens to authenticate a private Hugo Modules repository.

Some Hugo Modules won't be a good fit for Site Mounting, particularly if they belong to a different organization in GitHub, or require a complex setup. In this case, we can configure your CloudCannon site to install private Hugo Modules.

Let's enable Hugo Modules with GitHub's access token feature, CloudCannon's environment variables, and CloudCannon's Build Hooks feature.

Create a GitHub access token#

Open GitHub and navigate to your Fine-grained tokens settings. Click Generate new token.

When creating a token, you must select a name, expiry date, and which GitHub organization or user owns the private repository. Under Repository access, enable the Only Select Repositories option, and select the private repository that contains your Hugo Modules. In the Repository permissions field, set Contents to "Access: Read-only".

Click the Generate token button and copy the token to your clipboard.

Add the token to CloudCannon#

Open your site in CloudCannon and navigate to your Configuration under Site Settings, then Builds. Click on the Command Line Options card to expand it.

A screenshot of the Build Configuration page shows the CLI field to add a GH_TOKEN environment variable.

Add an environment variable to your site using the key "GH_TOKEN". Paste the GitHub token you generated into the value field. Click the Update Configuration button.

Configure git to use your access token#

Next, configure your git command to use this token when downloading your Hugo Modules using CloudCannon's Build Hooks.

Create a file inside a .cloudcannon folder in the root of your repository. Name this file "preinstall" and add the following:

.cloudcannon/preinstall
copied
git config --global url."https://${GH_TOKEN}:x-oauth-basic@github.com".insteadOf "https://github.com"

Do not paste your access token here. Leave the text as ${GH_TOKEN}.

CloudCannon will use the token from the environment variable set in the previous step. Git will use your access token whenever it interacts with https://github.com. Hugo will use this when downloading your modules.

Once you have committed this file to your repository, CloudCannon will trigger a new build of your site and download your Hugo Modules.

Open in a new tab