with docker
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Kasper Juul Hermansen 2022-10-31 21:57:16 +01:00
parent 03fb65a9b4
commit f44ef84c89
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
8 changed files with 210 additions and 5 deletions

View File

@ -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
View 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
View 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
}

View File

@ -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

View File

@ -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: {}

View File

@ -1,4 +0,0 @@
kind: template
load: bust_gobin_default_template.yaml
name: something
data: {}