// Command image builds and publishes the multi-arch images of an agent // (checker or status page server) from cross-compiled binaries, without // Docker. // // image -name checker -dist dist -repo registry/owner/uptimesebi-checker \ // -version v1.2.3 -commit abc123 -ref refs/tags/v1.2.3 [-autoupdate] [-push] // // dist holds one directory per platform: linux_amd64, linux_arm64, // linux_arm_v7 and linux_arm_v6, each with the binaries. Without -push // the index is only built. The registry login comes from REGISTRY_USER and // REGISTRY_PASSWORD; when RELEASE_SIGNING_KEY is set, pushed images are // signed so self-updating agents accept them. package main import ( "context" "errors" "flag" "fmt" "os" "strings" "time" "github.com/google/go-containerregistry/pkg/name" "git.zb-server.de/ZB-Server/uptimesebi/internal/agentimage" "git.zb-server.de/ZB-Server/uptimesebi/internal/signing" ) 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 agentimage.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.Version, "version", "dev", "release version, as the binaries report it") fs.StringVar(&o.Commit, "commit", "", "git revision") fs.StringVar(&o.Source, "source", "", "repository URL for the image labels") fs.BoolVar(&o.Launcher, "autoupdate", false, "build the self-updating variant (tags get -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)") repo := fs.String("repo", "", "image repository, e.g. git.example.com/owner/uptimesebi-checker") 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 == "" || *repo == "" { return errors.New("image: -name and -repo are required") } o.Created = time.Unix(*created, 0).UTC() idx, err := agentimage.Build(o) if err != nil { return err } digest, err := idx.Digest() if err != nil { return err } fmt.Printf("built %s@%s\n", *repo, digest) if !*push { return nil } suffix := "" if o.Launcher { suffix = "autoupdate" } tags := agentimage.ChannelTags(*ref, o.Version, suffix) if len(tags) == 0 { return fmt.Errorf("image: %q is not a ref that publishes images", *ref) } var nameOpts []name.Option if *insecure { nameOpts = append(nameOpts, name.Insecure) } ctx := context.Background() login := agentimage.Credentials{User: os.Getenv("REGISTRY_USER"), Password: os.Getenv("REGISTRY_PASSWORD")} if err := agentimage.Publish(ctx, idx, *repo, tags, login, nameOpts...); err != nil { return err } fmt.Printf("pushed %s:%s\n", *repo, strings.Join(tags, ", :")) raw := os.Getenv("RELEASE_SIGNING_KEY") if raw == "" { fmt.Println("RELEASE_SIGNING_KEY is not set; self-updating agents will not accept this image.") return nil } key, err := signing.ParsePrivateKey(raw) if err != nil { return err } if err := agentimage.Sign(ctx, idx, *repo, o.Name, o.Version, key, login, nameOpts...); err != nil { return err } fmt.Printf("signed %s@%s as %s\n", *repo, digest, o.Version) return nil }