2022-10-30 18:13:57 +01:00
|
|
|
package pipelines
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-10-31 00:13:07 +01:00
|
|
|
"path"
|
2022-10-30 18:13:57 +01:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"dagger.io/dagger"
|
2022-10-31 20:16:26 +01:00
|
|
|
"git.front.kjuulh.io/kjuulh/bust/pkg/tasks/container"
|
|
|
|
"git.front.kjuulh.io/kjuulh/bust/pkg/tasks/golang"
|
|
|
|
golangbin "git.front.kjuulh.io/kjuulh/bust/pkg/tasks/golang-bin"
|
2022-11-06 00:39:03 +01:00
|
|
|
"git.front.kjuulh.io/kjuulh/byg"
|
2022-10-30 18:13:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type DockerImageOpt struct {
|
|
|
|
ImageName string
|
|
|
|
ImageTag string
|
|
|
|
}
|
|
|
|
|
|
|
|
type GolangBinOpts struct {
|
|
|
|
*DockerImageOpt
|
2022-10-31 00:26:44 +01:00
|
|
|
BuildPath string
|
|
|
|
BinName string
|
|
|
|
BaseImage string
|
|
|
|
ExecuteOnEntrypoint bool
|
2022-10-31 02:02:00 +01:00
|
|
|
CGOEnabled bool
|
2022-10-30 18:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
finalImage *dagger.Container
|
|
|
|
)
|
|
|
|
|
|
|
|
pipeline := byg.
|
|
|
|
New().
|
|
|
|
Step(
|
|
|
|
"build golang",
|
|
|
|
byg.Step{
|
|
|
|
Execute: func(_ byg.Context) error {
|
|
|
|
var err error
|
2023-05-15 21:57:27 +02:00
|
|
|
c := container.LoadImage(client, "harbor.front.kjuulh.io/docker-proxy/library/golang")
|
2022-10-30 18:13:57 +01:00
|
|
|
c, err = container.MountCurrent(ctx, client, c, "/src")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c = container.Workdir(c, "/src")
|
|
|
|
|
2022-10-31 02:02:00 +01:00
|
|
|
if opts.CGOEnabled {
|
|
|
|
c = c.WithEnvVariable("CGO_ENABLED", "1")
|
|
|
|
} else {
|
|
|
|
c = c.WithEnvVariable("CGO_ENABLED", "0")
|
|
|
|
}
|
|
|
|
|
2022-10-30 18:13:57 +01:00
|
|
|
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
|
|
|
|
},
|
|
|
|
},
|
2022-10-30 21:25:14 +01:00
|
|
|
).
|
|
|
|
Step(
|
|
|
|
"create-production-image",
|
2022-10-30 18:13:57 +01:00
|
|
|
byg.Step{
|
2022-10-30 22:37:30 +01:00
|
|
|
Execute: func(_ byg.Context) error {
|
2022-10-30 19:57:04 +01:00
|
|
|
if opts.BaseImage == "" {
|
2023-05-15 21:57:27 +02:00
|
|
|
opts.BaseImage = "harbor.front.kjuulh.io/docker-proxy/library/alpine"
|
2022-10-30 19:57:04 +01:00
|
|
|
}
|
2022-10-30 22:27:04 +01:00
|
|
|
|
2022-10-31 00:13:07 +01:00
|
|
|
binpath := "/usr/bin"
|
|
|
|
usrbin := path.Join(binpath, opts.BinName)
|
2022-10-30 22:27:04 +01:00
|
|
|
c := container.LoadImage(client, opts.BaseImage)
|
2022-10-31 00:13:07 +01:00
|
|
|
c = c.Exec(dagger.ContainerExecOpts{
|
|
|
|
Args: []string{"mkdir", "-p", binpath},
|
|
|
|
})
|
2022-10-31 00:35:40 +01:00
|
|
|
_, err := c.ExitCode(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err = container.MountFileFromLoaded(ctx, c, bin, usrbin)
|
2022-10-30 22:37:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-31 00:26:44 +01:00
|
|
|
if opts.ExecuteOnEntrypoint {
|
|
|
|
finalImage = c.WithEntrypoint([]string{usrbin})
|
|
|
|
} else {
|
|
|
|
finalImage = c
|
|
|
|
}
|
2022-10-30 18:13:57 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
2022-10-31 01:48:32 +01:00
|
|
|
//byg.Step{
|
|
|
|
// Execute: func(_ byg.Context) error {
|
|
|
|
// return golang.Test(ctx, build)
|
|
|
|
// },
|
|
|
|
//},
|
2022-10-30 18:13:57 +01:00
|
|
|
).
|
|
|
|
Step(
|
|
|
|
"upload-image",
|
|
|
|
byg.Step{
|
|
|
|
Execute: func(_ byg.Context) error {
|
|
|
|
|
|
|
|
if opts.ImageTag == "" {
|
|
|
|
opts.ImageTag = strconv.FormatInt(time.Now().UTC().UnixMilli(), 10)
|
|
|
|
}
|
|
|
|
|
2023-05-15 21:57:27 +02:00
|
|
|
tag := fmt.Sprintf("harbor.front.kjuulh.io/kjuulh/%s:%s", opts.ImageName, opts.ImageTag)
|
2022-10-30 18:13:57 +01:00
|
|
|
|
|
|
|
_, err := finalImage.Publish(ctx, tag)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
p.add(pipeline)
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|