greet/internal/build/stages.go
Dan Anglin c2a1e28773
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/pr/woodpecker Pipeline was successful
ci: add CI pipelines
Summary:

Add simple CI pipelines to the greet project. Here we add CI pipelines
for both Woodpecker and Dagger CI as we are currently interested in both
technologies.

Changes:

- Add .woodpecker/woodpecker.yml
- Updated LICENSE
- Updated README.md
- Fixed linting issues in greet.go
- Created a new internal go module for CI/CD in internal/build
- Moved the magefiles to internal/build/magefiles
- Add dagger pipeline code
2023-05-13 23:06:25 +01:00

60 lines
1.6 KiB
Go

package main
import (
"log"
"context"
"fmt"
"dagger.io/dagger"
)
type stage struct {
name string
projectRoot string
stageFunc func(ctx context.Context, client *dagger.Client, projectRoot string) error
}
func (s *stage) run(ctx context.Context, client *dagger.Client) error {
log.Printf("Running stage: %s", s.name)
if err := s.stageFunc(ctx, client, s.projectRoot); 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 {
image := "golang:1.20.4"
containerWorkspace := "/workspace"
containerWorkdir := containerWorkspace + "/internal/build"
runner := client.Container().
From(image).
WithMountedDirectory(containerWorkspace, client.Host().Directory(projectRoot)).
WithWorkdir(containerWorkdir).
WithEnvVariable("GO_TEST_VERBOSE", "1").
WithEnvVariable("GO_TEST_COVER", "1").
WithExec([]string{"go", "run", "magefiles/main.go", "-v", "test"})
_, 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 {
image := "golangci/golangci-lint:v1.52.2-alpine"
containerWorkspace := "/workspace"
containerWorkdir := containerWorkspace + "/internal/build"
runner := client.Container().
From(image).
WithMountedDirectory(containerWorkspace, client.Host().Directory(projectRoot)).
WithWorkdir(containerWorkdir).
WithExec([]string{"go", "run", "magefiles/main.go", "-v", "lint"})
_, err := runner.Stdout(ctx)
return err
}