bust/pkg/pipelines/rust-bin.go

135 lines
2.7 KiB
Go
Raw Permalink Normal View History

2022-11-06 00:39:03 +01:00
package pipelines
import (
"context"
"fmt"
"log"
"path"
"strconv"
"time"
"dagger.io/dagger"
"git.front.kjuulh.io/kjuulh/bust/pkg/tasks/container"
rustbin "git.front.kjuulh.io/kjuulh/bust/pkg/tasks/rust-bin"
"git.front.kjuulh.io/kjuulh/byg"
)
type RustBinOpts struct {
*DockerImageOpt
BinName string
BaseImage string
}
func (p *Pipeline) WithRustBin(opts *RustBinOpts) *Pipeline {
log.Printf("building image: %s", opts.ImageName)
client := p.builder.Dagger
ctx := context.Background()
var (
bin dagger.FileID
finalImage *dagger.Container
)
pipeline := byg.
New().
Step(
"build rust",
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/rust:buster")
2022-11-06 00:59:01 +01:00
c = c.Exec(dagger.ContainerExecOpts{
Args: []string{
"apt", "update", "-y",
},
})
if _, err := c.ExitCode(ctx); err != nil {
return err
}
c = c.Exec(dagger.ContainerExecOpts{
Args: []string{
"apt", "install", "musl-tools", "-y",
},
})
if _, err := c.ExitCode(ctx); err != nil {
return err
}
c = c.Exec(dagger.ContainerExecOpts{
Args: []string{
"rustup",
"target",
"add",
"x86_64-unknown-linux-musl",
2022-11-06 00:59:01 +01:00
},
})
if _, err := c.ExitCode(ctx); err != nil {
return err
}
2022-11-06 00:39:03 +01:00
c, err = container.MountCurrent(ctx, client, c, "/src")
if err != nil {
return err
}
c = container.Workdir(c, "/src")
if bin, err = rustbin.Build(ctx, c, opts.BinName); err != nil {
return err
}
return err
},
},
).
Step(
"create-production-image",
byg.Step{
Execute: func(_ byg.Context) error {
if opts.BaseImage == "" {
2023-05-15 21:57:27 +02:00
opts.BaseImage = "harbor.front.kjuulh.io/docker-proxy/library/alpine"
2022-11-06 00:39:03 +01:00
}
binpath := "/usr/bin"
usrbin := path.Join(binpath, opts.BinName)
c := container.LoadImage(client, opts.BaseImage)
c = c.Exec(dagger.ContainerExecOpts{
Args: []string{"mkdir", "-p", binpath},
})
_, err := c.ExitCode(ctx)
if err != nil {
return err
}
c, err = container.MountFileFromLoaded(ctx, c, bin, usrbin)
if err != nil {
return err
}
finalImage = c
return nil
},
},
).
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-11-06 00:39:03 +01:00
_, err := finalImage.Publish(ctx, tag)
return err
},
},
)
p.add(pipeline)
return p
}