diff --git a/pkg/cli/build.go b/pkg/cli/build.go index 1bb55b1..33c9822 100644 --- a/pkg/cli/build.go +++ b/pkg/cli/build.go @@ -9,7 +9,10 @@ func Build() *cobra.Command { Use: "build", } - cmd.AddCommand(BuildGolangBin()) + cmd.AddCommand( + BuildGolangBin(), + BuildDocker(), + ) return cmd } diff --git a/pkg/cli/build_docker.go b/pkg/cli/build_docker.go new file mode 100644 index 0000000..ec76932 --- /dev/null +++ b/pkg/cli/build_docker.go @@ -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 +} diff --git a/pkg/pipelines/docker.go b/pkg/pipelines/docker.go new file mode 100644 index 0000000..edf4b32 --- /dev/null +++ b/pkg/pipelines/docker.go @@ -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 +} diff --git a/templates/bust_docker_template.yaml b/templates/bust_docker_template.yaml new file mode 100644 index 0000000..ca26732 --- /dev/null +++ b/templates/bust_docker_template.yaml @@ -0,0 +1,40 @@ +type: docker +kind: pipeline +name: "drone-dagger-test" + +steps: + - name: "build" + image: harbor.server.kjuulh.io/kjuulh/bust:1667244085545 + volumes: + - name: dockersock + path: /var/run + environment: + DOCKER_BUILDKIT: 1 + HARBOR_DOCKER_HOST: "harbor.server.kjuulh.io" + HARBOR_DOCKER_USERNAME: + from_secret: "harbor_docker_username" + HARBOR_DOCKER_PASSWORD: + from_secret: "harbor_docker_password" + commands: + - sleep 5 + - > + echo "$${HARBOR_DOCKER_PASSWORD}" | docker login + --password-stdin + --username="$${HARBOR_DOCKER_USERNAME}" + "$${HARBOR_DOCKER_HOST}" + - bust build docker + +services: + - name: docker + image: docker:dind + privileged: true + volumes: + - name: dockersock + path: /var/run + +volumes: + - name: dockersock + temp: {} + +image_pull_secrets: + - dockerconfig diff --git a/bust_gobin_default_template.yaml b/templates/bust_gobin_default_template.yaml similarity index 100% rename from bust_gobin_default_template.yaml rename to templates/bust_gobin_default_template.yaml diff --git a/templates/bust_gobin_template.yaml b/templates/bust_gobin_template.yaml new file mode 100644 index 0000000..cb9d7ce --- /dev/null +++ b/templates/bust_gobin_template.yaml @@ -0,0 +1,56 @@ +type: docker +kind: pipeline +name: "drone-dagger-test" + +steps: + - name: "build" + image: harbor.server.kjuulh.io/docker-proxy/library/docker:dind + volumes: + - name: dockersock + path: /var/run + environment: + DOCKER_BUILDKIT: 1 + HARBOR_DOCKER_HOST: "harbor.server.kjuulh.io" + HARBOR_DOCKER_USERNAME: + from_secret: "harbor_docker_username" + HARBOR_DOCKER_PASSWORD: + from_secret: "harbor_docker_password" + commands: + - sleep 5 + - apk add git + - mkdir -p tmp/bust + - git clone "https://git.front.kjuulh.io/kjuulh/bust.git" tmp/bust + - > + echo "$${HARBOR_DOCKER_PASSWORD}" | docker login + --password-stdin + --username="$${HARBOR_DOCKER_USERNAME}" + "$${HARBOR_DOCKER_HOST}" + - > + docker pull harbor.server.kjuulh.io/kjuulh/bust-builder:${DRONE_COMMIT} || + (docker build -t harbor.server.kjuulh.io/kjuulh/bust-builder:${DRONE_COMMIT} -f tmp/bust/Dockerfile . && docker push harbor.server.kjuulh.io/kjuulh/bust-builder:${DRONE_COMMIT}) + - > + docker run + -e DRONE_REPO_NAME="${DRONE_REPO_NAME}" + -e HARBOR_DOCKER_HOST=$${HARBOR_DOCKER_HOST} + -e HARBOR_DOCKER_USERNAME=$${HARBOR_DOCKER_USERNAME} + -e HARBOR_DOCKER_PASSWORD=$${HARBOR_DOCKER_PASSWORD} + -v "$PWD/:/src/" + -v /var/run/docker.sock:/var/run/docker.sock + harbor.server.kjuulh.io/kjuulh/bust-builder:${DRONE_COMMIT} + sh -c 'echo "$$HARBOR_DOCKER_PASSWORD" | docker login + --password-stdin + --username="$$HARBOR_DOCKER_USERNAME" + "$${HARBOR_DOCKER_HOST}" + && bust build golangbin' + +services: + - name: docker + image: docker:dind + privileged: true + volumes: + - name: dockersock + path: /var/run + +volumes: + - name: dockersock + temp: {} diff --git a/uploadtemplate.sh b/templates/uploadtemplate.sh similarity index 100% rename from uploadtemplate.sh rename to templates/uploadtemplate.sh diff --git a/tmp/.drone.yml b/tmp/.drone.yml deleted file mode 100644 index d3e7718..0000000 --- a/tmp/.drone.yml +++ /dev/null @@ -1,4 +0,0 @@ -kind: template -load: bust_gobin_default_template.yaml -name: something -data: {}