diggerhq / digger

Digger is an open source IaC orchestration tool. Digger allows you to run IaC in your existing CI pipeline ⚡️
https://digger.dev
Apache License 2.0
2.81k stars 131 forks source link

Caching of terraform dependencies #811

Open ZIJ opened 8 months ago

ZIJ commented 8 months ago

Digger feels slow; Francoi's suggestion is that we might not be using Github cache for terraform dependencies, so it always runs init from scratch

JoshuaJackson-jobvite commented 4 months ago

assuming that digger is running terraform relatively standard, having a .terraformrc that has something like:

plugin_cache_dir   = "$HOME/.terraform.d/plugin-cache"

and then you use the cache restore/cache before/after digger runs in the pipeline should just work.

Or as an env variable:


PLUGIN_CACHE_DIR=$HOME/.terraform.d/plugin-cache
JoshuaJackson-jobvite commented 4 months ago

Something like the following should work as an example:

name: Digger PR flow

on:
  pull_request:
    branches: ["main"]
    types: [closed, opened, synchronize, reopened]
  issue_comment:
    types: [created]
    if: startsWith(github.event.comment.body, 'digger')
  workflow_dispatch:

jobs:
  digger-job:
    runs-on: ubuntu-latest
    permissions:
      contents: write # required to merge PRs
      id-token: write # required for workload-identity-federation
      pull-requests: write # required to post PR comments
      statuses: write # required to validate combined PR status
      packages: read # required to read the packages
      actions: read
    steps:
      - name: create_cache_dir
        run: |
          mkdir -p $GITHUB_WORKSPACE/cache
      - uses: actions/cache/restore@v4
        id: restore_cache
        name: restore_cache
        with:
          path: ${{ github.workspace }}/cache
          key: ${{ runner.os }}-cache-${{ hashFiles('**/cache') }}
          restore-keys: |
            ${{ runner.os }}-cache-${{ hashFiles('**/cache') }}
            ${{ runner.os }}-cache
      - uses: diggerhq/digger@v0.4.2
        with:
          setup-aws: false
          no-backend: true
          disable-locking: true
          digger-hostname: "https://cloud.digger.dev"
          digger-organisation: "XXXX"
          digger-token: ${{ secrets.DIGGER_TOKEN }}
        env:
          GITHUB_OWNER: XXXXX
          TF_PLUGIN_CACHE_DIR: ${{ github.workspace }}/cache
      - uses: actions/cache/save@v4
        name: cache-save
        with:
          path: ${{ github.workspace }}/cache
          key: ${{ runner.os }}-${{ hashFiles('**/cache') }}

From my quick testing seems to be working.

JoshuaJackson-jobvite commented 4 months ago

Assuming acceptable I can look into updating the documentation around it as applicable.

JoshuaJackson-jobvite commented 4 months ago

@ZIJ any thoughts?

JoshuaJackson-jobvite commented 4 months ago

@motatoes as this also relates to #142 should this be combined under just one of these...but also wanted to bring the proposal to your attention as well.

motatoes commented 4 months ago

Hey @JoshuaJackson-jobvite thanks a lot for this contribution ! This seems like an elegant solution and I think it would be great to have in our composite action

Would love to discuss with you a construction over slack:

Join me on Slack -- it’s a faster, simpler way to work. Sign up here, from any device: https://join.slack.com/t/diggertalk/shared_invite/zt-2f3j0ynm9-_tFTqgcqh_rPqfkFikPnLg

JoshuaJackson-jobvite commented 3 months ago

Just checking in on this one to see how the internal testing might be going?

motatoes commented 3 months ago

Hey @JoshuaJackson-jobvite sorry got carried away with a big feature we are cooking. Once that's done in the next couple of days I will get around to this. It has not been forgotten! :)