ci: add cache and named pipelines
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/pr/woodpecker Pipeline was successful

This commit is contained in:
Dan Anglin 2023-05-20 05:48:22 +01:00
parent 07549bdffb
commit a6bec1f400
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 19 additions and 11 deletions

View file

@ -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)
}
}

View file

@ -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)