blob: 3b3b71080ab0cd1540d483e467f6ac946ca43535 [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.
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000030 # Make sure the token is configured to write containers for the boldsoftware org.
31 echo $GH_ACCESS_TOK | docker login ghcr.io -u $GH_USER --password-stdin
32
33This script will build and push multi-architecture Docker images to ghcr.io.
34Ensure you have followed the setup instructions above and are logged in to Docker and GitHub.
35
36Press Enter to continue or Ctrl+C to abort...`)
37 fmt.Scanln()
38
39 // Create a temporary directory for building
David Crawshaw11129492025-04-25 20:41:53 -070040 dir, err := os.MkdirTemp("", "sketch-pushdockerimg-*")
41 if err != nil {
42 panic(err)
43 }
44 defer os.RemoveAll(dir)
45
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000046 // Get default image information
David Crawshaw2a5bd6d2025-04-30 14:29:46 -070047 name, dockerfile, tag := dockerimg.DefaultImage()
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000048
49 // Write the Dockerfile to the temporary directory
David Crawshaw11129492025-04-25 20:41:53 -070050 if err := os.WriteFile(filepath.Join(dir, "Dockerfile"), []byte(dockerfile), 0o666); err != nil {
51 panic(err)
52 }
53
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000054 // Helper function to run commands
David Crawshaw11129492025-04-25 20:41:53 -070055 run := func(args ...string) {
56 cmd := exec.Command(args[0], args[1:]...)
57 cmd.Dir = dir
58 cmd.Stdout = os.Stdout
59 cmd.Stderr = os.Stderr
60 fmt.Printf("running %v\n", cmd.Args)
61 if err := cmd.Run(); err != nil {
62 panic(err)
63 }
64 }
65
David Crawshaw2a5bd6d2025-04-30 14:29:46 -070066 path := name + ":" + tag
67
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000068 // Set up BuildX for multi-arch builds
69 run("docker", "buildx", "create", "--name", "multiarch-builder", "--use")
70
71 // Make sure the builder is using the proper driver for multi-arch builds
72 run("docker", "buildx", "inspect", "--bootstrap")
73
74 // Build and push the multi-arch image in a single command
75 run("docker", "buildx", "build",
76 "--platform", "linux/amd64,linux/arm64",
77 "-t", path,
78 "--push",
79 ".",
David Crawshaw11129492025-04-25 20:41:53 -070080 )
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000081
82 // Inspect the built image to verify it contains both architectures
David Crawshaw2a5bd6d2025-04-30 14:29:46 -070083 run("docker", "buildx", "imagetools", "inspect", path)
David Crawshaw4bdd9aa2025-05-03 21:33:09 +000084
85 // Clean up the builder
86 run("docker", "buildx", "rm", "multiarch-builder")
87
88 fmt.Printf("\n✅ Successfully built and pushed multi-arch image: %s\n", path)
David Crawshaw11129492025-04-25 20:41:53 -070089}