-
Notifications
You must be signed in to change notification settings - Fork 0
/
Daggerpipeline.go
126 lines (106 loc) · 3.07 KB
/
Daggerpipeline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
"dagger.io/dagger"
)
func main() {
if err := build(context.Background()); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
// only works if no other image has been created in the meantime
func tagDockerImage() error {
cmd := exec.Command("docker", "load", "-i", "dagger-excuse-deno.tar")
if err := cmd.Run(); err != nil {
return err
}
cmd = exec.Command("sh", "-c", "docker images -q | head -n 1")
imageID, err := cmd.Output()
if err != nil {
return err
}
cmd = exec.Command("docker", "tag", strings.TrimSpace(string(imageID)), "dagger-excuse-deno:latest")
return cmd.Run()
}
func build(ctx context.Context) error {
// Initialize the Dagger client
client, err := dagger.Connect(ctx)
if err != nil {
return err
}
defer client.Close()
// Use Ubuntu as the base for compilation
ubuntu := client.Container().From("ubuntu:22.04")
// Install Deno
deno, err := ubuntu.WithExec([]string{"apt-get", "update"}).
WithExec([]string{"apt-get", "install", "-y", "curl", "unzip"}).
WithExec([]string{"sh", "-c", "curl -fsSL https://deno.land/x/install/install.sh | sh"}).
WithExec([]string{"mv", "/root/.deno/bin/deno", "/usr/local/bin/deno"}).
Sync(ctx)
if err != nil {
return err
}
// Copy the excuse.ts file
srcDir := client.Host().Directory(".")
withSrc := deno.WithDirectory("/app", srcDir)
// Cross-compile for macOS
macBinary, err := withSrc.WithWorkdir("/app").
WithExec([]string{"deno", "compile", "--allow-net=developerexcuses.com", "--target", "x86_64-apple-darwin", "excuse.ts"}).
File("/app/excuse").
Sync(ctx)
if err != nil {
return err
}
// Cross-compile for Windows
winBinary, err := withSrc.WithWorkdir("/app").
WithExec([]string{"deno", "compile", "--allow-net=developerexcuses.com", "--target", "x86_64-pc-windows-msvc", "excuse.ts"}).
File("/app/excuse.exe").
Sync(ctx)
if err != nil {
return err
}
// Compile for Linux
linuxBinary, err := withSrc.WithWorkdir("/app").
WithExec([]string{"deno", "compile", "--allow-net=developerexcuses.com", "excuse.ts"}).
File("/app/excuse").
Sync(ctx)
if err != nil {
return err
}
// Create the final container using distroless
distroless := client.Container().From("gcr.io/distroless/cc-debian12")
final := distroless.WithFile("/app/excuse", linuxBinary).
WithWorkdir("/app").
WithEntrypoint([]string{"/app/excuse"})
// Export the binaries
mac, err := macBinary.Export(ctx, "excuse-mac")
if err != nil {
return err
}
fmt.Printf("Mac export result %s\n", mac)
win, err := winBinary.Export(ctx, "excuse-win.exe")
if err != nil {
return err
}
fmt.Printf("Win export result %s\n", win)
linux, err := linuxBinary.Export(ctx, "excuse-linux")
if err != nil {
return err
}
fmt.Printf("Linux export result %s\n", linux)
// Export the final container as a tar file
_, err = final.Export(ctx, "dagger-excuse-deno.tar")
if err != nil {
return err
}
if err := tagDockerImage(); err != nil {
return err
}
fmt.Println("Container image exported as dagger-excuse-deno.tar")
return nil
}