I have a pipeline that runs terraform, which in turn modifies a state file. I want to copy this state file back to the host.
This seems to work, but with repeated calls I'm seeing old or incorrect copies of the state file on the host after run.
I've come up with the following repro:
package main
import (
"context"
"fmt"
"os"
"time"
"dagger.io/dagger"
)
type Dagger struct {
client *dagger.Client
//cache *dagger.CacheVolume
ctx context.Context
}
func toolbox(d Dagger) *dagger.Container {
return d.client.Container().From("ubuntu:latest").
WithMountedDirectory("/work", d.client.Host().Directory(".")).
WithWorkdir("/work").
WithEnvVariable("CACHEBUST", time.Now().String())
}
func run(d Dagger, cmd []string) error {
tm := toolbox(d)
tm = tm.WithExec(cmd)
out, err := tm.Stdout(d.ctx)
if err != nil {
return err
}
fmt.Println(out)
output := tm.Directory(".")
_, err = output.Export(d.ctx, ".")
if err != nil {
return err
}
return nil
}
func main() {
// init dagger client
ctx := context.Background()
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
if err != nil {
panic(err)
}
defer client.Close()
d := Dagger{
client: client,
ctx: ctx,
}
run(d, []string{"sh", "-c", `echo xxx1 > hello.txt`})
run(d, []string{"sh", "-c", `echo xxx2 >> hello.txt`})
run(d, []string{"sh", "-c", `echo xxx3 >> hello.txt`})
}
So after running, this I'd expect hello.txt to contain:
xxx1
xxx2
xxx3
Instead, it only seems to contain xxx3, despite me copying the file back to the host using Export() after each call to run(). Any tips would be great.