diff --git a/.github/workflows/compat.yml b/.github/workflows/compat.yml index 657fc945..4b468a76 100644 --- a/.github/workflows/compat.yml +++ b/.github/workflows/compat.yml @@ -94,15 +94,15 @@ jobs: # run boots the release's base image, so a change to it reaches nothing this checks. - name: Does the change reach the machine id: machine - run: spin-machine-dev changed --base "$BASE" --pins qemu-builder,alpine,qemu,debian,debian-snapshot,qboot,kernel,moby-check-config,sparse machine cmd/spin-machine qemu kernel cmd/spin-machine-dev internal/build internal/lab/compat.go internal/lab/tree.go internal/lab/sources.go boot/harness_test.go boot/record_test.go boot/patches_test.go boot/events_test.go boot/verbs_test.go boot/cpuplug_test.go boot/lockdown_test.go boot/testdata/lockdown-check.sh boot/Taskfile.yml go.mod .github/workflows/compat.yml + run: spin-machine-dev changed --base "$BASE" --part qemu --part kernel machine cmd/spin-machine cmd/spin-machine-dev internal/build internal/lab/compat.go internal/lab/tree.go internal/lab/sources.go boot/harness_test.go boot/record_test.go boot/patches_test.go boot/events_test.go boot/verbs_test.go boot/cpuplug_test.go boot/lockdown_test.go boot/testdata/lockdown-check.sh boot/Taskfile.yml go.mod .github/workflows/compat.yml - name: Does the change reach QEMU id: qemu - run: spin-machine-dev changed --base "$BASE" --pins qemu-builder,alpine,qemu,debian,debian-snapshot,qboot qemu go.mod cmd/spin-machine-dev internal/build + run: spin-machine-dev changed --base "$BASE" --part qemu go.mod cmd/spin-machine-dev internal/build - name: Does the change reach the kernel id: kernel - run: spin-machine-dev changed --base "$BASE" --pins debian,debian-snapshot,kernel,moby-check-config,sparse kernel go.mod cmd/spin-machine-dev internal/build + run: spin-machine-dev changed --base "$BASE" --part kernel go.mod cmd/spin-machine-dev internal/build resume: needs: changed diff --git a/.github/workflows/parts.yml b/.github/workflows/parts.yml index 47e4a2af..e8a22f2e 100644 --- a/.github/workflows/parts.yml +++ b/.github/workflows/parts.yml @@ -70,15 +70,15 @@ jobs: - name: Does the change reach QEMU id: qemu - run: spin-machine-dev changed --base "$BASE" --pins qemu-builder,alpine,qemu,debian,debian-snapshot,qboot qemu go.mod cmd/spin-machine-dev internal/build .github/workflows/parts.yml + run: spin-machine-dev changed --base "$BASE" --part qemu go.mod cmd/spin-machine-dev internal/build .github/workflows/parts.yml - name: Does the change reach the kernel id: kernel - run: spin-machine-dev changed --base "$BASE" --pins debian,debian-snapshot,kernel,moby-check-config,sparse kernel go.mod cmd/spin-machine-dev internal/build .github/workflows/parts.yml + run: spin-machine-dev changed --base "$BASE" --part kernel go.mod cmd/spin-machine-dev internal/build .github/workflows/parts.yml - name: Does the change reach e2fsprogs or the image id: image - run: spin-machine-dev changed --base "$BASE" --pins e2fsprogs-builder,e2fsprogs,ubuntu,systemd-source,openssh-source,mkosi,task,git-lfs,buildkit,docker-ce,docker-ce-cli,docker-ce-rootless-extras,containerd-io,docker-buildx-plugin,docker-compose-plugin image e2fsprogs cmd/spin-machine-pressure internal/pressure go.mod go.sum cmd/spin-machine-dev internal/build .github/workflows/parts.yml + run: spin-machine-dev changed --base "$BASE" --part image cmd/spin-machine-dev internal/build .github/workflows/parts.yml qemu: needs: changed diff --git a/.gitignore b/.gitignore index 190966bd..dce8b9ae 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,8 @@ coverage.html # Agent worktrees, made inside the repository by Claude Code. .claude/worktrees/ + +# What a plain `go build ./cmd/...` leaves at the root; spin-machine-dev build puts them in _output/bin. +/spin-machine-dev +/spin-machine +/spin-machine-pressure diff --git a/cmd/spin-machine-dev/build.go b/cmd/spin-machine-dev/build.go index b958eac7..1d2c846a 100644 --- a/cmd/spin-machine-dev/build.go +++ b/cmd/spin-machine-dev/build.go @@ -275,34 +275,51 @@ fetch qemu pulls. main's parts.yml runs it, with a docker login that can write p func changedCmd() *cobra.Command { var base string - var pins []string + var parts, pins []string c := &cobra.Command{ - Use: "changed --base --pins ...", + Use: "changed --base (--part | --pins )... [path...]", Short: "Say whether the commits since a base can change what a part builds", Hidden: true, - Long: `Say whether the commits since --base can change what is built from the pins and paths named: -true when anything under the paths changed or versions.yaml hands those pins other build arguments -than it did at the base. A workflow's path filter cannot say which lines of versions.yaml moved; this -can, so a bump of e2fsprogs does not rebuild QEMU and the kernel. + Long: `Say whether the commits since --base can change what the parts named build: true when +anything under their paths, the parts they are made with, or the paths named changed, or when +versions.yaml hands their pins other build arguments than it did at the base. The pins and paths +are internal/build's, so a workflow names the part and only what it adds, and a part's pin added to +internal/build reaches every workflow at once. --pins names entries for a check that is not a part +(codeql.yml's patched trees). A workflow's path filter cannot say which lines of versions.yaml +moved; this can, so a bump of e2fsprogs does not rebuild QEMU and the kernel. Prints build=true or build=false, and writes it to the step's outputs. It errs towards building: no base, a base this clone lacks, or a versions.yaml there it cannot read is true.`, - Args: cobra.MinimumNArgs(1), - RunE: func(c *cobra.Command, paths []string) error { + RunE: func(c *cobra.Command, extra []string) error { w, err := open() if err != nil { return err } - if len(pins) == 0 { - return couldNot(fmt.Errorf("--pins names none: say which versions.yaml entries the part is built from")) + if len(parts) == 0 && len(pins) == 0 { + return couldNot(fmt.Errorf("--part and --pins name none: say which parts (%s) or versions.yaml entries the change is to reach", strings.Join(build.Names(), ", "))) } - touched := build.Touches(c.Context(), w.root, base, w.pins, pins, paths) + var paths []string + if len(parts) > 0 { + plan, err := build.Plan(parts) + if err != nil { + return couldNot(err) + } + for _, p := range plan { + pins = append(pins, p.Pins...) + paths = append(paths, p.Paths...) + } + } + if len(paths)+len(extra) == 0 { + return couldNot(fmt.Errorf("no paths: --pins needs the paths its check is made from")) + } + touched := build.Touches(c.Context(), w.root, base, w.pins, pins, append(paths, extra...)) _, _ = fmt.Fprintf(c.OutOrStdout(), "build=%t\n", touched) return output("build", fmt.Sprint(touched)) }, } c.Flags().StringVar(&base, "base", "", "the commit the change is to; none builds") - c.Flags().StringSliceVar(&pins, "pins", nil, "the versions.yaml entries the part's Dockerfile is handed") + c.Flags().StringSliceVar(&parts, "part", nil, "the parts whose pins and paths decide it, with the parts they are made with") + c.Flags().StringSliceVar(&pins, "pins", nil, "versions.yaml entries that decide it, for a check that is not a part") return c } diff --git a/cmd/spin-machine-dev/verbs_test.go b/cmd/spin-machine-dev/verbs_test.go index 2cf53f82..e2efb5f1 100644 --- a/cmd/spin-machine-dev/verbs_test.go +++ b/cmd/spin-machine-dev/verbs_test.go @@ -442,17 +442,60 @@ func TestVerifySaysWhatIsMissingAndAsksQEMUForTheArguments(t *testing.T) { } func TestChangedSaysWhetherAPartBuilds(t *testing.T) { - checkout(t) - stdout, _, status := verb(t, "changed", "--pins", "e2fsprogs", "e2fsprogs") + dir := checkout(t) + // A part's own path and one the workflow adds. + stdout, _, status := verb(t, "changed", "--part", "e2fsprogs", "go.mod") if status != 0 || stdout != "build=true\n" || read(t, os.Getenv("GITHUB_OUTPUT")) != "build=true\n" { t.Errorf("no base: exit %d, %q, %q", status, stdout, read(t, os.Getenv("GITHUB_OUTPUT"))) } - if _, stderr, status := verb(t, "changed", "e2fsprogs"); status != 2 || !strings.Contains(stderr, "--pins") { - t.Errorf("no pins: exit %d, %q", status, stderr) + if _, stderr, status := verb(t, "changed", "e2fsprogs"); status != 2 || !strings.Contains(stderr, "--part") { + t.Errorf("no part: exit %d, %q", status, stderr) + } + if _, stderr, status := verb(t, "changed", "--part", "nope"); status != 2 || !strings.Contains(stderr, "nope") { + t.Errorf("a part that is not one: exit %d, %q", status, stderr) } if _, _, status := verb(t, "changed", "--pins", "e2fsprogs"); status == 0 { - t.Error("no paths was taken") + t.Error("--pins with no paths was taken") + } + + // A part's pins and paths are internal/build's, with those of the parts it is made with: a + // bump of a package only the image installs, and a change to the e2fsprogs the image is made + // with, each reach the image and not the kernel. + gitRepo(t, dir) + base := strings.TrimSpace(gitOut(t, dir, "rev-parse", "HEAD")) + changes := map[string]func(){ + "docker-ce bumped": func() { + raw := read(t, filepath.Join(dir, "versions.yaml")) + before, entry, _ := strings.Cut(raw, "- name: docker-ce\n") + pin, rest, _ := strings.Cut(entry, " pin: ") + entry = pin + " pin: " + strings.Repeat("0", 64) + rest[64:] + put(t, filepath.Join(dir, "versions.yaml"), before+"- name: docker-ce\n"+entry, 0o644) + }, + "e2fsprogs changed": func() { put(t, filepath.Join(dir, "e2fsprogs", "Dockerfile"), "x\n", 0o644) }, + } + for name, change := range changes { + gitOut(t, dir, "reset", "-q", "--hard", base) + change() + gitOut(t, dir, "add", "-A") + gitOut(t, dir, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "-m", name) + for part, want := range map[string]string{"image": "build=true\n", "kernel": "build=false\n"} { + if stdout, stderr, status := verb(t, "changed", "--base", base, "--part", part); status != 0 || stdout != want { + t.Errorf("%s, --part %s: exit %d, %q, %q", name, part, status, stdout, stderr) + } + } + } +} + +// gitOut runs git in dir and is its output. +func gitOut(t *testing.T, dir string, args ...string) string { + t.Helper() + cmd := exec.Command("git", args...) + cmd.Dir = dir + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("git %v: %v: %s", args, err, out) } + return string(out) } // gitRepo makes dir a repository with one commit, tagged with tags. diff --git a/docs/cli.md b/docs/cli.md index c56dff04..5713893d 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -116,14 +116,14 @@ PATH. Under `go run`, a step would exit 1 whatever the verb's status. taskgate ci.yml task lint ; task test ; task coverage ; task mutate ; task bin ; spin-machine-dev build spin-machine ; spin-machine-dev fetch qemu ; spin-machine-dev vsock ; spin-machine-dev verify args -parts.yml changed: spin-machine-dev changed --base $BASE --pins ... , once a part +parts.yml changed: spin-machine-dev changed --base $BASE --part , once a part qemu: spin-machine-dev build qemu --cache-registry ; vsock ; verify args ; publish qemu (main) kernel: spin-machine-dev build kernel --cache-registry ; release traceable-kernel --cache-registry image: spin-machine-dev build e2fsprogs ; spin-machine-dev build image parts: spin-machine-dev needs -compat.yml changed: spin-machine-dev changed --base $BASE --pins ... , for the machine, +compat.yml changed: spin-machine-dev changed --base $BASE --part , for the machine, QEMU and the kernel resume: (when the change reaches the machine) spin-machine-dev vsock ; spin-machine-dev build qemu, or fetch qemu when the change does not reach it ;