Skip to content

Commit

Permalink
fix(terraform): duplicate caches created (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
hknutsen authored Nov 19, 2024
1 parent aaa156f commit 5392dfc
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions .github/workflows/terraform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,11 @@ jobs:
shell: bash
working-directory: ${{ inputs.working_directory }}

env:
# Enable Terraform plugin cache.
# https://developer.hashicorp.com/terraform/cli/config/config-file#provider-plugin-cache
TF_PLUGIN_CACHE_DIR: ${{ github.workspace }}/${{ inputs.working_directory }}/.terraform.d/plugin-cache

outputs:
upload-outcome: ${{ steps.upload.outcome }}
artifact-id: ${{ steps.upload.outputs.artifact-id }}
plugin-cache-dir: ${{ steps.mkdir.outputs.plugin-cache-dir }}
cache-primary-key: ${{ steps.cache-restore.outputs.cache-primary-key }}

steps:
- name: Checkout
Expand All @@ -135,13 +132,18 @@ jobs:
# The wrapper must be disabled to prevent this.

- name: Create Terraform plugin cache
run: mkdir --parents "$TF_PLUGIN_CACHE_DIR"
id: mkdir
run: |
plugin_cache_dir="$HOME/.terraform.d/plugin-cache"
mkdir --parents "$plugin_cache_dir"
echo "plugin-cache-dir=$plugin_cache_dir" >> "$GITHUB_OUTPUT"
- name: Cache Terraform plugins
uses: actions/cache@6849a6489940f00c2f30c0fb92c6274307ccb58a
- name: Restore cache
id: cache-restore
uses: actions/cache/restore@6849a6489940f00c2f30c0fb92c6274307ccb58a
with:
path: ${{ env.TF_PLUGIN_CACHE_DIR }}
key: terraform-${{ hashFiles(format('{0}/.terraform.lock.hcl', inputs.working_directory)) }}
path: ${{ steps.mkdir.outputs.plugin-cache-dir }}
key: ${{ runner.os }}-terraform-${{ hashFiles(format('{0}/.terraform.lock.hcl', inputs.working_directory)) }}
# The dependency lock file tracks provider dependencies for the Terraform configuration in the working directory.
# Calculate a hash for the dependency lock file and use this hash to identify the plugin cache for the Terraform configuration.
# https://developer.hashicorp.com/terraform/language/files/dependency-lock
Expand All @@ -163,6 +165,9 @@ jobs:
id: init
env:
TFBACKEND_CONFIG: ${{ inputs.backend_config }}
# Enable Terraform plugin cache.
# https://developer.hashicorp.com/terraform/cli/config/config-file#provider-plugin-cache
TF_PLUGIN_CACHE_DIR: ${{ steps.mkdir.outputs.plugin-cache-dir }}
run: |
optional_args=()
if [[ -n "$TFBACKEND_CONFIG" ]]; then
Expand Down Expand Up @@ -251,6 +256,13 @@ jobs:
# Ref: https://docs.github.com/en/actions/learn-github-actions/usage-limits-billing-and-administration#usage-limits
retention-days: 35

- name: Save cache
if: steps.cache-restore.outputs.cache-hit != 'true'
uses: actions/cache/save@6849a6489940f00c2f30c0fb92c6274307ccb58a
with:
path: ${{ steps.mkdir.outputs.plugin-cache-dir }}
key: ${{ steps.cache-restore.outputs.cache-primary-key }}

terraform-apply:
name: Terraform Apply
needs: terraform-plan
Expand Down Expand Up @@ -288,6 +300,12 @@ jobs:
gpg -d --batch --passphrase "$ENCRYPTION_PASSWORD" "$tarball" | tar -xv
rm "$tarball"
- name: Restore cache
uses: actions/cache/restore@6849a6489940f00c2f30c0fb92c6274307ccb58a
with:
path: ${{ needs.terraform-plan.outputs.plugin-cache-dir }}
key: ${{ needs.terraform-plan.outputs.cache-primary-key }}

- name: Terraform Apply
run: terraform apply -auto-approve -input=false "$TFPLAN_FILE"

Expand Down

0 comments on commit 5392dfc

Please sign in to comment.