From a6bec1f400564b8c599fc47b09f99529711235d1 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sat, 20 May 2023 05:48:22 +0100 Subject: [PATCH] ci: add cache and named pipelines --- internal/build/{dagger.go => build.go} | 5 ++++- internal/build/stages.go | 25 +++++++++++++++---------- 2 files changed, 19 insertions(+), 11 deletions(-) rename internal/build/{dagger.go => build.go} (85%) diff --git a/internal/build/dagger.go b/internal/build/build.go similarity index 85% rename from internal/build/dagger.go rename to internal/build/build.go index d802199..644702b 100644 --- a/internal/build/dagger.go +++ b/internal/build/build.go @@ -26,6 +26,9 @@ func run(ctx context.Context) error { } defer client.Close() + client = client.Pipeline("Greet Build Pipeline") + cache := client.CacheVolume("go-mod-cache") + projectRoot, err := filepath.Abs("../..") if err != nil { return fmt.Errorf("unable to get the project's root directory, %w", err) @@ -45,7 +48,7 @@ func run(ctx context.Context) error { } for i := range stages { - if err := stages[i].run(ctx, client); err != nil { + if err := stages[i].run(ctx, client, cache); err != nil { return fmt.Errorf("error running pipeline; %w", err) } } diff --git a/internal/build/stages.go b/internal/build/stages.go index a42cd42..e750918 100644 --- a/internal/build/stages.go +++ b/internal/build/stages.go @@ -1,30 +1,33 @@ package main import ( - "log" "context" "fmt" + "log" + "dagger.io/dagger" ) type stage struct { name string projectRoot string - stageFunc func(ctx context.Context, client *dagger.Client, projectRoot string) error + stageFunc func(ctx context.Context, client *dagger.Client, projectRoot string, cache *dagger.CacheVolume) error } -func (s *stage) run(ctx context.Context, client *dagger.Client) error { +func (s *stage) run(ctx context.Context, client *dagger.Client, cache *dagger.CacheVolume) error { log.Printf("Running stage: %s", s.name) - if err := s.stageFunc(ctx, client, s.projectRoot); err != nil { + pipeline := client.Pipeline(s.name) + + if err := s.stageFunc(ctx, pipeline, s.projectRoot, cache); err != nil { return fmt.Errorf("error running stage '%s'; %w", s.name, err) } return nil } -// test is a stage that runs the test suite using mage -func test(ctx context.Context, client *dagger.Client, projectRoot string) error { +// test is a stage that runs the test suite using mage. +func test(ctx context.Context, client *dagger.Client, projectRoot string, cache *dagger.CacheVolume) error { image := "golang:1.20.4" containerWorkspace := "/workspace" containerWorkdir := containerWorkspace + "/internal/build" @@ -35,15 +38,16 @@ func test(ctx context.Context, client *dagger.Client, projectRoot string) error WithWorkdir(containerWorkdir). WithEnvVariable("GO_TEST_VERBOSE", "1"). WithEnvVariable("GO_TEST_COVER", "1"). - WithExec([]string{"go", "run", "magefiles/main.go", "-v", "test"}) + WithExec([]string{"go", "run", "magefiles/main.go", "-v", "test"}). + WithMountedCache("/go/pkg/mod", cache) _, err := runner.Stdout(ctx) return err } -// lint is a stage that runs the linter using mage -func lint(ctx context.Context, client *dagger.Client, projectRoot string) error { +// lint is a stage that runs the linter using mage. +func lint(ctx context.Context, client *dagger.Client, projectRoot string, cache *dagger.CacheVolume) error { image := "golangci/golangci-lint:v1.52.2-alpine" containerWorkspace := "/workspace" containerWorkdir := containerWorkspace + "/internal/build" @@ -52,7 +56,8 @@ func lint(ctx context.Context, client *dagger.Client, projectRoot string) error From(image). WithMountedDirectory(containerWorkspace, client.Host().Directory(projectRoot)). WithWorkdir(containerWorkdir). - WithExec([]string{"go", "run", "magefiles/main.go", "-v", "lint"}) + WithExec([]string{"go", "run", "magefiles/main.go", "-v", "lint"}). + WithMountedCache("/go/pkg/mod", cache) _, err := runner.Stdout(ctx)