Skip to content

Instantly share code, notes, and snippets.

@BondAnthony
Last active November 12, 2020 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BondAnthony/1ad255c829142483fa1bc4428fd43e76 to your computer and use it in GitHub Desktop.
Save BondAnthony/1ad255c829142483fa1bc4428fd43e76 to your computer and use it in GitHub Desktop.
Terraform monorepo actions

Terraform + Github Actions

I struggled to get the right mix of github actions to lint my terraform modules within a monorepo. Here is the configuration I settled on which dynamically creates new jobs based on the data within the project.

I use a module naming pattern of provider-module_name, which becomes aws-kubernetes or gcp-gke. This information is important because the action executes the following command to create a list of modules.

$(printf '\"%s\"', gcp-* aws-* azure-* | sed 's/,$//')
name: Terraform CI

on:
  pull_request:
    branches:
      - master
jobs:
  modules:
    name: module list
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.modules.outputs.matrix }}
    steps:
      - name: Check out code
        uses: actions/checkout@v1
      - id: modules
        run: |
          MODULES=$(printf '\"%s\"', gcp-* aws-* azure-* | sed 's/,$//') 
          echo $MODULES
          echo "::set-output name=matrix::{\"module\":[$MODULES]}"
  validate:
    name: Validate
    needs: modules
    runs-on: ubuntu-latest
    strategy:
      matrix: ${{fromJson(needs.modules.outputs.matrix)}}
      fail-fast: false
      max-parallel: 15
    steps:
    - name: Check out code
      uses: actions/checkout@v1
    - name: terraform init
      uses: docker://hashicorp/terraform:0.12.29
      with:
        entrypoint: terraform
        args: init -backend=false ${{ matrix.module }}

    - name: terraform validate
      uses: docker://hashicorp/terraform:0.12.29
      with:
        entrypoint: terraform
        args: validate ${{ matrix.module }} -backend=false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment