blob: 41d1b65a993e97d41a8f7f7b866b5c2c64a4c3ab [file] [log] [blame]
David Crawshaw11129492025-04-25 20:41:53 -07001//go:build ignore
2// +build ignore
3
4package main
5
6import (
7 "fmt"
8 "os"
9 "os/exec"
10 "path/filepath"
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000011 "runtime"
David Crawshaw11129492025-04-25 20:41:53 -070012
13 "sketch.dev/dockerimg"
14)
15
16func main() {
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000017 if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
18 fmt.Fprintf(os.Stderr, "pushdockerimg.go: requires ubuntu linux/amd64\n")
19 os.Exit(2)
20 }
21 // Display setup instructions for vanilla Ubuntu
22 fmt.Print(`Push a sketch docker image to the public GitHub container registry.
23
24 # One-off setup instructions:
25 sudo apt-get update
26 sudo apt-get install docker.io docker-buildx qemu-user-static
27 # Login to Docker with GitHub credentials
28 # You can get #GH_ACCESS_TOK from github.com or from 'gh auth token'.
29 # Make sure the token is configured to write containers for the boldsoftware org.
30 echo $GH_ACCESS_TOK | docker login ghcr.io -u $GH_USER --password-stdin
31
32This script will build and push multi-architecture Docker images to ghcr.io.
33Ensure you have followed the setup instructions above and are logged in to Docker and GitHub.
34
35Press Enter to continue or Ctrl+C to abort...`)
36 fmt.Scanln()
37
38 // Create a temporary directory for building
David Crawshaw11129492025-04-25 20:41:53 -070039 dir, err := os.MkdirTemp("", "sketch-pushdockerimg-*")
40 if err != nil {
41 panic(err)
42 }
43 defer os.RemoveAll(dir)
44
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000045 // Get default image information
David Crawshaw2a5bd6d2025-04-30 14:29:46 -070046 name, dockerfile, tag := dockerimg.DefaultImage()
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000047
48 // Write the Dockerfile to the temporary directory
David Crawshaw11129492025-04-25 20:41:53 -070049 if err := os.WriteFile(filepath.Join(dir, "Dockerfile"), []byte(dockerfile), 0o666); err != nil {
50 panic(err)
51 }
52
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000053 // Helper function to run commands
David Crawshaw11129492025-04-25 20:41:53 -070054 run := func(args ...string) {
55 cmd := exec.Command(args[0], args[1:]...)
56 cmd.Dir = dir
57 cmd.Stdout = os.Stdout
58 cmd.Stderr = os.Stderr
59 fmt.Printf("running %v\n", cmd.Args)
60 if err := cmd.Run(); err != nil {
61 panic(err)
62 }
63 }
64
David Crawshaw2a5bd6d2025-04-30 14:29:46 -070065 path := name + ":" + tag
66
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000067 // Set up BuildX for multi-arch builds
68 run("docker", "buildx", "create", "--name", "multiarch-builder", "--use")
69
70 // Make sure the builder is using the proper driver for multi-arch builds
71 run("docker", "buildx", "inspect", "--bootstrap")
72
73 // Build and push the multi-arch image in a single command
74 run("docker", "buildx", "build",
75 "--platform", "linux/amd64,linux/arm64",
76 "-t", path,
77 "--push",
78 ".",
David Crawshaw11129492025-04-25 20:41:53 -070079 )
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000080
81 // Inspect the built image to verify it contains both architectures
David Crawshaw2a5bd6d2025-04-30 14:29:46 -070082 run("docker", "buildx", "imagetools", "inspect", path)
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000083
84 // Clean up the builder
85 run("docker", "buildx", "rm", "multiarch-builder")
86
87 fmt.Printf("\n✅ Successfully built and pushed multi-arch image: %s\n", path)
David Crawshaw11129492025-04-25 20:41:53 -070088}