blob: ffd7323843ee7849acbe929ee49f1d959a53d02d [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
Philip Zeyliger33d282f2025-05-03 04:01:54 +000028 # You can get $GH_ACCESS_TOK from github.com or from 'gh auth token'.
29 # On github.com, User icon in top right...Settings...Developer Settings.
Josh Bleecher Snyderb6bc1132025-05-29 13:24:52 -070030 # Choose classic personal access token, add scopes write:packages.
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000031 # Make sure the token is configured to write containers for the boldsoftware org.
32 echo $GH_ACCESS_TOK | docker login ghcr.io -u $GH_USER --password-stdin
33
34This script will build and push multi-architecture Docker images to ghcr.io.
35Ensure you have followed the setup instructions above and are logged in to Docker and GitHub.
36
37Press Enter to continue or Ctrl+C to abort...`)
38 fmt.Scanln()
39
40 // Create a temporary directory for building
David Crawshaw11129492025-04-25 20:41:53 -070041 dir, err := os.MkdirTemp("", "sketch-pushdockerimg-*")
42 if err != nil {
43 panic(err)
44 }
45 defer os.RemoveAll(dir)
46
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000047 // Get default image information
David Crawshaw2a5bd6d2025-04-30 14:29:46 -070048 name, dockerfile, tag := dockerimg.DefaultImage()
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000049
50 // Write the Dockerfile to the temporary directory
David Crawshaw11129492025-04-25 20:41:53 -070051 if err := os.WriteFile(filepath.Join(dir, "Dockerfile"), []byte(dockerfile), 0o666); err != nil {
52 panic(err)
53 }
54
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000055 // Helper function to run commands
David Crawshaw11129492025-04-25 20:41:53 -070056 run := func(args ...string) {
57 cmd := exec.Command(args[0], args[1:]...)
58 cmd.Dir = dir
59 cmd.Stdout = os.Stdout
60 cmd.Stderr = os.Stderr
61 fmt.Printf("running %v\n", cmd.Args)
62 if err := cmd.Run(); err != nil {
63 panic(err)
64 }
65 }
66
David Crawshaw2a5bd6d2025-04-30 14:29:46 -070067 path := name + ":" + tag
68
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000069 // Set up BuildX for multi-arch builds
70 run("docker", "buildx", "create", "--name", "multiarch-builder", "--use")
71
72 // Make sure the builder is using the proper driver for multi-arch builds
73 run("docker", "buildx", "inspect", "--bootstrap")
74
75 // Build and push the multi-arch image in a single command
76 run("docker", "buildx", "build",
77 "--platform", "linux/amd64,linux/arm64",
78 "-t", path,
philip.zeyliger2ca1f102025-07-02 22:17:00 +000079 "-t", name+":latest",
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000080 "--push",
81 ".",
David Crawshaw11129492025-04-25 20:41:53 -070082 )
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000083
84 // Inspect the built image to verify it contains both architectures
David Crawshaw2a5bd6d2025-04-30 14:29:46 -070085 run("docker", "buildx", "imagetools", "inspect", path)
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000086
87 // Clean up the builder
88 run("docker", "buildx", "rm", "multiarch-builder")
89
90 fmt.Printf("\n✅ Successfully built and pushed multi-arch image: %s\n", path)
David Crawshaw11129492025-04-25 20:41:53 -070091}