Skip to content

Commit 7016b9c

Browse files
authored
fix(ci): adopt dispatched release validation runs (#120342)
* fix(ci): adopt dispatched release validation runs * test(ci): update release child validation guard * fix(ci): cancel mismatched release children
1 parent 2253328 commit 7016b9c

3 files changed

Lines changed: 196 additions & 36 deletions

File tree

‎.github/workflows/full-release-validation.yml‎

Lines changed: 75 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,35 @@ jobs:
479479
gh_with_retry api "repos/${GITHUB_REPOSITORY}/actions/runs/${run_id}"
480480
}
481481

482+
validate_child_run() {
483+
local candidate_run_id="$1"
484+
local candidate_run_json
485+
if [[ ! "$candidate_run_id" =~ ^[0-9]+$ ]]; then
486+
echo "::error::Refusing to adopt invalid ${workflow} run ID ${candidate_run_id}." >&2
487+
return 1
488+
fi
489+
candidate_run_json="$(
490+
gh_with_retry api "repos/${GITHUB_REPOSITORY}/actions/runs/${candidate_run_id}"
491+
)"
492+
if ! jq -e \
493+
--argjson id "$candidate_run_id" \
494+
--argjson workflow_id "$expected_workflow_id" \
495+
--arg title "$dispatch_run_name" \
496+
--arg branch "$CHILD_WORKFLOW_REF" \
497+
'(.id == $id)
498+
and (.workflow_id == $workflow_id)
499+
and (.display_title == $title)
500+
and (.head_branch == $branch)
501+
and (.event == "workflow_dispatch")' \
502+
<<< "$candidate_run_json" >/dev/null; then
503+
echo "::error::Refusing to adopt unvalidated ${workflow} run ${candidate_run_id}." >&2
504+
jq '{id, workflow_id, path, display_title, head_branch, head_sha, event, html_url}' \
505+
<<< "$candidate_run_json" >&2
506+
return 1
507+
fi
508+
printf '%s\n' "$candidate_run_json"
509+
}
510+
482511
fetch_child_jobs() {
483512
if [[ "$workflow" == "npm-telegram-beta-e2e.yml" || "$workflow" == "openclaw-performance.yml" ]]; then
484513
gh_with_retry run view "$run_id" --json jobs --jq '.jobs[]'
@@ -491,7 +520,6 @@ jobs:
491520
local field="$1"
492521
if [[ "$workflow" == "npm-telegram-beta-e2e.yml" || "$workflow" == "openclaw-performance.yml" ]]; then
493522
case "$field" in
494-
head_sha) field=headSha ;;
495523
html_url) field=url ;;
496524
esac
497525
gh_with_retry run view "$run_id" --json "$field" --jq ".${field} // \"\""
@@ -544,7 +572,7 @@ jobs:
544572
local workflow="$1"
545573
local dispatch_run_name="$2"
546574
shift 2
547-
local dispatch_output dispatch_status matches_json match_count run_id status conclusion url poll_count run_json jobs_json child_head_sha encoded_workflow_ref current_workflow_sha
575+
local dispatch_output dispatch_status dispatch_run_ids matches_json match_count run_id status conclusion url poll_count run_json jobs_json child_head_sha encoded_workflow_ref current_workflow_sha expected_workflow_id
548576

549577
encoded_workflow_ref="$(jq -rn --arg value "$CHILD_WORKFLOW_REF" '$value | @uri')"
550578
current_workflow_sha="$(
@@ -554,8 +582,12 @@ jobs:
554582
echo "::error::Child workflow ref ${CHILD_WORKFLOW_REF} moved to ${current_workflow_sha}, expected ${PARENT_WORKFLOW_SHA}; refusing dispatch." >&2
555583
return 1
556584
fi
585+
expected_workflow_id="$(
586+
gh_with_retry api "repos/${GITHUB_REPOSITORY}/actions/workflows/${workflow}" --jq .id
587+
)"
557588

