rework pipeline

This commit is contained in:
Kasper Juul Hermansen 2022-10-30 18:13:57 +01:00
parent cadac9fc08
commit 2b641df577
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
15 changed files with 345 additions and 52 deletions

View File

@ -5,7 +5,7 @@ import (
"log"
"git.front.kjuulh.io/kjuulh/dagger-go/internal"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/tasks"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/pipelines"
)
func main() {
@ -15,7 +15,6 @@ func main() {
log.Fatal(err)
}
}
func run(ctx context.Context) error {
builder, err := internal.New(ctx)
if err != nil {
@ -23,5 +22,14 @@ func run(ctx context.Context) error {
}
defer builder.CleanUp()
return tasks.Build(builder, "some-image", "example/main.go")
return pipelines.
New(builder).
WithGolangBin(&pipelines.GolangBinOpts{
DockerImageOpt: &pipelines.DockerImageOpt{
ImageName: "golang-bin",
},
BuildPath: "example/golang-bin/main.go",
BinName: "golang-bin",
}).
Execute(ctx)
}

2
go.mod
View File

@ -8,7 +8,7 @@ require (
)
require (
git.front.kjuulh.io/kjuulh/byg v0.0.1 // indirect
git.front.kjuulh.io/kjuulh/byg v0.0.7 // indirect
github.com/Khan/genqlient v0.5.0 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/agext/levenshtein v1.2.3 // indirect

2
go.sum
View File

@ -37,6 +37,8 @@ dagger.io/dagger v0.3.1/go.mod h1:+p5s9Itrr/KT4UttGNpeUTNtVQUI2z9LDIOH9wBzu8g=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
git.front.kjuulh.io/kjuulh/byg v0.0.1 h1:SBrM5WBv1zTzt29pQMwGUJljGRI7Li7uATnZ3taotC0=
git.front.kjuulh.io/kjuulh/byg v0.0.1/go.mod h1:8Vg5Mgqzva5fzHGeMlxjp/DMLbUy9uaKFxMNB34yYuA=
git.front.kjuulh.io/kjuulh/byg v0.0.7 h1:0ZDkRj1R2lvbWUQO5GjWJum9zpVMej6l8ZXn5YNmBNE=
git.front.kjuulh.io/kjuulh/byg v0.0.7/go.mod h1:8Vg5Mgqzva5fzHGeMlxjp/DMLbUy9uaKFxMNB34yYuA=
github.com/99designs/gqlgen v0.17.2/go.mod h1:K5fzLKwtph+FFgh9j7nFbRUdBKvTcGnsta51fsMTn3o=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=

View File

@ -7,7 +7,7 @@ import (
"os"
"git.front.kjuulh.io/kjuulh/dagger-go/internal"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/tasks"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/pipelines"
"github.com/spf13/cobra"
)
@ -31,13 +31,23 @@ func Build(mainGoPath string, imageTag string) *cobra.Command {
log.Printf("Building image: %s\n", imageTag)
client, err := internal.New(ctx)
builder, err := internal.New(ctx)
if err != nil {
return err
}
defer client.CleanUp()
defer builder.CleanUp()
return pipelines.
New(builder).
WithGolangBin(&pipelines.GolangBinOpts{
DockerImageOpt: &pipelines.DockerImageOpt{
ImageName: "golang-bin",
},
BuildPath: "example/golang-bin/main.go",
BinName: "golang-bin",
}).
Execute(ctx)
return tasks.Build(client, imageTag, mainGoPath)
},
}

23
pkg/pipelines/default.go Normal file
View File

@ -0,0 +1,23 @@
package pipelines
import (
"context"
"log"
"git.front.kjuulh.io/kjuulh/byg"
)
func (p *Pipeline) WithDefault() error {
return byg.
New().
Step(
"default step",
byg.Step{
Execute: func(ctx byg.Context) error {
log.Println("Hello, world!")
return nil
},
}).
Execute(context.Background())
}

119
pkg/pipelines/golang-bin.go Normal file
View File

@ -0,0 +1,119 @@
package pipelines
import (
"context"
"fmt"
"log"
"strconv"
"time"
"dagger.io/dagger"
"git.front.kjuulh.io/kjuulh/byg"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/tasks/container"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/tasks/golang"
golangbin "git.front.kjuulh.io/kjuulh/dagger-go/pkg/tasks/golang-bin"
)
type DockerImageOpt struct {
ImageName string
ImageTag string
}
type GolangBinOpts struct {
*DockerImageOpt
BuildPath string
BinName string
}
func (p *Pipeline) WithGolangBin(opts *GolangBinOpts) *Pipeline {
log.Printf("building image: %s", opts.ImageName)
client := p.builder.Dagger
ctx := context.Background()
var (
bin dagger.FileID
build *dagger.Container
scratch *dagger.Container
finalImage *dagger.Container
)
pipeline := byg.
New().
Step(
"build golang",
byg.Step{
Execute: func(_ byg.Context) error {
var err error
c := container.LoadImage(client, "harbor.front.kjuulh.io/docker-proxy/library/golang")
c, err = container.MountCurrent(ctx, client, c, "/src")
if err != nil {
return err
}
c = container.Workdir(c, "/src")
build, err = golang.Cache(ctx, client, c)
if err != nil {
return err
}
bin, err = golangbin.Build(ctx, build, opts.BinName, opts.BuildPath)
if err != nil {
return err
}
return err
},
},
byg.Step{
Execute: func(ctx byg.Context) error {
scratch = container.LoadImage(client, "harbor.front.kjuulh.io/docker-proxy/library/busybox")
return nil
},
},
).
Step(
"create-production-image",
byg.Step{
Execute: func(ctx byg.Context) error {
tempmount := fmt.Sprintf("/tmp/%s", opts.BinName)
usrbin := fmt.Sprintf("/usr/bin/%s", opts.BinName)
c := container.MountFileFromLoaded(scratch, bin, tempmount)
c = c.Exec(dagger.ContainerExecOpts{
Args: []string{"mkdir", "-p", "/usr/bin"},
})
c = c.Exec(dagger.ContainerExecOpts{
Args: []string{"cp", tempmount, usrbin},
})
finalImage = c.WithEntrypoint([]string{opts.BinName})
return nil
},
},
byg.Step{
Execute: func(_ byg.Context) error {
return golang.Test(ctx, build)
},
},
).
Step(
"upload-image",
byg.Step{
Execute: func(_ byg.Context) error {
if opts.ImageTag == "" {
opts.ImageTag = strconv.FormatInt(time.Now().UTC().UnixMilli(), 10)
}
tag := fmt.Sprintf("harbor.front.kjuulh.io/kjuulh/%s:%s", opts.ImageName, opts.ImageTag)
_, err := finalImage.Publish(ctx, tag)
return err
},
},
)
p.add(pipeline)
return p
}

44
pkg/pipelines/pipeline.go Normal file
View File

@ -0,0 +1,44 @@
package pipelines
import (
"context"
"git.front.kjuulh.io/kjuulh/byg"
"git.front.kjuulh.io/kjuulh/dagger-go/internal"
"golang.org/x/sync/errgroup"
)
type Pipeline struct {
builder *internal.Builder
pipelines []*byg.Builder
}
func New(builder *internal.Builder) *Pipeline {
return &Pipeline{builder: builder}
}
func (p *Pipeline) WithCustom(custom func(p *Pipeline) *byg.Builder) {
p.add(custom(p))
}
func (p *Pipeline) Execute(ctx context.Context) error {
errgroup, ctx := errgroup.WithContext(ctx)
for _, pipeline := range p.pipelines {
pipeline := pipeline // Allocate for closure
errgroup.Go(func() error {
return pipeline.Execute(ctx)
})
}
if err := errgroup.Wait(); err != nil {
return err
}
return nil
}
func (p *Pipeline) add(pipeline *byg.Builder) {
p.pipelines = append(p.pipelines, pipeline)
}

View File

@ -1,44 +0,0 @@
package tasks
import (
"context"
"log"
"dagger.io/dagger"
"git.front.kjuulh.io/kjuulh/byg"
"git.front.kjuulh.io/kjuulh/dagger-go/internal"
)
func Build(builder *internal.Builder, imageTag string, buildPath string) error {
log.Printf("building image: %s", imageTag)
client := builder.Dagger
ctx := context.Background()
return byg.
New().
Step(
"build golang",
byg.Step{
Execute: func(_ byg.Context) error {
src, err := client.
Host().
Workdir().
Read().
ID(context.Background())
if err != nil {
return err
}
golang := client.Container().From("harbor.front.kjuulh.io/docker-proxy/library/golang:alpine")
golang = golang.WithMountedDirectory("/src", src).WithWorkdir("/src")
_, err = golang.Exec(dagger.ContainerExecOpts{
Args: []string{"go", "build", "-o", "build/", buildPath},
}).ExitCode(ctx)
return err
},
}).
Execute(context.Background())
}

View File

@ -0,0 +1,13 @@
package container
import (
"log"
"dagger.io/dagger"
)
func CreateScratch(client *dagger.Client) *dagger.Container {
log.Println("creating scratch image")
return client.Container(dagger.ContainerOpts{ID: ""})
}

View File

@ -0,0 +1,13 @@
package container
import (
"log"
"dagger.io/dagger"
)
func LoadImage(client *dagger.Client, image string) *dagger.Container {
log.Printf("loading image: %s", image)
return client.Container().From(image)
}

View File

@ -0,0 +1,27 @@
package container
import (
"context"
"log"
"dagger.io/dagger"
)
func MountCurrent(ctx context.Context, client *dagger.Client, container *dagger.Container, into string) (*dagger.Container, error) {
log.Printf("mounting current working directory into path: %s", into)
src, err := client.
Host().
Workdir().
Read().
ID(ctx)
if err != nil {
return nil, err
}
return container.WithMountedDirectory(into, src), nil
}
func MountFileFromLoaded(container *dagger.Container, bin dagger.FileID, path string) *dagger.Container {
log.Printf("mounting binary into container: into (path=%s)", path)
return container.WithMountedFile(path, bin)
}

View File

@ -0,0 +1,9 @@
package container
import (
"dagger.io/dagger"
)
func Workdir(container *dagger.Container, into string) *dagger.Container {
return container.WithWorkdir(into)
}

View File

@ -0,0 +1,29 @@
package golangbin
import (
"context"
"fmt"
"log"
"dagger.io/dagger"
)
func Build(ctx context.Context, container *dagger.Container, binname string, buildpath string) (dagger.FileID, error) {
log.Printf("building binary: (binName=%s) into (buildPath=%s)", binname, buildpath)
binpath := fmt.Sprintf("dist/%s", binname)
c := container.Exec(dagger.ContainerExecOpts{
Args: []string{"go", "build", "-o", binpath, buildpath},
})
_, err := c.ExitCode(ctx)
if err != nil {
return "", err
}
bin, err := c.File(binpath).ID(ctx)
if err != nil {
return "", err
}
return bin, nil
}

18
pkg/tasks/golang/cache.go Normal file
View File

@ -0,0 +1,18 @@
package golang
import (
"context"
"dagger.io/dagger"
)
func Cache(ctx context.Context, client *dagger.Client, container *dagger.Container) (*dagger.Container, error) {
cacheKey := "gomods"
cacheID, err := client.CacheVolume(cacheKey).ID(ctx)
if err != nil {
return nil, err
}
return container.
WithMountedCache(cacheID, "/cache").
WithEnvVariable("GOMODCACHE", "/cache"), nil
}

22
pkg/tasks/golang/test.go Normal file
View File

@ -0,0 +1,22 @@
package golang
import (
"context"
"log"
"dagger.io/dagger"
)
func Test(ctx context.Context, container *dagger.Container) error {
log.Printf("testing: image")
c := container.Exec(dagger.ContainerExecOpts{
Args: []string{"go", "test", "./..."},
})
_, err := c.ExitCode(ctx)
if err != nil {
return err
}
return nil
}