| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 1 | //go:build ignore |
| 2 | // +build ignore |
| 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| David Crawshaw | 4bdd9aa | 2025-05-03 21:33:09 +0000 | [diff] [blame] | 11 | "runtime" |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 12 | |
| 13 | "sketch.dev/dockerimg" |
| 14 | ) |
| 15 | |
| 16 | func main() { |
| David Crawshaw | 4bdd9aa | 2025-05-03 21:33:09 +0000 | [diff] [blame] | 17 | 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 Zeyliger | 33d282f | 2025-05-03 04:01:54 +0000 | [diff] [blame] | 28 | # 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 Snyder | b6bc113 | 2025-05-29 13:24:52 -0700 | [diff] [blame] | 30 | # Choose classic personal access token, add scopes write:packages. |
| David Crawshaw | 4bdd9aa | 2025-05-03 21:33:09 +0000 | [diff] [blame] | 31 | # 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 | |
| 34 | This script will build and push multi-architecture Docker images to ghcr.io. |
| 35 | Ensure you have followed the setup instructions above and are logged in to Docker and GitHub. |
| 36 | |
| 37 | Press Enter to continue or Ctrl+C to abort...`) |
| 38 | fmt.Scanln() |
| 39 | |
| 40 | // Create a temporary directory for building |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 41 | dir, err := os.MkdirTemp("", "sketch-pushdockerimg-*") |
| 42 | if err != nil { |
| 43 | panic(err) |
| 44 | } |
| 45 | defer os.RemoveAll(dir) |
| 46 | |
| David Crawshaw | 4bdd9aa | 2025-05-03 21:33:09 +0000 | [diff] [blame] | 47 | // Get default image information |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 48 | name, dockerfile, tag := dockerimg.DefaultImage() |
| David Crawshaw | 4bdd9aa | 2025-05-03 21:33:09 +0000 | [diff] [blame] | 49 | |
| 50 | // Write the Dockerfile to the temporary directory |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 51 | if err := os.WriteFile(filepath.Join(dir, "Dockerfile"), []byte(dockerfile), 0o666); err != nil { |
| 52 | panic(err) |
| 53 | } |
| 54 | |
| David Crawshaw | 4bdd9aa | 2025-05-03 21:33:09 +0000 | [diff] [blame] | 55 | // Helper function to run commands |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 56 | 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 Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 67 | path := name + ":" + tag |
| 68 | |
| David Crawshaw | 4bdd9aa | 2025-05-03 21:33:09 +0000 | [diff] [blame] | 69 | // 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.zeyliger | 2ca1f10 | 2025-07-02 22:17:00 +0000 | [diff] [blame] | 79 | "-t", name+":latest", |
| David Crawshaw | 4bdd9aa | 2025-05-03 21:33:09 +0000 | [diff] [blame] | 80 | "--push", |
| 81 | ".", |
| David Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 82 | ) |
| David Crawshaw | 4bdd9aa | 2025-05-03 21:33:09 +0000 | [diff] [blame] | 83 | |
| 84 | // Inspect the built image to verify it contains both architectures |
| David Crawshaw | 2a5bd6d | 2025-04-30 14:29:46 -0700 | [diff] [blame] | 85 | run("docker", "buildx", "imagetools", "inspect", path) |
| David Crawshaw | 4bdd9aa | 2025-05-03 21:33:09 +0000 | [diff] [blame] | 86 | |
| 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 Crawshaw | 1112949 | 2025-04-25 20:41:53 -0700 | [diff] [blame] | 91 | } |