11 Commits

Author SHA1 Message Date
2c06aa4353 with golang bin 2022-10-30 18:34:23 +01:00
18f7e1fc27 with actual cli 2022-10-30 18:33:59 +01:00
2b641df577 rework pipeline 2022-10-30 18:13:57 +01:00
cadac9fc08 with alpine 2022-10-29 23:39:21 +02:00
a872bad97d with more generic way, but still wonky 2022-10-29 23:33:46 +02:00
0838bc5a56 with main.go path 2022-10-29 23:27:43 +02:00
c4ca5f25a0 default build.go 2022-10-29 23:25:49 +02:00
f89936200e with buildpath 2022-10-29 23:25:31 +02:00
e9dd36f1e4 with tmp dir 2022-10-29 23:12:06 +02:00
001742340c with defined dir 2022-10-29 23:08:07 +02:00
dfa693ea08 with base src/ 2022-10-29 23:06:06 +02:00
23 changed files with 454 additions and 118 deletions

View File

@@ -8,7 +8,7 @@ RUN go build -o dist/dagger-go main.go
FROM harbor.front.kjuulh.io/docker-proxy/library/docker:dind FROM harbor.front.kjuulh.io/docker-proxy/library/docker:dind
WORKDIR /src/docker WORKDIR /src
COPY --from=builder /src/builder/dist/dagger-go /usr/bin/ COPY --from=builder /src/builder/dist/dagger-go /usr/bin/

View File

@@ -1,5 +1,3 @@
{{ $imageTag := "harbor.front.kjuulh.io/library/${DRONE_REPO_NAME}" }}
type: docker type: docker
kind: pipeline kind: pipeline
name: "drone-dagger-test" name: "drone-dagger-test"
@@ -18,7 +16,7 @@ steps:
- mkdir -p tmp/dagger-go - mkdir -p tmp/dagger-go
- git clone "https://git.front.kjuulh.io/kjuulh/dagger-go.git" tmp/dagger-go - git clone "https://git.front.kjuulh.io/kjuulh/dagger-go.git" tmp/dagger-go
- docker build -t dagger-go-builder:${DRONE_COMMIT} -f tmp/dagger-go/Dockerfile . - docker build -t dagger-go-builder:${DRONE_COMMIT} -f tmp/dagger-go/Dockerfile .
- 'docker run -v "$PWD/:/src/build" -v /var/run/docker.sock:/var/run/docker.sock dagger-go-builder:${DRONE_COMMIT} dagger-go build --image-tag "{{ or .input.imageTag $imageTag }}"' - 'docker run -v "$PWD/:/src/" -e DRONE_REPO_NAME="${DRONE_REPO_NAME}" -v /var/run/docker.sock:/var/run/docker.sock dagger-go-builder:${DRONE_COMMIT} dagger-go build golangbin'
services: services:
- name: docker - name: docker

13
example/cli/main.go Normal file
View File

@@ -0,0 +1,13 @@
package main
import (
"log"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/cli"
)
func main() {
if err := cli.NewCli().Execute(); err != nil {
log.Fatal(err)
}
}

View File

@@ -0,0 +1,35 @@
package main
import (
"context"
"log"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/builder"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/pipelines"
)
func main() {
ctx := context.Background()
if err := run(ctx); err != nil {
log.Fatal(err)
}
}
func run(ctx context.Context) error {
builder, err := builder.New(ctx)
if err != nil {
return err
}
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)
}

2
go.mod
View File

@@ -8,7 +8,7 @@ require (
) )
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/Khan/genqlient v0.5.0 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/agext/levenshtein v1.2.3 // 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= 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 h1:SBrM5WBv1zTzt29pQMwGUJljGRI7Li7uATnZ3taotC0=
git.front.kjuulh.io/kjuulh/byg v0.0.1/go.mod h1:8Vg5Mgqzva5fzHGeMlxjp/DMLbUy9uaKFxMNB34yYuA= 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/99designs/gqlgen v0.17.2/go.mod h1:K5fzLKwtph+FFgh9j7nFbRUdBKvTcGnsta51fsMTn3o=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 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= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=

