Within GitHub Actions, there are two primary methods for accessing multiple private Terraform module repositories during runtime. Currently, I’ve documented and thoroughly tested two approaches:

  1. Deploy Keys : Leveraging deploy keys offers a straightforward method. Each repository can be associated with its own deploy key, ensuring secure access while maintaining repository-specific permissions.
  2. GitHub App Integration (Covered in this Post) : Alternatively, you can utilize a GitHub App to manage access. This approach provides a more centralized means of controlling access to multiple repositories, offering enhanced manageability and scalability.

While these are the methods I’ve focused on, it’s worth noting that other approaches may exist, but these two have been extensively researched and validated.

Utilizing deploy keys for this purpose can become cumbersome as the number of repositories increases, especially considering that each repository requires its own unique deploy key. Managing multiple deploy keys can lead to complexity and potential maintenance challenges. Below is the information you’ll need to update in the configuration file

To circumvent this complexity, we can adopt the GitHub approach outlined below, following a step-by-step guide.

  1. Navigate to GitHub Settings: Sign in to your GitHub account and navigate to “Settings.”
  2. Select “Developer settings”: Within the settings menu, find and click on “Developer settings.”
  3. Choose “GitHub Apps”: In the developer settings, locate and select “GitHub Apps” from the sidebar menu.
  4. Click “New GitHub App”: On the GitHub Apps page, click on the “New GitHub App” button.
  5. Provide App Details: Fill out the necessary details for your GitHub App, including the following:
  • Name: Choose a name for your GitHub App.
  • Description: Provide a brief description of your GitHub App.
  • Homepage URL: Optionally, provide a URL for the homepage of your GitHub App.
  • Webhook URL: Disable it
  • Permissions: Select the permissions your GitHub App needs to access repositories and other resources.
  • Events: Choose the events your GitHub App should be subscribed to.
  • Webhook Secret: Optionally, provide a secret for securing webhook payloads.

You need to follow below steps further

  1. Generate Private Key: Generate a private key for your GitHub App. This key will be used to authenticate your GitHub App when making API requests.
  2. Install App on GitHub Account or Organization: Once you’ve created your GitHub App, you’ll need to install it on your GitHub account or organization to grant it access to repositories and other resources.
  3. Configure App Settings: After installation, you may need to configure additional settings for your GitHub App, such as specifying repository access permissions.

We’ll integrate and test the GitHub App within our GitHub Actions workflow. To proceed, create two secret variables either at the repository level or the organization level. These variables will be utilized during the workflow execution.

TERRAFORM_APP_ID App id of GitHub App you just created

TERRAFORM_PRIVATE_KEY :Private key you just generated for GitHub App

name: Terraform Plan
on:
workflow_dispatch:
env:
TERRAFORM_VERSION: "1.3.7"
TERRAFORM_DIRECTORY: "terraform"
permissions:
id-token: write
contents: read
pull-requests: write
jobs:
terraform:
runs-on: ubuntu-latest
steps:
– name: Checkout PR
uses: actions/checkout@v4
– name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.terraform_version }}
terraform_wrapper: false
– name: "Get Application Token"
id: get_app_token
uses: tibdex/github-app-token@v1
with:
app_id: ${{ secrets.TERRAFORM_APP_ID }}
private_key: ${{ secrets.TERRAFORM_PRIVATE_KEY }}
– name: Extend Git credentials
uses: de-vri-es/setup-git-credentials@v2.1.2
with:
credentials: https://user:${{ steps.get_app_token.outputs.token }}@github.com
– name: Terraform Init
shell: bash
run: |
terraform init
view raw gistfile1.txt hosted with ❤ by GitHub

Leave a comment

I’m Lalit


Welcome to CloudWithLalit, my cozy corner of the internet dedicated to documenting everything I learn during my journey through cloud and DevOps. Here, I extend a warm invitation for you to join me on this voyage of learning, sharing insights, and everything in between to aid you on your own journey. Let’s embark on this adventure together and get crafty with all things cloud and DevOps!

Let’s connect