Skip to content

Commit f3e523d

Browse files
authored
allow cache values to be overridable for terraform and terragrunt (#1873)
* allow cache values to be overridable for TF_PLUGIN_CACHE_DIR and TERRAGRUNT_PROVIDER_CACHE_DIR
1 parent 1e88db8 commit f3e523d

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

.github/workflows/libs_test.yml

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ jobs:
1616
uses: opentofu/[email protected]
1717
with:
1818
tofu_version: 1.8.5
19+
20+
- name: Setup terraform
21+
uses: hashicorp/setup-terraform@v3
22+
with:
23+
terraform_wrapper: false
24+
terraform_version: "1.5.5"
1925

2026
- name: Download Go
2127
uses: actions/setup-go@v5

action.yml

+18-6
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ inputs:
167167
description: "Leverage actions/cache to cache dependencies to speed up execution"
168168
required: false
169169
default: 'false'
170+
terraform-cache-dir:
171+
description: "allows overriding of the terraform cache dir which defaults to ${github.workspace}/cache"
172+
required: false
173+
default: ''
174+
170175
digger-spec:
171176
description: "(orchestrator only) the spec to pass onto digger cli"
172177
required: false
@@ -247,11 +252,21 @@ runs:
247252
subscription-id: ${{ inputs.azure-subscription-id }}
248253
if: ${{ inputs.setup-azure == 'true' && inputs.azure-client-id != '' }}
249254

255+
# if terraform-cache-dir is set then we set it to that otherwise set it to '${{github.workspace}}/cache'
256+
- name: retrieve cache dir
257+
shell: bash
258+
run: |
259+
CACHE_DIR=${{ inputs.terraform-cache-dir == '' &&
260+
format('{0}/cache', github.workspace) ||
261+
inputs.terraform-cache-dir }}
262+
echo "TF_PLUGIN_CACHE_DIR=$CACHE_DIR" >> $GITHUB_ENV
263+
echo "TERRAGRUNT_PROVIDER_CACHE_DIR=$CACHE_DIR" >> $GITHUB_ENV
264+
250265
- uses: actions/cache/restore@v4
251266
id: restore_cache
252267
name: restore_cache
253268
with:
254-
path: ${{ github.workspace }}/cache
269+
path: $TF_PLUGIN_CACHE_DIR
255270
key: digger-cache
256271
restore-keys: |
257272
digger-cache
@@ -346,6 +361,7 @@ runs:
346361
mkdir -p $GITHUB_WORKSPACE/cache
347362
shell: bash
348363

364+
349365
- name: build and run digger
350366
if: ${{ !startsWith(github.action_ref, 'v') }}
351367
shell: bash
@@ -369,9 +385,7 @@ runs:
369385
INPUT_DRIFT_DETECTION_SLACK_NOTIFICATION_URL: ${{ inputs.drift-detection-slack-notification-url }}
370386
NO_BACKEND: ${{ inputs.no-backend }}
371387
DEBUG: 'true'
372-
TF_PLUGIN_CACHE_DIR: ${{ github.workspace }}/cache
373388
TERRAGRUNT_PROVIDER_CACHE: ${{ inputs.cache-dependencies == 'true' && 1 || 0 }}
374-
TERRAGRUNT_PROVIDER_CACHE_DIR: ${{ github.workspace }}/cache
375389
DIGGER_RUN_SPEC: ${{inputs.digger-spec}}
376390
run: |
377391
if [[ ${{ inputs.ee }} == "true" ]]; then
@@ -407,9 +421,7 @@ runs:
407421
INPUT_DIGGER_COMMAND: ${{ inputs.command }}
408422
INPUT_DRIFT_DETECTION_SLACK_NOTIFICATION_URL: ${{ inputs.drift-detection-slack-notification-url }}
409423
NO_BACKEND: ${{ inputs.no-backend }}
410-
TF_PLUGIN_CACHE_DIR: ${{ github.workspace }}/cache
411424
TERRAGRUNT_PROVIDER_CACHE: ${{ inputs.cache-dependencies == 'true' && 1 || 0 }}
412-
TERRAGRUNT_PROVIDER_CACHE_DIR: ${{ github.workspace }}/cache
413425
DIGGER_RUN_SPEC: ${{inputs.digger-spec}}
414426
id: digger
415427
shell: bash
@@ -428,7 +440,7 @@ runs:
428440
name: cache-save
429441
if: ${{ always() && inputs.cache-dependencies == 'true' && steps.restore_cache.outputs.cache-hit != 'true' }}
430442
with:
431-
path: ${{ github.workspace }}/cache
443+
path: $TF_PLUGIN_CACHE_DIR
432444
key: digger-cache-${{ hashFiles('**/cache') }}
433445

434446
branding:

cli/pkg/digger/digger.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ func DetectCI() CIName {
6565
}
6666

6767
func RunJobs(jobs []orchestrator.Job, prService ci.PullRequestService, orgService ci.OrgService, lock locking2.Lock, reporter reporting.Reporter, planStorage storage.PlanStorage, policyChecker policy.Checker, commentUpdater comment_updater.CommentUpdater, backendApi backendapi.Api, jobId string, reportFinalStatusToBackend bool, reportTerraformOutput bool, prCommentId string, workingDir string) (bool, bool, error) {
68-
6968
defer reporter.Flush()
7069

70+
log.Printf("Info: [TF_PLUGIN_CACHE_DIR=%v] ", os.Getenv("TF_PLUGIN_CACHE_DIR"))
71+
log.Printf("Info: [TERRAGRUNT_PROVIDER_CACHE_DIR=%v", os.Getenv("TERRAGRUNT_PROVIDER_CACHE_DIR"))
72+
7173
runStartedAt := time.Now()
7274

7375
exectorResults := make([]execution.DiggerExecutorResult, len(jobs))

0 commit comments

Comments
 (0)