Skip to content

Commit

Permalink
testing different caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel OBrien committed May 16, 2024
1 parent 98aec88 commit 27bb8dd
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions ci/dagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,20 @@ func (m *Ci) Precommit(ctx context.Context, source *Directory) (string, error) {
WithDirectory(sourceDir, source, dagger.ContainerWithDirectoryOpts{
Owner: "circleci",
}).WithWorkdir(sourceDir).
WithExec([]string{"pre-commit", "install"}).
WithExec([]string{"pre-commit", "run", "-a", "golangci-lint"}).
WithExec([]string{"pre-commit", "run", "-a", "golangci-lint"}).
Stdout(ctx)
}

// Setup and run go tests
func (m *Ci) TestRepo(ctx context.Context, source *Directory) (string, error) {
return dag.Container().
From(imageId).
With(m.baseImage(ctx)).
WithExec([]string{"go", "install", "github.com/kyoh86/richgo@v0.3.10"}).
With(m.testImage(ctx)).
WithDirectory(sourceDir, source, dagger.ContainerWithDirectoryOpts{
Owner: "circleci",
}).WithWorkdir(sourceDir).
WithExec([]string{"sh", "-c", "go test -race -v ./... | richgo testfilter"}).
WithEnvVariable("RICHGO_FORCE_COLOR", "1").
Stdout(ctx)
}

Expand Down Expand Up @@ -88,6 +87,7 @@ func (m *Ci) Snapshot(ctx context.Context, source *dagger.Directory) (string, er
goReleaserDocker := dag.Container().
From(goReleaserImageId).
WithDirectory(sourceDir, source).WithWorkdir(sourceDir).
WithMountedCache("/go/pkg/mod", m.goModCacheVolume()).
WithServiceBinding("docker", docker).
WithEnvVariable("DOCKER_HOST", "tcp://docker:2375")

Expand All @@ -108,19 +108,24 @@ func (m *Ci) goBuildCacheVolume() *CacheVolume {

// Trying to return a cached base image here.
func (m *Ci) baseImage(ctx context.Context) dagger.WithContainerFunc {
return func(dag *dagger.Container) *dagger.Container {

dag, err := dag.From(imageId).
WithMountedCache("/go/pkg/mod", m.goModCacheVolume()).
return func(ctr *dagger.Container) *dagger.Container {
return ctr.From(imageId).
WithExec([]string{"sudo", "apt-get", "update"}).
WithExec([]string{"sudo", "apt-get", "install", "python3-pip"}).
Sync(ctx)
WithMountedCache("/go/pkg/mod", m.goModCacheVolume()).
WithEnvVariable("GOMODCACHE", "/go/pkg/mod").
WithMountedCache("/go/build-cache", m.goBuildCacheVolume()).
WithEnvVariable("GOCACHE", "/go/build-cache").
WithExec([]string{"sudo", "chown", "-R", "circleci", "/go/build-cache"}).
WithExec([]string{"sudo", "chown", "-R", "circleci", "/go/pkg/mod"})
}

if err != nil {
fmt.Println("Failed to run [base image setup]")
}
}

return dag
func (m *Ci) testImage(ctx context.Context) dagger.WithContainerFunc {
return func(ctr *dagger.Container) *dagger.Container {
return ctr.From(imageId).
With(m.baseImage(ctx)).
WithExec([]string{"go", "install", "github.com/kyoh86/richgo@v0.3.10"})
}

}

0 comments on commit 27bb8dd

Please sign in to comment.