Interview Prep/Real-World Interview/Write-a-pipeline tasks

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.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-pipeline tasks