#Creating a file with WithExec isn't reflected into the Dagger Container

1 messages · Page 1 of 1 (latest)

fresh drum
#

Hey guys, I'm struggling with a bug in a set of Dagger tasks that I'm writing in Go.
Basically, I'm fetching a private Git repository with a certain Terragrunt-based root-modules; this part works well. When I'm running a command to generate the output of the plan (here's the scenario, file generation), let's say WithExec([]string{"sh", "-c", "terragrunt plan -out=output.tfplan"}). it doesn't fail, but it does not generate the expected output.tfplan in the container. I'm even inspecting the Entries and printing the standard output, and nope! no file at all.
What I'm doing wrong?

// Generate the terraform plan file.
_, _ = tg.
    WithUnixSocket(sshSocketPath, client.Host().UnixSocket(sshSocketPath)).
    WithEnvVariable("SSH_AUTH_SOCK", sshSocketPath).
    WithEnvVariable("GIT_SSH_COMMAND", "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=accept-new").
    WithExec([]string{"sh", "-c", "terragrunt plan -out=output.tfplan"}).
    Stdout(ctx)
// Export the plan output.
planOutput, planErr := tg.
    WithUnixSocket(sshSocketPath, client.Host().UnixSocket(sshSocketPath)).
    WithEnvVariable("SSH_AUTH_SOCK", sshSocketPath).
    WithEnvVariable("GIT_SSH_COMMAND", "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=accept-new").
    WithExec([]string{"sh", "-c", "terragrunt show -json output.tfplan > output.json"}).
    Stdout(ctx)
if planErr != nil {
    return fmt.Errorf("failed to export plan output: %w", planErr)
}

// Inspect the generated plan output.
output, catErr := tg.
    WithEntrypoint(nil).
    WithEnvVariable("CACHEBUSTER", time.Now().String()).
    WithExec([]string{"sh", "-c", "cat output.json"}).
    Stdout(ctx)
if catErr != nil {
    return fmt.Errorf("failed to read plan output: %w", catErr)
}

I have to say that the commands executed on themselves aren't failing. It's just the file that somehow isn't there.

rigid jungle
#

it looks like in the first chain, you're dropping the result of running terraform. You should continue from that state if you want to access that file

fresh drum
#

Hey @rigid jungle , makes sense. I will try it shortly. I'm curoius thought, is it the recommended pattern? Wouldn't the container store the file on its working directory path?

rigid jungle
#

yes, it is the recommended pattern. Each call to a Dagger function returns an immutable value. You can pass that value (in your case a container) to another function, which will return acnew value, and so on. In Dagger, containers are just data that you pass around like any other.