Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/toolkit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,9 @@ toolkit credentials references --json # effective op:// locators; no secret read
`toolkit ci maintenance --json` reports recent explicit maintenance runs and
the durable Temporal coordinator's memo key. It distinguishes release notes, CI
image refreshes, and superseded source requests from normal main recovery.
Each run's `source` is the commit it built: the request's declared
`CI_MAINTENANCE_SOURCE`, which the extension checks out in place of the main tip
Woodpecker records as `woodpeckerCommit`.
Maintenance success is never merge-readiness evidence. Inspect a failed or
ambiguous submission and its coordinator state before retrying it.

Expand Down
17 changes: 16 additions & 1 deletion packages/toolkit/src/lib/ci/maintenance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ export function maintenanceKind(pipeline: WoodpeckerPipeline) {
return name === "maintenance-superseded" ? "superseded" : null;
}

/**
* The commit a maintenance run built. Woodpecker records main's tip; the config
* extension checks out the request's declared verified source in its place,
* unless it superseded the request, which then runs no work at the tip.
*/
export function maintenanceSource(
pipeline: WoodpeckerPipeline,
kind: NonNullable<ReturnType<typeof maintenanceKind>>,
) {
return kind === "superseded"
? pipeline.commit
: (pipeline.variables?.["CI_MAINTENANCE_SOURCE"] ?? pipeline.commit);
}

