Write a workflow step that caches Terraform providers and Go modules.
IntermediateHands-onWrite-a-pipeline tasks
Answer
- uses: actions/cache@v4
with:
path: |
~/.terraform.d/plugin-cache
~/go/pkg/mod
key: ${{ runner.os }}-deps-${{ hashFiles('**/.terraform.lock.hcl', '**/go.sum') }}
restore-keys: ${{ runner.os }}-deps-
- run: echo "TF_PLUGIN_CACHE_DIR=$HOME/.terraform.d/plugin-cache" >> "$GITHUB_ENV" && mkdir -p ~/.terraform.d/plugin-cache
Key trick → Key the cache on lockfile hashes so it's invalidated exactly when dependencies change. restore-keys gives a partial hit to start from.
More on Write-a-pipeline tasks
Write a GitHub Actions workflow that runs tests on every PR and push to main.BeginnerWrite a workflow that builds a Docker image and pushes it to ECR using OIDC (no access keys).IntermediateWrite a matrix job that tests on Python 3.10, 3.11 and 3.12 on Linux and Windows.BeginnerWrite a declarative Jenkinsfile with build, test, Docker push and a manual approval before prod.AdvancedWrite a GitHub Actions deploy job that requires approval for production using environments.BeginnerWrite a reusable workflow and call it from another repo.Intermediate