Compare commits

...

2 commits

Author SHA1 Message Date
f7805184a0
Merge branch 'cache-and-named-pipelines'
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2023-05-20 06:16:23 +01:00
a6bec1f400
ci: add cache and named pipelines
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/pr/woodpecker Pipeline was successful
2023-05-20 05:48:22 +01:00
2 changed files with 19 additions and 11 deletions

View file

@ -26,6 +26,9 @@ func run(ctx context.Context) error {
} }
defer client.Close() defer client.Close()
client = client.Pipeline("Greet Build Pipeline")
cache := client.CacheVolume("go-mod-cache")
projectRoot, err := filepath.Abs("../..") projectRoot, err := filepath.Abs("../..")
if err != nil { if err != nil {
return fmt.Errorf("unable to get the project's root directory, %w", err) 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 { 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) return fmt.Errorf("error running pipeline; %w", err)
} }
} }

View file

@ -1,30 +1,33 @@
package main package main
import ( import (
"log"
"context" "context"
"fmt" "fmt"
"log"
"dagger.io/dagger" "dagger.io/dagger"
) )
type stage struct { type stage struct {
name string name string
projectRoot 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) 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 fmt.Errorf("error running stage '%s'; %w", s.name, err)
} }
return nil return nil
} }
// test is a stage that runs the test suite using mage // test is a stage that runs the test suite using mage.
func test(ctx context.Context, client *dagger.Client, projectRoot string) error { func test(ctx context.Context, client *dagger.Client, projectRoot string, cache *dagger.CacheVolume) error {
image := "golang:1.20.4" image := "golang:1.20.4"
containerWorkspace := "/workspace" containerWorkspace := "/workspace"
containerWorkdir := containerWorkspace + "/internal/build" containerWorkdir := containerWorkspace + "/internal/build"
@ -35,15 +38,16 @@ func test(ctx context.Context, client *dagger.Client, projectRoot string) error
WithWorkdir(containerWorkdir). WithWorkdir(containerWorkdir).
WithEnvVariable("GO_TEST_VERBOSE", "1"). WithEnvVariable("GO_TEST_VERBOSE", "1").
WithEnvVariable("GO_TEST_COVER", "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) _, err := runner.Stdout(ctx)
return err return err
} }
// lint is a stage that runs the linter using mage // lint is a stage that runs the linter using mage.
func lint(ctx context.Context, client *dagger.Client, projectRoot string) error { func lint(ctx context.Context, client *dagger.Client, projectRoot string, cache *dagger.CacheVolume) error {
image := "golangci/golangci-lint:v1.52.2-alpine" image := "golangci/golangci-lint:v1.52.2-alpine"
containerWorkspace := "/workspace" containerWorkspace := "/workspace"
containerWorkdir := containerWorkspace + "/internal/build" containerWorkdir := containerWorkspace + "/internal/build"
@ -52,7 +56,8 @@ func lint(ctx context.Context, client *dagger.Client, projectRoot string) error
From(image). From(image).
WithMountedDirectory(containerWorkspace, client.Host().Directory(projectRoot)). WithMountedDirectory(containerWorkspace, client.Host().Directory(projectRoot)).
WithWorkdir(containerWorkdir). 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) _, err := runner.Stdout(ctx)