// Command image builds the multi-arch images of an agent (checker or status // page server) from cross-compiled binaries, without Docker: one layer on // an empty base per platform, combined into an OCI index. // // image -name checker -dist dist -repo registry/owner/uptimesebi-checker \ // -version v1.2.3 -commit abc123 -ref refs/tags/v1.2.3 [-push] // // dist holds one directory per platform: linux_amd64, linux_arm64, // linux_arm_v7 and linux_arm_v6, each with the binary. Without -push the // index is only built and checked. The registry login comes from // REGISTRY_USER and REGISTRY_PASSWORD. package main import ( "context" "errors" "flag" "fmt" "os" "strings" "time" ) func main() { if err := run(os.Args[1:]); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } } type listFlag []string func (l *listFlag) String() string { return strings.Join(*l, ",") } func (l *listFlag) Set(v string) error { *l = append(*l, v); return nil } func run(args []string) error { fs := flag.NewFlagSet("image", flag.ContinueOnError) var o options fs.StringVar(&o.Name, "name", "", "binary name: checker or statuspage") fs.StringVar(&o.Dist, "dist", "dist", "directory with one subdirectory per platform") fs.StringVar(&o.Repo, "repo", "", "image repository, e.g. git.example.com/owner/uptimesebi-checker") fs.StringVar(&o.Version, "version", "dev", "release version") fs.StringVar(&o.Commit, "commit", "", "git revision") fs.StringVar(&o.Source, "source", "", "repository URL for the image labels") fs.StringVar(&o.Suffix, "suffix", "", "tag suffix of an image variant, e.g. autoupdate") fs.Var((*listFlag)(&o.Env), "env", "NAME=value baked into the image (repeatable)") fs.Var((*listFlag)(&o.Ports), "port", "exposed port, e.g. 8081/tcp (repeatable)") ref := fs.String("ref", "", "git ref being built, e.g. refs/heads/main or refs/tags/v1.2.3") created := fs.Int64("created", 0, "creation time as Unix seconds, for reproducible images") push := fs.Bool("push", false, "push to the registry") insecure := fs.Bool("insecure", false, "talk plain HTTP to the registry") if err := fs.Parse(args); err != nil { return err } if o.Name == "" || o.Repo == "" { return errors.New("image: -name and -repo are required") } o.Created = time.Unix(*created, 0).UTC() idx, err := build(o) if err != nil { return err } digest, err := idx.Digest() if err != nil { return err } tags := channelTags(*ref, o.Version, o.Commit, o.Created, o.Suffix) fmt.Printf("built %s@%s for %s\n", o.Repo, digest, strings.Join(platformNames(), ", ")) if !*push { return nil } if len(tags) == 0 { return fmt.Errorf("image: %q is not a ref that publishes images", *ref) } if *ref == "refs/heads/main" && (o.Commit == "" || *created == 0) { return errors.New("image: builds of main need -commit and -created for their tag") } login := credentials{os.Getenv("REGISTRY_USER"), os.Getenv("REGISTRY_PASSWORD")} if err := publish(context.Background(), idx, o.Repo, tags, login, *insecure); err != nil { return err } fmt.Printf("pushed %s:%s\n", o.Repo, strings.Join(tags, ", :")) return nil }