558-
# A failed dispatch POST can still create a run. Never retry it; recover only by exact run name.
589+
# The dispatch POST is one-shot: adopt its returned identity when available.
590+
# If absent or ambiguous, exact-name recovery avoids a duplicate child.
559591
set +e
560592
dispatch_output="$(gh workflow run "$workflow" --ref "$CHILD_WORKFLOW_REF" "$@" 2>&1)"
561593
dispatch_status=$?
@@ -567,55 +599,69 @@ jobs:
567599
fi
568600

569601
run_id=""
570-
for _ in $(seq 1 60); do
571-
if matches_json="$(
572-
DISPATCH_RUN_NAME="$dispatch_run_name" CHILD_WORKFLOW_REF="$CHILD_WORKFLOW_REF" \
573-
gh_with_retry api -X GET "repos/${GITHUB_REPOSITORY}/actions/workflows/${workflow}/runs" \
574-
-F event=workflow_dispatch \
575-
-F per_page=100 \
576-
--jq '[.workflow_runs[] | select(.display_title == env.DISPATCH_RUN_NAME and .head_branch == env.CHILD_WORKFLOW_REF) | .id]'
577-
)"; then
578-
match_count="$(jq 'length' <<< "$matches_json")"
579-
if (( match_count > 1 )); then
580-
echo "::error::Multiple runs matched ${dispatch_run_name}; refusing to guess." >&2
581-
exit 1
582-
fi
583-
if (( match_count == 1 )); then
584-
run_id="$(jq -r '.[0]' <<< "$matches_json")"
585-
break
602+
dispatch_run_ids="$(
603+
sed -nE "s#^https://github[.]com/${GITHUB_REPOSITORY}/actions/runs/([0-9]+)\$#\1#p" \
604+
<<< "$dispatch_output" | sort -u
605+
)"
606+
if [[ "$dispatch_run_ids" == *$'\n'* ]]; then
607+
echo "::error::${workflow} dispatch returned multiple run identities; refusing to guess." >&2
608+
exit 1
609+
elif [[ -n "$dispatch_run_ids" ]]; then
610+
run_id="$dispatch_run_ids"
611+
else
612+
for _ in $(seq 1 60); do
613+
if matches_json="$(
614+
DISPATCH_RUN_NAME="$dispatch_run_name" CHILD_WORKFLOW_REF="$CHILD_WORKFLOW_REF" \
615+
gh_with_retry api -X GET "repos/${GITHUB_REPOSITORY}/actions/workflows/${workflow}/runs" \
616+
-F event=workflow_dispatch \
617+
-F per_page=100 \
618+
--jq '[.workflow_runs[] | select(.display_title == env.DISPATCH_RUN_NAME and .head_branch == env.CHILD_WORKFLOW_REF) | .id]'
619+
)"; then
620+
match_count="$(jq 'length' <<< "$matches_json")"
621+
if (( match_count > 1 )); then
622+
echo "::error::Multiple runs matched ${dispatch_run_name}; refusing to guess." >&2
623+
exit 1
624+
fi
625+
if (( match_count == 1 )); then
626+
run_id="$(jq -r '.[0]' <<< "$matches_json")"
627+
break
628+
fi
586629
fi
587-
fi
588-
sleep 5
589-
done
630+
sleep 5
631+
done
632+
fi
590633
if [[ -z "$run_id" ]]; then
591634
echo "::error::Could not find exact dispatched run ${dispatch_run_name}; dispatch status ${dispatch_status}. The dispatch was not retried to avoid creating a duplicate child." >&2
592635
exit 1
593636
fi
594-
if [[ "$dispatch_status" -ne 0 ]]; then
595-
echo "::warning::${workflow} dispatch returned status ${dispatch_status}; adopted exact run ${run_id}." >&2
596-
fi
597-
echo "Dispatched ${workflow}: https://github.com/${GITHUB_REPOSITORY}/actions/runs/${run_id}"
598-
echo "run_id=${run_id}" >> "$GITHUB_OUTPUT"
599637

