Use gems from private GitHub repos.

Published on April 02, 2024
Written by Victor Cobos

This brief guide walks you through the steps to install private gems hosted on GitHub.

GitHub personal access token

To authenticate bundler to GitHub, you will need a personal access token. Follow this guide to create one.

Setup Gemfile

Like any other gems, your private gem needs to be listed in your Gemfile. Add your private gem to the Gemfile:

gem "<gem>", git: "https://github.com/<owner>/<repo>.git", branch: "<branch>"

Use https link, it's important for the authentication part.

Setting up your credentials locally

This step may not always be necessary, depending on how you run the project locally and the permissions you have.
To set up your credentials, use bundle-config:

bundle config --local github.com "<personal access token>"

Install the gem dependencies using bundle install as usual.

Github Actions

You can specify env variables when running Github Actions, and that's what we'll use for authentication. Go to your repo's secrets page https://github.com/[user]/[repo]/settings/secrets/actions and create a repository secret with the personal access token. You can use it in your workflow file like so:

name: your_job

jobs:
  your_job:
    runs-on: ubuntu-latest
    env:
      BUNDLE_GITHUB__COM: "x-access-token:${{ secrets.PERSONAL_ACCESS_TOKEN }}"

Deploying

Just add the same BUNDLE_GITHUB__COM env variable to your server:

BUNDLE_GITHUB__COM=x-access-token:<personal access token>

And you are ready to deploy 🚀

Subscribe to get future articles via the RSS feed .