export async function ciMaintenance(config: WoodpeckerConfig) {
const listed = await listPipelines(config, {
event: "manual",
Expand All @@ -43,7 +57,8 @@ export async function ciMaintenance(config: WoodpeckerConfig) {
{
kind,
pipeline: pipeline.number,
source: pipeline.commit,
source: maintenanceSource(pipeline, kind),
woodpeckerCommit: pipeline.commit,
status: pipeline.status,
started: pipeline.started ?? null,
finished: pipeline.finished ?? null,
Expand Down
2 changes: 2 additions & 0 deletions packages/toolkit/src/lib/woodpecker/ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export const WoodpeckerSummarySchema = z.object({
finished: z.number().optional(),
});
export const WoodpeckerPipelineSchema = WoodpeckerSummarySchema.extend({
/** Manual-pipeline variables; Woodpecker omits an empty set. */
variables: z.record(z.string(), z.string()).optional(),
workflows: z.array(WorkflowSchema).default([]),
errors: z.array(z.unknown()).nullish(),
});
Expand Down
21 changes: 20 additions & 1 deletion packages/toolkit/test/lib/ci/maintenance.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "vitest";
import { maintenanceKind } from "#lib/ci/maintenance.ts";
import { maintenanceKind, maintenanceSource } from "#lib/ci/maintenance.ts";
import { ciFixture } from "./fixtures.ts";

test.each([
Expand All @@ -19,6 +19,25 @@ test.each([
pipeline.branch = "feature";
expect(maintenanceKind(pipeline)).toBeNull();
});
test("reports the declared source a pinned run built, not the tip Woodpecker recorded (#21090)", () => {
const pipeline = ciFixture().pipeline;
if (pipeline === null) throw new Error("Missing fixture");
const tip = "e8471e9e22be34c8d2ee8ff2166005330f09cd86";
const verified = "133b0528fd1a25ca4e5c4c9bd698f15f69eb79d8";
pipeline.commit = tip;
pipeline.variables = {
CI_MAINTENANCE_KIND: "ci-images",
CI_MAINTENANCE_SOURCE: verified,
};
expect(maintenanceSource(pipeline, "ci-images")).toBe(verified);
// A superseded request ran no work at the tip.
expect(maintenanceSource(pipeline, "superseded")).toBe(tip);
// An operator's manual run without a declared source builds the tip.
pipeline.variables = { CI_MAINTENANCE_KIND: "ci-images" };
expect(maintenanceSource(pipeline, "ci-images")).toBe(tip);
delete pipeline.variables;
expect(maintenanceSource(pipeline, "ci-images")).toBe(tip);
});
test("normal manual recovery stays a verification pipeline", () => {
const pipeline = ciFixture().pipeline;
if (pipeline === null) throw new Error("Missing fixture");
Expand Down
6 changes: 6 additions & 0 deletions packages/woodpecker-config-extension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,12 @@ before its pipelines finish. Woodpecker still starts the manual pipeline at the
tip. When the source is still that verified commit, the extension checks it out
through the source cache in place of the tip. Otherwise, or without the source
cache, it emits a no-work receipt and the dispatcher selects again.
Woodpecker's pipeline record and its `CI_COMMIT_SHA` still name the tip, so
every emitted command container sets `CI_COMMIT_SHA` to the commit its workflow
checked out, and pins, image builds and diagnostics report the built source.
The source-cache clone keeps Woodpecker's value and fails unless the commit it
was told to check out is that `CI_COMMIT_SHA` or the request's declared
`CI_MAINTENANCE_SOURCE` or `CI_EXTENDED_SOURCE`.
Maintenance runs on its own one-slot `maintenance` agent at main's admission
priority, sharing one concurrency group across all kinds. Its pods queue for
compute quota like any other workflow and take no slot from the main or PR
Expand Down
31 changes: 31 additions & 0 deletions packages/woodpecker-config-extension/src/checkout/declared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Variables in which a signed manual request names the verified main commit it
* was selected for. Woodpecker starts a manual pipeline at main's tip; while
* that commit is still the verified one, the extension checks it out in the
* tip's place (`requestedSourcePipeline`).
*/
export const MAINTENANCE_SOURCE_VARIABLE = "CI_MAINTENANCE_SOURCE";
export const EXTENDED_SOURCE_VARIABLE = "CI_EXTENDED_SOURCE";

/**
* The emitted checkout commit must be one Woodpecker or the signed request
* named: `CI_COMMIT_SHA` as Woodpecker set it for the clone container, or a
* declared request source. Anything else would build one commit while the
* pipeline reports another, so the clone fails instead.
*/
export function assertDeclaredCheckout(
commit: string,
environment: Readonly<Record<string, string | undefined>>,
): void {
const started = environment["CI_COMMIT_SHA"];
if (started === undefined || started === "")
throw new Error("Checkout requires Woodpecker's CI_COMMIT_SHA");
if (commit === started) return;
const declared = [MAINTENANCE_SOURCE_VARIABLE, EXTENDED_SOURCE_VARIABLE].find(
(name) => environment[name] === commit,
);
if (declared === undefined)
throw new Error(
`Checkout commit ${commit} is neither CI_COMMIT_SHA ${started} nor a declared request source`,
);
}
2 changes: 2 additions & 0 deletions packages/woodpecker-config-extension/src/checkout/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { z } from "zod";
import path from "node:path";
import { assertDeclaredCheckout } from "./declared.ts";
import { checkoutSource } from "./snapshot.ts";

const [cache, commit] = z
.tuple([z.string().min(1), z.string()])
.parse(Bun.argv.slice(2));
assertDeclaredCheckout(commit, Bun.env);
const result = await checkoutSource({
cache:
Bun.env["CI_SOURCE_CACHE_COLD"] === "true"
Expand Down
10 changes: 10 additions & 0 deletions packages/woodpecker-config-extension/src/pipeline/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ function seedVolumes(cache: SourceCache) {

/** Identity of the pipeline being generated, stamped onto every step pod. */
export type PipelineIdentity = {
/**
* The commit every workflow checks out. A pinned maintenance or extended
* request checks out its declared source instead of the tip Woodpecker
* started, so command containers receive this as `CI_COMMIT_SHA`.
*/
readonly commit: string;
readonly branch: string;
/** Forge URL for the commit this pipeline is building. */
Expand Down Expand Up @@ -295,6 +300,11 @@ function emitCommand(
environment: {
...step.environment,
...sharedCacheEnvironment(step.volumes),
// Woodpecker sets `CI_COMMIT_SHA` to the commit it started, which for a
// pinned manual request is main's tip rather than the checkout. Step
// environment overrides it, so every build, pin and diagnostic reports
// the commit the workflow actually checked out.
CI_COMMIT_SHA: identity.commit,
},
...(step.volumes === undefined || step.volumes.length === 0
? {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { GITHUB_DOWNLOAD, HANDOFF_KEYS } from "#src/pipeline/lanes/tofu.ts";
import { stormE2eSteps } from "#src/pipeline/lanes/storm-e2e.ts";
import { requestedSourcePipeline } from "#src/pipeline/maintenance.ts";
import { EXTENDED_SOURCE_VARIABLE } from "#src/checkout/declared.ts";
import { TURBO_VERIFY_TIER } from "#src/pipeline/tiers.ts";
import {
TURBO_CACHE_TOKEN,
Expand All @@ -41,8 +42,6 @@ import {

/** Dispatcher-chosen identity of one request; echoed into the step's environment. */
export const EXTENDED_REQUEST_VARIABLE = "CI_EXTENDED_REQUEST";
/** The verified main commit the dispatcher selected; see `requestedSourcePipeline`. */
export const EXTENDED_SOURCE_VARIABLE = "CI_EXTENDED_SOURCE";
export const EXTENDED_STEP_KEY = "verify-extended";
export const EXTENDED_SUPERSEDED_KEY = "extended-superseded";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { GITHUB_DOWNLOAD } from "#src/pipeline/lanes/tofu.ts";
import { MEDIUM_TIER } from "#src/pipeline/tiers.ts";
import { shellQuote } from "#src/pipeline/emit.ts";
import { MAINTENANCE_SOURCE_VARIABLE } from "#src/checkout/declared.ts";

export const MaintenanceKindSchema = z.enum([
"release-notes",
Expand Down Expand Up @@ -68,7 +69,7 @@ export function requestedMaintenanceSteps(
kind: MaintenanceKind,
pipeline: Pipeline,
) {
const expectedSource = pipeline.variables["CI_MAINTENANCE_SOURCE"];
const expectedSource = pipeline.variables[MAINTENANCE_SOURCE_VARIABLE];
return expectedSource === undefined || expectedSource === pipeline.commit
? maintenanceSteps(images, kind)
: [
Expand Down Expand Up @@ -106,7 +107,7 @@ export function maintenanceSourcePipeline(
): Pipeline {
return requestedSourcePipeline(
pipeline,
"CI_MAINTENANCE_SOURCE",
MAINTENANCE_SOURCE_VARIABLE,
verifiedMain,
);
}
Expand All @@ -120,7 +121,7 @@ export async function eligibleMaintenanceSteps(
) {
// A request whose source was not adopted is superseded, never failed: the
// coordinator selects the newly verified source on its next tick.
const expectedSource = pipeline.variables["CI_MAINTENANCE_SOURCE"];
const expectedSource = pipeline.variables[MAINTENANCE_SOURCE_VARIABLE];
if (expectedSource !== undefined && expectedSource !== pipeline.commit)
return requestedMaintenanceSteps(images, kind, pipeline);
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { describe, expect, test, vi } from "vitest";
import { z } from "zod";
import { parse } from "yaml";
import { createApp } from "#src/app.ts";
import { assertDeclaredCheckout } from "#src/checkout/declared.ts";
import {
DISABLED_PERFORMANCE,
type PerformanceOptions,
Expand Down Expand Up @@ -322,6 +323,48 @@ describe("signed maintenance routing", () => {
expect(body.configs[0]?.data).not.toContain(fixture.pipeline.commit);
expect(changedBase).toHaveBeenCalledOnce();
});
test("a pinned image refresh reports the commit it checks out, which only the declared source admits (#21090)", async () => {
const { app, changedBase } = harness(false, gate, true, {
maintenance: true,
});
changedBase.mockResolvedValue(verified);
const response = await app.request(
await request({
...behindTip,
variables: { ...behindTip.variables, CI_MAINTENANCE_KIND: "ci-images" },
}),
);
const body = ConfigResponseSchema.parse(await response.json());
const Container = z.object({
name: z.string(),
commands: z.array(z.string()),
environment: z.record(z.string(), z.string()).optional(),
});
const workflow = z
.object({ clone: z.array(Container), steps: z.array(Container) })
.parse(parse(body.configs[0]?.data ?? ""));
const clone = workflow.clone.find(
(container) => container.name === "clone",
);
const checkout = clone?.commands.at(-1)?.split(" ").at(-1) ?? "";
expect(checkout).toBe(verified);
// The clone keeps Woodpecker's CI_COMMIT_SHA (the tip it started), so its
// check admits the pinned commit only through the declared source.
for (const container of workflow.clone)
expect(container.environment?.["CI_COMMIT_SHA"]).toBeUndefined();
const started = { CI_COMMIT_SHA: fixture.pipeline.commit };
expect(() =>
assertDeclaredCheckout(checkout, {
...started,
CI_MAINTENANCE_SOURCE: verified,
}),
).not.toThrow();
expect(() => assertDeclaredCheckout(checkout, started)).toThrow("neither");
// Every command reports the checked-out commit, not the tip.
expect(
workflow.steps.map((step) => step.environment?.["CI_COMMIT_SHA"]),
).toEqual(workflow.steps.map(() => verified));
});
test("without the source cache a verified source behind the tip is superseded", async () => {
const { app, changedBase } = harness(false, gate, false, {
maintenance: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { expect, test } from "vitest";
import { assertDeclaredCheckout } from "#src/checkout/declared.ts";

// Pipeline #21090: Woodpecker started at main's tip and the coordinator
// declared the newest verified main commit, which the clone checked out.
const TIP = "e8471e9e22be34c8d2ee8ff2166005330f09cd86";
const VERIFIED = "133b0528fd1a25ca4e5c4c9bd698f15f69eb79d8";
const OTHER = "f".repeat(40);

test("a clone may check out the commit Woodpecker started", () => {
expect(() =>
assertDeclaredCheckout(TIP, { CI_COMMIT_SHA: TIP }),
).not.toThrow();
});

test.each(["CI_MAINTENANCE_SOURCE", "CI_EXTENDED_SOURCE"])(
"a clone may check out the source a signed request declared in %s",
(variable) => {
expect(() =>
assertDeclaredCheckout(VERIFIED, {
CI_COMMIT_SHA: TIP,
[variable]: VERIFIED,
}),
).not.toThrow();
},
);

test("an undeclared commit fails the clone instead of building it under the tip's name", () => {
expect(() =>
assertDeclaredCheckout(VERIFIED, { CI_COMMIT_SHA: TIP }),
).toThrow(`neither CI_COMMIT_SHA ${TIP} nor a declared request source`);
expect(() =>
assertDeclaredCheckout(OTHER, {
CI_COMMIT_SHA: TIP,
CI_MAINTENANCE_SOURCE: VERIFIED,
CI_EXTENDED_SOURCE: VERIFIED,
}),
).toThrow("neither");
// Another variable naming the commit is not a declaration.
expect(() =>
assertDeclaredCheckout(VERIFIED, {
CI_COMMIT_SHA: TIP,
CI_PREV_COMMIT_SHA: VERIFIED,
}),
).toThrow("neither");
});

test("a clone without Woodpecker's commit identity fails", () => {
for (const environment of [{}, { CI_COMMIT_SHA: "" }])
expect(() => assertDeclaredCheckout(TIP, environment)).toThrow(
"requires Woodpecker's CI_COMMIT_SHA",
);
});
33 changes: 33 additions & 0 deletions packages/woodpecker-config-extension/test/emit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,39 @@ test("records the largest emitted clone pod as the workflow checkout tier", () =
}
});

test("every command reports the emitted checkout commit and no clone overrides Woodpecker's", () => {
const image = `docker.io/shepherdjerred/woodpecker-config-extension@sha256:${"a".repeat(64)}`;
const steps = testPipelineSteps();
const cached = cacheSourceSteps(
steps.filter((step) => step.skipClone !== true && step.backend !== "local"),
{ enabled: true, credentialless: false, trustDomain: "trusted", image },
);
const Container = z.object({
name: z.string(),
environment: z.record(z.string(), z.string()).optional(),
});
const Workflow = z.object({
steps: z.array(Container),
clone: z.array(Container).optional(),
});
for (const step of [...steps, ...cached]) {
const workflow = Workflow.parse(
parse(substitute(emitWorkflow(step, TEST_IDENTITY))),
);
for (const container of workflow.steps)
expect(
container.environment?.["CI_COMMIT_SHA"],
`${step.key} ${container.name}`,
).toBe(TEST_IDENTITY.commit);
// The clone's declared-source check compares against Woodpecker's value.
for (const container of workflow.clone ?? [])
expect(
container.environment?.["CI_COMMIT_SHA"],
`${step.key} ${container.name}`,
).toBeUndefined();
}
});

describe("pod shape", () => {
/**
* Services are separate pods. Without their own requests Kueue and the
Expand Down