600638
cancel_child() {
601639
if [[ -n "${active_child_run_id:-}" ]]; then
602640
echo "Cancelling child workflow ${active_child_workflow}: ${active_child_run_id}" >&2
603641
gh run cancel "$active_child_run_id" >/dev/null 2>&1 || true
604642
fi
605643
}
644+
run_json="$(validate_child_run "$run_id")"
645+
# Identity fields prove cancellation ownership before workflow-SHA validation.
606646
# EXIT traps run after function locals unwind; preserve only adopted child identity.
607647
active_child_workflow="$workflow"
608648
active_child_run_id="$run_id"
609649
trap cancel_child EXIT INT TERM
610650

611-
child_head_sha="$(read_child_run_field head_sha)"
651+
child_head_sha="$(jq -r '.head_sha // ""' <<< "$run_json")"
612652
if [[ "$child_head_sha" != "$PARENT_WORKFLOW_SHA" ]]; then
613-
echo "::error::${workflow} child run used workflow SHA ${child_head_sha}, expected parent workflow SHA ${PARENT_WORKFLOW_SHA}."
653+
echo "::error::${workflow} child run used workflow SHA ${child_head_sha}, expected parent workflow SHA ${PARENT_WORKFLOW_SHA}." >&2
614654
cancel_child
615655
trap - EXIT INT TERM
616656
exit 1
617657
fi
618658

