#Am I using cache correctly cause it seems to redownload each time?

1 messages · Page 1 of 1 (latest)

scenic pollen
#

I setup a basectr() and then this is set and used.

Seems every execution to redownload which takes 2 mins (uggg).

Is there something I'm getting wrong in making it reuse this?


// base sets up the Container with a Terraform Image and cache volumes
func (t *TfYolo) base() *dagger.Container {
    return dag.Container().
        From(terraformImage).
        WithEnvVariable("TF_DATA_DIR", terraformDataDirectoryMountPath).
        WithMountedCache(
            "terraform-cache",
            dag.CacheVolume(".terraform"),
            dagger.ContainerWithMountedCacheOpts{
                Sharing: dagger.Shared,
                Owner:   "root", // base image is golang:alpine, which is root user https://github.com/hashicorp/terraform/blob/main/Dockerfile
            },
        ).
        WithWorkdir(mntPrefix)
}

// Initializes Terraform in the provided directory with ARM_SUBSCRIPTION_ID as an environment variable
func (t *TfYolo) Init(
    ctx context.Context,
    dir *dagger.Directory,
    armSubscriptionID string,
    // +optional
    bustCache bool,
) (string, error) {
    logger := setupLogger("terraform init")

    logger.Info(
        "terraform init",
        slog.String("ARM_SUBSCRIPTION_ID", armSubscriptionID),
    )
    c := t.base().
        WithEnvVariable("ARM_SUBSCRIPTION_ID", armSubscriptionID).
        WithEnvVariable("TF_IN_AUTOMATION", "true").
        WithMountedDirectory(mntPrefix, dir)
    if bustCache {
        slog.Info("🔥 busting cache")
        c = c.WithEnvVariable("CACHEBUSTER", time.Now().String())
    }

    c = c.WithExec([]string{"terraform", "init", "-input=false"}). // NOTE: -input=false not sure if needed since in CI with automation, but docs now say cosmetic only?
                                    WithExec([]string{"terraform", "validate"})
    return c.Stdout(ctx)
}

Some sample code to explain

#

600ms in local tooling.
Rerunning and this time it seemed to honor the caching ... only of course after I commented here. laughcry
If you see a problem in how I'm doing it let me know.