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

This commit is contained in:
Kasper Juul Hermansen 2022-11-06 00:39:03 +01:00
parent c6c42c4d08
commit 6c885ec33c
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
17 changed files with 312 additions and 3 deletions

View File

@ -0,0 +1 @@
target/

1
example/rust-bin/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
target/

7
example/rust-bin/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "rust-bin"
version = "0.1.0"

View File

@ -0,0 +1,8 @@
[package]
name = "rust-bin"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

34
example/rust-bin/main.go Normal file
View File

@ -0,0 +1,34 @@
package main
import (
"context"
"log"
"git.front.kjuulh.io/kjuulh/bust/pkg/builder"
"git.front.kjuulh.io/kjuulh/bust/pkg/pipelines"
)
func main() {
ctx := context.Background()
if err := run(ctx); err != nil {
log.Fatal(err)
}
}
func run(ctx context.Context) error {
builder, err := builder.New(ctx)
if err != nil {
return err
}
defer builder.CleanUp()
return pipelines.
New(builder).
WithRustBin(&pipelines.RustBinOpts{
DockerImageOpt: &pipelines.DockerImageOpt{
ImageName: "rust-bin",
},
BinName: "rust-bin",
}).
Execute(ctx)
}

View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

View File

@ -10,6 +10,7 @@ func Build() *cobra.Command {
} }
cmd.AddCommand( cmd.AddCommand(
BuildRustBin(),
BuildGolangBin(), BuildGolangBin(),
BuildDocker(), BuildDocker(),
) )

52
pkg/cli/build_rust_bin.go Normal file
View File

@ -0,0 +1,52 @@
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 BuildRustBin() *cobra.Command {
var (
binName string
)
cmd := &cobra.Command{
Use: "rustbin",
RunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.ParseFlags(args); err != nil {
return err
}
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).
WithRustBin(&pipelines.RustBinOpts{
DockerImageOpt: &pipelines.DockerImageOpt{
ImageName: repoName,
},
BinName: binName,
}).
Execute(ctx)
},
}
cmd.PersistentFlags().StringVar(&binName, "bin-name", "", "bin-name is the binary to build, and what will be present in the output folder")
return cmd
}

View File

@ -19,6 +19,9 @@ var docker embed.FS
//go:embed templates/default/* //go:embed templates/default/*
var defaultFs embed.FS var defaultFs embed.FS
//go:embed templates/rustbin_default/*
var rustbinDefault embed.FS
func NewInitCmd() *cobra.Command { func NewInitCmd() *cobra.Command {
var ( var (
template string template string
@ -48,6 +51,11 @@ func NewInitCmd() *cobra.Command {
return err return err
} }
break break
case "rustbin_default":
if err := initializeTemplate(&rustbinDefault, "rustbin_default", name); err != nil {
return err
}
break
default: default:
return errors.New("could not find matching templates, please run [bust template ls] instead") return errors.New("could not find matching templates, please run [bust template ls] instead")
} }

View File

@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var templates = []string{"docker", "gobin_default", "default"} var templates = []string{"docker", "gobin_default", "default", "rustbin_default"}
func NewLsCmd() *cobra.Command { func NewLsCmd() *cobra.Command {
return &cobra.Command{ return &cobra.Command{

View File

@ -0,0 +1,5 @@
kind: template
load: bust_rustbin_default_template.yaml
name: [[.Name]]
data:
bin_name: default

View File

@ -9,10 +9,10 @@ import (
"time" "time"
"dagger.io/dagger" "dagger.io/dagger"
"git.front.kjuulh.io/kjuulh/byg"
"git.front.kjuulh.io/kjuulh/bust/pkg/tasks/container" "git.front.kjuulh.io/kjuulh/bust/pkg/tasks/container"
"git.front.kjuulh.io/kjuulh/bust/pkg/tasks/golang" "git.front.kjuulh.io/kjuulh/bust/pkg/tasks/golang"
golangbin "git.front.kjuulh.io/kjuulh/bust/pkg/tasks/golang-bin" golangbin "git.front.kjuulh.io/kjuulh/bust/pkg/tasks/golang-bin"
"git.front.kjuulh.io/kjuulh/byg"
) )
type DockerImageOpt struct { type DockerImageOpt struct {

View File

@ -3,8 +3,8 @@ package pipelines
import ( import (
"context" "context"
"git.front.kjuulh.io/kjuulh/byg"
"git.front.kjuulh.io/kjuulh/bust/pkg/builder" "git.front.kjuulh.io/kjuulh/bust/pkg/builder"
"git.front.kjuulh.io/kjuulh/byg"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )

105
pkg/pipelines/rust-bin.go Normal file
View File

@ -0,0 +1,105 @@
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
c := container.LoadImage(client, "harbor.server.kjuulh.io/docker-proxy/library/rust")
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 == "" {
opts.BaseImage = "harbor.server.kjuulh.io/docker-proxy/library/alpine"
}
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)
}
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,43 @@
package rustbin
import (
"context"
"fmt"
"log"
"dagger.io/dagger"
)
func Build(ctx context.Context, container *dagger.Container, binName string) (dagger.FileID, error) {
log.Printf("building binary: (binName=%s)", binName)
c := container.Exec(dagger.ContainerExecOpts{
Args: []string{
"rustup",
"target",
"add",
"x86_64-unknown-linux-musl",
},
})
if _, err := c.ExitCode(ctx); err != nil {
return "", err
}
c = c.Exec(dagger.ContainerExecOpts{
Args: []string{
"cargo",
"build",
"--release",
"--target=x86_64-unknown-linux-musl",
},
})
if _, err := c.ExitCode(ctx); err != nil {
return "", err
}
bin, err := c.File(fmt.Sprintf("target/release/%s", binName)).ID(ctx)
if err != nil {
return "", err
}
return bin, nil
}

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 rustbin --bin-name {{ .input.binName }}
services:
- name: docker
image: docker:dind
privileged: true
volumes:
- name: dockersock
path: /var/run
volumes:
- name: dockersock
temp: {}
image_pull_secrets:
- dockerconfig

View File

@ -11,3 +11,4 @@ add_template kjuulh bust_default_template.yaml
add_template kjuulh bust_docker_template.yaml add_template kjuulh bust_docker_template.yaml
add_template kjuulh bust_gobin_template.yaml add_template kjuulh bust_gobin_template.yaml
add_template kjuulh bust_gobin_default_template.yaml add_template kjuulh bust_gobin_default_template.yaml
add_template kjuulh bust_rustbin_default_template.yaml