-
Notifications
You must be signed in to change notification settings - Fork 242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enabling Plugin Cache Directory #4
Comments
Thank you for workaround example, just want to mention that order of cache and checkout should be opposite. In your example checkout will clean cache. So it should look like:
|
It is mentioned here that the plugin cache directory will not be created by the terraform cli. So you must ensure the directory exists before trying to cache anything. Here is an example of a workaround which is functioning for me: env:
TF_PLUGIN_CACHE_DIR: ${{ github.workspace }}/.terraform.d/plugin-cache
jobs:
terraform:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1.2.1
with:
terraform_wrapper: false
terraform_version: '1.0.0'
- name: Create Terraform Plugin Cache Dir
run: mkdir --parents $TF_PLUGIN_CACHE_DIR
- name: Cache Terraform
uses: actions/cache@v2
with:
path: ${{ env.TF_PLUGIN_CACHE_DIR }}
key: ${{ runner.os }}-terraform-${{ hashFiles('**/.terraform.lock.hcl') }} |
I tried to set up caching, but I'm not sure it's working. At least I see no difference in Did you see the difference? |
To confirm if caching worked, check the init's output text, it should include "from the shared cache directory". Snip from uncached provider download:
Snip from successful provider reuse:
Also, I believe the more-recent Github Actions YAML above will only restore from cache if the lockfile has an exact match |
I've been trying to get this working on a single .lock.hcl file without any luck. From the docs it states that hashFiles() needs the path to be under the github_workspace folder So works but would it be possible to get something like |
Hi @audunsolemdal, yes. The following works for me: - name: Configure Terraform plugin cache
run: |
echo 'plugin_cache_dir = "$HOME/.terraform.d/plugin-cache"' > ~/.terraformrc
mkdir -p ~/.terraform.d/plugin-cache
- name: Cache Terraform
uses: actions/cache@v3
with:
path: ~/.terraform.d/plugin-cache
key: ${{ runner.os }}-terraform-${{ hashFiles(format('{0}/.terraform.lock.hcl', env.TF_DIR)) }}
restore-keys: ${{ runner.os }}-terraform- TF_DIR in my case is |
If I understood correctly this issue is about adding the caching capability to the |
|
I did try this solution, but I got an error on the cleanup for the caching step:
I am using a "multiple apps separated by environment" folder organization with s3 remote states. For example:
Also verified that the TF_PLUGIN_CACHE_DIR var was set to the correct path:
|
One small yet important detail regarding above - in order to leverage plugin cache please make sure your
Here's what did the trick for me:
$ terraform init -lockfile=readonly
...
Initializing provider plugins...
- Reusing previous version of fastly/fastly from the dependency lock file
- Reusing previous version of hashicorp/aws from the dependency lock file
- Reusing previous version of ngrok/ngrok from the dependency lock file
- Using fastly/fastly v5.13.0 from the shared cache directory
- Using hashicorp/aws v5.69.0 from the shared cache directory
- Using ngrok/ngrok v0.3.0 from the shared cache directory Sample workflow: jobs:
plan:
# ...
- name: Configure Terraform plugin cache
run: |
mkdir -p ~/.terraform.d/plugin-cache
- name: Cache Terraform plugins
uses: actions/cache@v4
with:
path: ~/.terraform.d/plugin-cache
key: ${{ runner.os }}-terraform-${{ hashFiles('.terraform.lock.hcl') }}
restore-keys: ${{ runner.os }}-terraform-
- name: Terraform Init
id: init
run: |
# Alternative to ~/.terraformrc file
export TF_PLUGIN_CACHE_DIR="${HOME}/.terraform.d/plugin-cache"
terraform init -lockfile=readonly |
Description
We have a use case that requires running Terraform in a GitHub Action across multiple, separate configurations in the same repository. Since Terraform prefers to maintain its own plugin directory per directory, this leads to re-downloading (mostly the same) plugins for each separate directory. It would be great if there was the option to enable the plugin cache directory, which can be handled in the Terraform CLI configuration file via the
plugin_cache_dir
option, e.g. from the documentation:Seems like it could be either a boolean where the GitHub Action maintains the directory location itself (
enable_plugin_cache_dir: true
) or allow configurations to provide it directly (plugin_cache_dir: /wherever/whenever
), which might be useful for managed runners at the expense of being more complex. It seems like in either case, the GitHub Action would should try to create the directory, skipping errors for existing ones, as its prior existence is required.Potential Workarounds
References
The text was updated successfully, but these errors were encountered: