From 20099b81d24086413e248680427595dbad4e044d Mon Sep 17 00:00:00 2001 From: Yohan Belval Date: Tue, 12 Nov 2024 15:40:49 +0000 Subject: [PATCH] feat(lint): add caching layer to terraform --- lint/action.yaml | 26 ++++++++++++++++++++++++++ test/action.yaml | 30 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/lint/action.yaml b/lint/action.yaml index 750eb7c..186cbb3 100644 --- a/lint/action.yaml +++ b/lint/action.yaml @@ -37,6 +37,32 @@ runs: - name: Setup tools # Provide opportunity to install terraform, golang, and others uses: open-turo/action-setup-tools@v2 + + - name: Configure Terraform plugin cache + shell: bash + run: | + echo "TF_PLUGIN_CACHE_DIR=$HOME/.terraform.d/plugin-cache" >>"$GITHUB_ENV" + mkdir --parents "$HOME/.terraform.d/plugin-cache" + - name: Compute terraform plugin cache key + if: inputs.s3-bucket-name != '' + id: tf_plugin_cache_key + run: | + echo "key=terraform-${{ runner.os }}-${{ hashFiles('**/.terraform.lock.hcl') }}" >> $GITHUB_OUTPUT + shell: bash + - name: Load cached terraform plugins if available + if: inputs.s3-bucket-name != '' + uses: everpcpc/actions-cache@v2 + id: read_cache + with: + bucket: ${{ inputs.s3-bucket-name }} + use-fallback: false + path: ~/.terraform.d/plugin-cache + key: ${{ env.cache-name }}-${{ steps.tf_plugin_cache_key.outputs.key }} + restore-keys: ${{ env.cache-name }}- + env: + AWS_REGION: ${{ inputs.s3-bucket-region }} + cache-name: ${{ github.event.repository.name }}/cache-tf-plugins + - name: Pre-commit uses: open-turo/action-pre-commit@v3 with: diff --git a/test/action.yaml b/test/action.yaml index 5b6d91d..34e04dd 100644 --- a/test/action.yaml +++ b/test/action.yaml @@ -18,6 +18,12 @@ inputs: aws-secret-access-key: required: false description: Secret access key that grants access to AWS services. Requires aws-access-key-id input as well. + s3-bucket-name: + required: false + description: S3 bucket name to cache dependencies to speed up runs. + s3-bucket-region: + required: false + description: S3 bucket region to cache dependencies to speed up runs. runs: using: composite @@ -39,6 +45,30 @@ runs: run: | if [[ ! -f ./script/setup ]]; then echo "Missing required file: ./script/setup"; exit 1; fi if [[ ! -f ./script/test ]]; then echo "Missing required file: ./script/test"; exit 1; fi + - name: Configure Terraform plugin cache + shell: bash + run: | + echo "TF_PLUGIN_CACHE_DIR=$HOME/.terraform.d/plugin-cache" >>"$GITHUB_ENV" + mkdir --parents "$HOME/.terraform.d/plugin-cache" + - name: Compute terraform plugin cache key + if: inputs.s3-bucket-name != '' + id: tf_plugin_cache_key + run: | + echo "key=terraform-${{ runner.os }}-${{ hashFiles('**/.terraform.lock.hcl') }}" >> $GITHUB_OUTPUT + shell: bash + - name: Load cached terraform plugins if available + if: inputs.s3-bucket-name != '' + uses: everpcpc/actions-cache@v2 + id: read_cache + with: + bucket: ${{ inputs.s3-bucket-name }} + use-fallback: false + path: ~/.terraform.d/plugin-cache + key: ${{ env.cache-name }}-${{ steps.tf_plugin_cache_key.outputs.key }} + restore-keys: ${{ env.cache-name }}- + env: + AWS_REGION: ${{ inputs.s3-bucket-region }} + cache-name: ${{ github.event.repository.name }}/cache-tf-plugins - name: Setup shell: bash run: ./script/setup