659+
if [[ "$dispatch_status" -ne 0 ]]; then
660+
echo "::warning::${workflow} dispatch returned status ${dispatch_status}; adopted exact run ${run_id}." >&2
661+
fi
662+
echo "Dispatched ${workflow}: https://github.com/${GITHUB_REPOSITORY}/actions/runs/${run_id}"
663+
echo "run_id=${run_id}" >> "$GITHUB_OUTPUT"
664+
619665
fail_fast_failed_jobs() {
620666
if [[ "$FAIL_FAST" != "true" ]]; then
621667
return 0

‎test/scripts/package-acceptance-workflow.test.ts‎

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,13 @@ if (args[0] === "workflow" && args[1] === "run") {
272272
console.error(env.MOCK_GH_DISPATCH_ERROR);
273273
process.exit(1);
274274
}
275-
console.log("Created workflow_dispatch event.");
275+
console.log(env.MOCK_GH_DISPATCH_OUTPUT);
276276
} else if (args[0] === "api" && args.some((value) => value.includes("/commits/"))) {
277277
console.log(env.MOCK_GH_CURRENT_SHA);
278278
} else if (args[0] === "api" && args.some((value) => value.includes("/actions/workflows/") && value.endsWith("/runs"))) {
279279
console.log(env.MOCK_GH_MATCHES);
280+
} else if (args[0] === "api" && args.some((value) => value.includes("/actions/workflows/"))) {
281+
console.log(env.MOCK_GH_WORKFLOW_ID);
280282
} else if (args[0] === "api" && args.some((value) => value.includes("/jobs?"))) {
281283
if (env.MOCK_GH_JOBS_ERROR) {
282284
console.error(env.MOCK_GH_JOBS_ERROR);
@@ -290,9 +292,15 @@ if (args[0] === "workflow" && args[1] === "run") {
290292
}
291293
console.log(JSON.stringify({
292294
conclusion,
295+
display_title: env.MOCK_GH_RUN_TITLE,
296+
event: env.MOCK_GH_RUN_EVENT,
297+
head_branch: env.MOCK_GH_RUN_HEAD_BRANCH,
293298
head_sha: env.MOCK_GH_CHILD_SHA,
294299
html_url: url,
300+
id: Number(env.MOCK_GH_RUN_ID),
301+
path: env.MOCK_GH_RUN_PATH,
295302
status: nextStatus(),
303+
workflow_id: Number(env.MOCK_GH_RUN_WORKFLOW_ID),
296304
}));
297305
} else if (args[0] === "run" && args[1] === "view") {
298306
const field = args[args.indexOf("--json") + 1];
@@ -390,10 +398,21 @@ if (args[0] === "workflow" && args[1] === "run") {
390398
MOCK_GH_CHILD_SHA: parentSha,
391399
MOCK_GH_CONCLUSION: "success",
392400
MOCK_GH_CURRENT_SHA: parentSha,
401+
MOCK_GH_DISPATCH_OUTPUT: "Created workflow_dispatch event.",
393402
MOCK_GH_JOBS: JSON.stringify(defaultJobs),
394403
MOCK_GH_MATCHES: "[101]",
404+
MOCK_GH_RUN_EVENT: "workflow_dispatch",
405+
MOCK_GH_RUN_HEAD_BRANCH:
406+
overrides.MOCK_GH_RUN_HEAD_BRANCH ??
407+
overrides.CHILD_WORKFLOW_REF ??
408+
stepEnv.CHILD_WORKFLOW_REF,
409+
MOCK_GH_RUN_ID: "101",
410+
MOCK_GH_RUN_PATH: `.github/workflows/${child.workflow}`,
411+
MOCK_GH_RUN_TITLE: `${child.runName} full-release-validation-77-2${child.nonceSuffix}`,
412+
MOCK_GH_RUN_WORKFLOW_ID: "789",
395413
MOCK_GH_STATUSES: '["completed"]',
396414
MOCK_GH_STATUS_POLLS: statusPath,
415+
MOCK_GH_WORKFLOW_ID: "789",
397416
PATH: `${workdir}:${process.env.PATH}`,
398417
...overrides,
399418
},
@@ -1807,7 +1826,7 @@ describe("package acceptance workflow", () => {
18071826
expect(script.match(/gh workflow run/gu)).toHaveLength(1);
18081827
expect(script).not.toContain("gh_with_retry workflow run");
18091828
expectTextToIncludeAll(script, [
1810-
"A failed dispatch POST can still create a run. Never retry it",
1829+
"The dispatch POST is one-shot",
18111830
'encoded_workflow_ref="$(jq -rn --arg value "$CHILD_WORKFLOW_REF"',
18121831
'gh_with_retry api "repos/${GITHUB_REPOSITORY}/commits/${encoded_workflow_ref}" --jq .sha',
18131832
'"$current_workflow_sha" != "$PARENT_WORKFLOW_SHA"',
@@ -1816,6 +1835,8 @@ describe("package acceptance workflow", () => {
18161835
"dispatch_status=$?",
18171836
'if [[ "$dispatch_status" -ne 0 && ! "$dispatch_output" =~ $GH_TRANSIENT_SERVER_OR_NETWORK_PATTERN ]]',
18181837
"dispatch failed with non-ambiguous status ${dispatch_status}; refusing adoption polling.",
1838+
'sed -nE "s#^https://github[.]com/${GITHUB_REPOSITORY}/actions/runs/([0-9]+)\\$#\\1#p"',
1839+
'validate_child_run "$run_id"',
18191840
'DISPATCH_RUN_NAME="$dispatch_run_name" CHILD_WORKFLOW_REF="$CHILD_WORKFLOW_REF"',
18201841
".display_title == env.DISPATCH_RUN_NAME and .head_branch == env.CHILD_WORKFLOW_REF",
18211842
"Multiple runs matched ${dispatch_run_name}; refusing to guess.",
@@ -1908,6 +1929,76 @@ describe("package acceptance workflow", () => {
19081929
);
19091930
});
19101931

1932+
it.each(FULL_RELEASE_CHILD_DISPATCHES)(
1933+
"adopts and validates the run URL returned for $jobName without listing runs",
1934+
(child) => {
1935+
const { calls, result } = runFullReleaseChildDispatch(child, {
1936+
MOCK_GH_DISPATCH_OUTPUT: "https://github.com/openclaw/openclaw/actions/runs/101",
1937+
});
1938+
1939+
expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(0);
1940+
expect(calls.filter(({ args }) => args[0] === "workflow")).toHaveLength(1);
1941+
expect(
1942+
calls.filter(({ args }) =>
1943+
args.some((value) => value.includes("/actions/workflows/") && value.endsWith("/runs")),
1944+
),
1945+
).toHaveLength(0);
1946+
expect(
1947+
calls.some(({ args }) => args.some((value) => value.endsWith("/actions/runs/101"))),
1948+
).toBe(true);
1949+
expect(calls.filter(({ args }) => args[0] === "run" && args[1] === "cancel")).toHaveLength(0);
1950+
},
1951+
);
1952+
1953+
it("recovers by exact name when a successful dispatch returns no run URL", () => {
1954+
const { calls, result } = runFullReleaseChildDispatch(FULL_RELEASE_CHILD_DISPATCHES[0], {
1955+
MOCK_GH_DISPATCH_OUTPUT: "",
1956+
});
1957+
1958+
expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(0);
1959+
expect(calls.filter(({ args }) => args[0] === "workflow")).toHaveLength(1);
1960+
expect(
1961+
calls.filter(({ args }) =>
1962+
args.some((value) => value.includes("/actions/workflows/") && value.endsWith("/runs")),
1963+
),
1964+
).toHaveLength(1);
1965+
});
1966+
1967+
it.each([
1968+
["workflow", { MOCK_GH_RUN_WORKFLOW_ID: "790" }],
1969+
["title", { MOCK_GH_RUN_TITLE: "Unrelated workflow run" }],
1970+
["head branch", { MOCK_GH_RUN_HEAD_BRANCH: "other" }],
1971+
["event", { MOCK_GH_RUN_EVENT: "push" }],
1972+
] as const)("refuses a returned run URL with the wrong %s", (_label, overrides) => {
1973+
const { calls, result } = runFullReleaseChildDispatch(FULL_RELEASE_CHILD_DISPATCHES[0], {
1974+
MOCK_GH_DISPATCH_OUTPUT: "https://github.com/openclaw/openclaw/actions/runs/101",
1975+
...overrides,
1976+
});
1977+
1978+
expect(result.status).toBe(1);
1979+
expect(result.stderr).toContain("Refusing to adopt unvalidated ci.yml run 101");
1980+
expect(
1981+
calls.some(({ args }) =>
1982+
args.some((value) => value.includes("/actions/workflows/") && value.endsWith("/runs")),
1983+
),
1984+
).toBe(false);
1985+
expect(calls.filter(({ args }) => args[0] === "run" && args[1] === "cancel")).toHaveLength(0);
1986+
});
1987+
1988+
it("refuses a nonnumeric exact-name candidate without cancellation ownership", () => {
1989+
const { calls, result } = runFullReleaseChildDispatch(FULL_RELEASE_CHILD_DISPATCHES[0], {
1990+
MOCK_GH_DISPATCH_OUTPUT: "",
1991+
MOCK_GH_MATCHES: '["not-a-run-id"]',
1992+
});
1993+
1994+
expect(result.status).toBe(1);
1995+
expect(result.stderr).toContain("Refusing to adopt invalid ci.yml run ID not-a-run-id");
1996+
expect(
1997+
calls.some(({ args }) => args.some((value) => value.endsWith("/actions/runs/not-a-run-id"))),
1998+
).toBe(false);
1999+
expect(calls.filter(({ args }) => args[0] === "run" && args[1] === "cancel")).toHaveLength(0);
2000+
});
2001+
19112002
it.each(FULL_RELEASE_CHILD_DISPATCHES)(
19122003
"rejects moved workflow refs before dispatching $jobName",
19132004
(child) => {
@@ -1980,14 +2071,15 @@ describe("package acceptance workflow", () => {
19802071
);
19812072

19822073
it.each(FULL_RELEASE_CHILD_DISPATCHES)(
1983-
"cancels exactly the adopted $jobName child when its workflow SHA differs",
2074+
"cancels exactly the identified $jobName child when its workflow SHA differs",
19842075
(child) => {
19852076
const { calls, result } = runFullReleaseChildDispatch(child, {
19862077
MOCK_GH_CHILD_SHA: "c".repeat(40),
2078+
MOCK_GH_DISPATCH_OUTPUT: "https://github.com/openclaw/openclaw/actions/runs/101",
19872079
});
19882080

19892081
expect(result.status).toBe(1);
1990-
expect(result.stdout).toContain("expected parent workflow SHA");
2082+
expect(result.stderr).toContain("expected parent workflow SHA");
19912083
expect(calls.filter(({ args }) => args[0] === "run" && args[1] === "cancel")).toEqual([
19922084
expect.objectContaining({ args: ["run", "cancel", "101"] }),
19932085
]);

‎test/scripts/release-no-push-workflow.test.ts‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ describe("release validation no-push transport", () => {
309309
expect(releaseHelper.with?.["persist-credentials"]).toBe(false);
310310
});
311311

312-
it("rejects every child whose workflow SHA differs from the parent workflow SHA", () => {
312+
it("owns identified children before rejecting a mismatched workflow SHA", () => {
313313
const full = readWorkflow(FULL_RELEASE);
314314
for (const [jobName, stepName] of [
315315
["normal_ci", "Dispatch and monitor CI"],
@@ -319,9 +319,31 @@ describe("release validation no-push transport", () => {
319319
["performance", "Dispatch and monitor OpenClaw Performance"],
320320
] as const) {
321321
const dispatch = step(job(full, jobName), stepName);
322+
const dispatchRun = dispatch.run ?? "";
322323
expect(dispatch.env?.PARENT_WORKFLOW_SHA, jobName).toBe("${{ github.sha }}");
323-
expect(dispatch.run, jobName).toContain('"$child_head_sha" != "$PARENT_WORKFLOW_SHA"');
324-
expect(dispatch.run, jobName).toContain("expected parent workflow SHA");
324+
expect(dispatchRun, jobName).toContain(
325+
'if [[ "$child_head_sha" != "$PARENT_WORKFLOW_SHA" ]]; then',
326+
);
327+
expect(dispatchRun.match(/\.head_sha == \$head_sha/gu), jobName).toBeNull();
328+
expect(dispatchRun, jobName).toContain('run_json="$(validate_child_run "$run_id")"');
329+
expect(dispatchRun, jobName).toContain('active_child_run_id="$run_id"');
330+
expect(dispatchRun, jobName).toContain("trap cancel_child EXIT INT TERM");
331+
expect(
332+
dispatchRun.indexOf('run_json="$(validate_child_run "$run_id")"'),
333+
jobName,
334+
).toBeLessThan(dispatchRun.indexOf('active_child_run_id="$run_id"'));
335+
expect(dispatchRun.indexOf('active_child_run_id="$run_id"'), jobName).toBeLessThan(
336+
dispatchRun.indexOf("trap cancel_child EXIT INT TERM"),
337+
);
338+
expect(dispatchRun.indexOf("trap cancel_child EXIT INT TERM"), jobName).toBeLessThan(
339+
dispatchRun.indexOf('if [[ "$child_head_sha" != "$PARENT_WORKFLOW_SHA" ]]'),
340+
);
341+
const shaMismatch = dispatchRun.slice(
342+
dispatchRun.indexOf('if [[ "$child_head_sha" != "$PARENT_WORKFLOW_SHA" ]]'),
343+
dispatchRun.indexOf("fail_fast_failed_jobs()"),
344+
);
345+
expect(shaMismatch, jobName).toContain("cancel_child");
346+
expect(shaMismatch, jobName).toContain("trap - EXIT INT TERM");
325347
}
326348

327349
const verify = step(job(full, "summary"), "Verify child workflow results");

0 commit comments

Comments
 (0)