2023-04-07 19:32:12 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"dagger.io/dagger"
|
2023-04-07 20:19:04 +02:00
|
|
|
platformFormat "github.com/containerd/containerd/platforms"
|
2023-04-07 19:32:12 +02:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
2023-04-07 20:19:04 +02:00
|
|
|
var platforms = []dagger.Platform{
|
|
|
|
"linux/amd64",
|
|
|
|
"linux/arm64",
|
|
|
|
}
|
|
|
|
|
|
|
|
func architectureOf(platform dagger.Platform) string {
|
|
|
|
return platformFormat.MustParse(string(platform)).Architecture
|
|
|
|
}
|
|
|
|
|
2023-04-07 19:32:12 +02:00
|
|
|
func Build(ctx context.Context) error {
|
|
|
|
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
|
2023-04-07 20:19:04 +02:00
|
|
|
containers := make([]*dagger.Container, 0, len(platforms))
|
2023-04-07 19:32:12 +02:00
|
|
|
|
|
|
|
egrp, _ := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
ctx = context.Background()
|
|
|
|
|
2023-04-07 20:33:35 +02:00
|
|
|
shuttleGit := client.
|
2023-05-05 23:41:55 +02:00
|
|
|
Git("git@github.com:lunarway/shuttle.git", dagger.GitOpts{
|
2023-04-07 20:33:35 +02:00
|
|
|
KeepGitDir: true,
|
|
|
|
}).
|
2023-05-05 23:41:55 +02:00
|
|
|
Tag("v0.17.2").
|
2023-04-07 20:33:35 +02:00
|
|
|
Tree(dagger.GitRefTreeOpts{
|
|
|
|
SSHAuthSocket: client.Host().UnixSocket(os.Getenv("SSH_AUTH_SOCK")),
|
|
|
|
})
|
|
|
|
|
2023-04-07 19:32:12 +02:00
|
|
|
for _, platform := range platforms {
|
|
|
|
platform := platform
|
|
|
|
|
|
|
|
egrp.Go(func() error {
|
2023-04-07 20:19:04 +02:00
|
|
|
golang := client.Container().From("golang:1.20.3")
|
2023-04-07 19:32:12 +02:00
|
|
|
|
|
|
|
shuttleImg := golang.
|
|
|
|
WithDirectory("/app", shuttleGit).
|
|
|
|
WithWorkdir("/app").
|
|
|
|
WithEnvVariable("CGO_ENABLED", "0").
|
2023-04-07 20:19:04 +02:00
|
|
|
WithEnvVariable("GOOS", "linux").
|
|
|
|
WithEnvVariable("GOARCH", architectureOf(platform)).
|
2023-04-07 19:32:12 +02:00
|
|
|
WithExec([]string{"go", "build"})
|
|
|
|
|
|
|
|
if _, err := shuttleImg.ExitCode(ctx); err != nil {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
shuttle := shuttleImg.Directory("/app").
|
|
|
|
File("shuttle")
|
|
|
|
|
|
|
|
docker := client.
|
|
|
|
Container(dagger.ContainerOpts{
|
|
|
|
Platform: platform,
|
|
|
|
}).
|
2023-04-07 19:53:34 +02:00
|
|
|
From("docker:cli").
|
2023-04-07 21:00:57 +02:00
|
|
|
File("/usr/local/bin/docker")
|
2023-04-07 19:32:12 +02:00
|
|
|
|
|
|
|
image := client.
|
|
|
|
Container(dagger.ContainerOpts{
|
|
|
|
Platform: platform,
|
|
|
|
}).
|
2023-04-07 20:51:09 +02:00
|
|
|
From("golang:1.20.3").
|
2023-05-05 23:41:55 +02:00
|
|
|
WithExec([]string{
|
|
|
|
"apt", "update",
|
|
|
|
}).
|
|
|
|
WithExec([]string{
|
|
|
|
"apt", "install", "-y", "curl", "build-essential",
|
|
|
|
}).
|
2023-05-06 00:22:04 +02:00
|
|
|
WithEnvVariable("RUSTUP_HOME", "/usr/local/rustup").
|
|
|
|
WithEnvVariable("CARGO_HOME", "/usr/local/cargo").
|
2023-05-05 23:41:55 +02:00
|
|
|
WithExec([]string{
|
|
|
|
"bash", "-c", "curl https://sh.rustup.rs -sSf | bash -s -- -y",
|
2023-05-06 00:22:04 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
path, err := image.EnvVariable(ctx, "PATH")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
image = image.
|
|
|
|
WithEnvVariable(
|
|
|
|
"PATH", fmt.Sprintf("/usr/local/cargo/bin:%s", path),
|
|
|
|
).
|
2023-05-06 00:38:25 +02:00
|
|
|
WithExec([]string{"rustup", "toolchain", "install", "nightly"}).
|
|
|
|
WithExec([]string{"rustup", "default", "nightly"}).
|
2023-04-07 19:32:12 +02:00
|
|
|
WithWorkdir("/app").
|
|
|
|
WithFile("/usr/local/bin/shuttle", shuttle).
|
|
|
|
WithExec([]string{"shuttle", "version"})
|
|
|
|
if _, err := image.ExitCode(ctx); err != nil {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-06 00:22:04 +02:00
|
|
|
if _, err := image.WithExec([]string{"cargo", "--version"}).ExitCode(ctx); err != nil {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-07 21:00:57 +02:00
|
|
|
image = image.WithFile("/usr/bin/docker", docker)
|
2023-04-07 19:32:12 +02:00
|
|
|
if _, err := image.ExitCode(ctx); err != nil {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
containers = append(containers, image)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2023-04-07 20:19:04 +02:00
|
|
|
|
|
|
|
time.Sleep(5 * time.Second)
|
2023-04-07 19:32:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := egrp.Wait(); err != nil {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tag := time.Now().UnixMilli()
|
|
|
|
|
|
|
|
imageTag, err := client.Container().
|
|
|
|
Publish(
|
|
|
|
ctx,
|
|
|
|
fmt.Sprintf("docker.io/kasperhermansen/shuttle-drone:%d", tag),
|
|
|
|
dagger.ContainerPublishOpts{
|
|
|
|
PlatformVariants: containers,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("imageTag: %s\n", imageTag)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|