13
main.go Normal file
View File

@@ -0,0 +1,13 @@
package main
import (
"log"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/cli"
)
func main() {
if err := cli.NewCli().Execute(); err != nil {
log.Fatal(err)
}
}

View File

@@ -1,4 +1,4 @@
package internal package builder
import ( import (
"context" "context"

15
pkg/cli/build.go Normal file
View File

@@ -0,0 +1,15 @@
package cli
import (
"github.com/spf13/cobra"
)
func Build() *cobra.Command {
cmd := &cobra.Command{
Use: "build",
}
cmd.AddCommand(BuildGolangBin())
return cmd
}

View File

@@ -0,0 +1,45 @@
package cli
import (
"errors"
"fmt"
"os"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/builder"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/pipelines"
"github.com/spf13/cobra"
)
func BuildGolangBin() *cobra.Command {
cmd := &cobra.Command{
Use: "golangbin",
RunE: func(cmd *cobra.Command, args []string) error {
repoName := os.Getenv("DRONE_REPO_NAME")
if repoName == "" {
return errors.New("could not find DRONE_REPO_NAME")
}
imageTag := fmt.Sprintf("harbor.front.kjuulh.io/library/%s", repoName)
ctx := cmd.Context()
builder, err := builder.New(ctx)
if err != nil {
return err
}
defer builder.CleanUp()
return pipelines.
New(builder).
WithGolangBin(&pipelines.GolangBinOpts{
DockerImageOpt: &pipelines.DockerImageOpt{
ImageName: imageTag,
},
BuildPath: "main.go",
BinName: "main",
}).
Execute(ctx)
},
}
return cmd
}

View File

@@ -1,41 +0,0 @@
package cli
import (
"log"
"git.front.kjuulh.io/kjuulh/dagger-go/internal"
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/tasks"
"github.com/spf13/cobra"
)
func Build() *cobra.Command {
var (
imageTag string
)
cmd := &cobra.Command{
Use: "build",
RunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.ParseFlags(args); err != nil {
return err
}
ctx := cmd.Context()
log.Printf("Building image: %s\n", imageTag)
client, err := internal.New(ctx)
if err != nil {
return err
}
defer client.CleanUp()
return tasks.Build(client, imageTag)
},
}
cmd.PersistentFlags().StringVar(&imageTag, "image-tag", "", "the url for which to tag the docker image, defaults to private url, with repo as image name")
cmd.MarkPersistentFlagRequired("image-tag")
return cmd
}

13
pkg/cli/root.go Normal file
View File

@@ -0,0 +1,13 @@
package cli
import "github.com/spf13/cobra"
func NewCli() *cobra.Command {
cmd := &cobra.Command{
Use: "dagger",
}
cmd.AddCommand(Build())
return cmd
}

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

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

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/pkg/builder"
"golang.org/x/sync/errgroup"
)
type Pipeline struct {
builder *builder.Builder
pipelines []*byg.Builder
}
func New(builder *builder.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,71 +0,0 @@
package tasks
import (
"context"
"log"
"os"
"dagger.io/dagger"
"git.front.kjuulh.io/kjuulh/byg"
"git.front.kjuulh.io/kjuulh/dagger-go/internal"
)
func Build(builder *internal.Builder, imageTag 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
}
log.Println("listing files in /src/build")
dir, err := os.ReadDir("/src/build")
if err == nil {
for _, d := range dir {
log.Printf("content: %s\n", d.Name())
}
} else {
return err
}
log.Println("listing files in /src/docker")
dir, err = os.ReadDir("/src/docker")
if err == nil {
for _, d := range dir {
log.Printf("content: %s\n", d.Name())
}
}
log.Println("listing files in /src/")
dir, err = os.ReadDir("/src/")
if err == nil {
for _, d := range dir {
log.Printf("content: %s\n", d.Name())
}
}
golang := client.Container().From("golang:latest")
golang = golang.WithMountedDirectory("/src/build", src).WithWorkdir("/src")
_, err = golang.Exec(dagger.ContainerExecOpts{
Args: []string{"go", "build", "-o", "build/"},
}).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
}