This commit is contained in:
@@ -9,7 +9,10 @@ func Build() *cobra.Command {
|
||||
Use: "build",
|
||||
}
|
||||
|
||||
cmd.AddCommand(BuildGolangBin())
|
||||
cmd.AddCommand(
|
||||
BuildGolangBin(),
|
||||
BuildDocker(),
|
||||
)
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
40
pkg/cli/build_docker.go
Normal file
40
pkg/cli/build_docker.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"git.front.kjuulh.io/kjuulh/bust/pkg/builder"
|
||||
"git.front.kjuulh.io/kjuulh/bust/pkg/pipelines"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func BuildDocker() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "docker",
|
||||
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")
|
||||
}
|
||||
|
||||
ctx := cmd.Context()
|
||||
|
||||
builder, err := builder.New(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer builder.CleanUp()
|
||||
|
||||
return pipelines.
|
||||
New(builder).
|
||||
WithDocker(&pipelines.DockerOpt{
|
||||
DockerImageOpt: &pipelines.DockerImageOpt{ImageName: repoName},
|
||||
Path: "Dockerfile",
|
||||
}).
|
||||
Execute(ctx)
|
||||
},
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
70
pkg/pipelines/docker.go
Normal file
70
pkg/pipelines/docker.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package pipelines
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"dagger.io/dagger"
|
||||
"git.front.kjuulh.io/kjuulh/byg"
|
||||
)
|
||||
|
||||
type DockerOpt struct {
|
||||
*DockerImageOpt
|
||||
Path string
|
||||
}
|
||||
|
||||
func (p *Pipeline) WithDocker(opts *DockerOpt) *Pipeline {
|
||||
log.Printf("building image: %s", opts.ImageName)
|
||||
|
||||
client := p.builder.Dagger
|
||||
ctx := context.Background()
|
||||
|
||||
var (
|
||||
finalImage *dagger.Container
|
||||
)
|
||||
|
||||
pipeline := byg.
|
||||
New().
|
||||
Step(
|
||||
"build image",
|
||||
byg.Step{
|
||||
Execute: func(_ byg.Context) error {
|
||||
var err error
|
||||
|
||||
dir, err := client.Host().Workdir().Read().ID(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
finalImage = client.Container().Build(dir, dagger.ContainerBuildOpts{Dockerfile: opts.Path})
|
||||
if _, err = finalImage.ExitCode(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
},
|
||||
).
|
||||
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.server.kjuulh.io/kjuulh/%s:%s", opts.ImageName, opts.ImageTag)
|
||||
|
||||
_, err := finalImage.Publish(ctx, tag)
|
||||
return err
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
p.add(pipeline)
|
||||
|
||||
return p
|
||||
}
|
Reference in New Issue
Block a user