Repository navigation
fix(ci): wait for required test status metadata #401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| category: fixed | ||
| type: patch | ||
| --- | ||
|
|
||
| Allow bounded retries for GitHub Actions metadata lag before mirroring required PHP matrix statuses, using only verified results from the current attempt. Use a full rerun or a fresh dispatch to refresh required statuses. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -146,21 +146,29 @@ jobs: | |
| if (count($versions) !== count(array_unique($versions))) { | ||
| throw new RuntimeException('Duplicate PHP test matrix entry.'); | ||
| } | ||
| $run = githubApi(["repos/$repository/actions/runs/$runId"]); | ||
| if ((string) ($run['id'] ?? '') !== $runId) { | ||
| throw new RuntimeException('Workflow run ID does not match the current run.'); | ||
| } | ||
| if (($run['repository']['full_name'] ?? null) !== $repository) { | ||
| throw new RuntimeException('Workflow repository does not match the current repository.'); | ||
| } | ||
| if (!is_int($run['run_attempt'] ?? null)) { | ||
| throw new RuntimeException('Workflow run attempt is not an integer.'); | ||
| } | ||
| if ((string) $run['run_attempt'] !== $attempt) { | ||
| throw new RuntimeException('Workflow run attempt does not match the current attempt.'); | ||
| } | ||
| if (($run['status'] ?? null) !== 'in_progress') { | ||
| throw new RuntimeException('Workflow run status is not in_progress.'); | ||
| // The API can briefly lag behind the runner that started this job. | ||
| for ($poll = 1; ; ++$poll) { | ||
| $run = githubApi(["repos/$repository/actions/runs/$runId"]); | ||
| if ((string) ($run['id'] ?? '') !== $runId) { | ||
| throw new RuntimeException('Workflow run ID does not match the current run.'); | ||
| } | ||
| if (($run['repository']['full_name'] ?? null) !== $repository) { | ||
| throw new RuntimeException('Workflow repository does not match the current repository.'); | ||
| } | ||
| if (!is_int($run['run_attempt'] ?? null)) { | ||
| throw new RuntimeException('Workflow run attempt is not an integer.'); | ||
| } | ||
| if ((string) $run['run_attempt'] !== $attempt) { | ||
| throw new RuntimeException('Workflow run attempt does not match the current attempt.'); | ||
| } | ||
| $status = $run['status'] ?? null; | ||
| if ($status === 'in_progress') { | ||
| break; | ||
| } | ||
| if ($poll === 8 || !in_array($status, ['queued', 'requested', 'waiting', 'pending'], true)) { | ||
| throw new RuntimeException('Workflow run status is not in_progress.'); | ||
| } | ||
| sleep(3); | ||
| } | ||
|
|
||
| // Validate the full matrix and current attempt before the first write. | ||
|
|
@@ -309,7 +317,7 @@ jobs: | |
| - Dependency health result: `${{ needs.dependency-health.result }}` | ||
|
|
||
| publish_required_statuses: | ||
| if: ${{ always() && inputs.publish-required-statuses && github.actor != 'dependabot[bot]' && needs.publish_pending_statuses.result == 'success' && (needs.tests.result == 'success' || needs.tests.result == 'failure') }} | ||
| if: ${{ !cancelled() && inputs.publish-required-statuses && github.actor != 'dependabot[bot]' && needs.publish_pending_statuses.result == 'success' && (needs.tests.result == 'success' || needs.tests.result == 'failure') }} | ||
| name: Publish Required Test Statuses | ||
| needs: | ||
| - resolve_php | ||
|
|
@@ -327,72 +335,117 @@ jobs: | |
| TARGET_SHA: ${{ github.sha }} | ||
| TARGET_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | ||
| TEST_MATRIX: ${{ needs.resolve_php.outputs.test-matrix }} | ||
| TEST_RESULT: ${{ needs.tests.result }} | ||
| run: | | ||
| export TEST_JOBS_FILE="${RUNNER_TEMP}/required-test-jobs.json" | ||
| gh api --paginate --slurp \ | ||
| "repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/jobs?filter=all&per_page=100" > "$TEST_JOBS_FILE" | ||
| # Wait for the API to reflect completed matrix dependencies. Only the | ||
| # explicit readiness exit code is retried; malformed evidence fails closed. | ||
| for poll in {1..8}; do | ||
| gh api --paginate --slurp \ | ||
| "repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/jobs?filter=all&per_page=100" > "$TEST_JOBS_FILE" | ||
|
|
||
| # PHP evaluates these variables; Bash must leave them literal. | ||
| # shellcheck disable=SC2016 | ||
| php -r ' | ||
| $matrix = json_decode(getenv("TEST_MATRIX"), true, 512, JSON_THROW_ON_ERROR); | ||
| $versions = $matrix["php-version"] ?? null; | ||
| if (!is_array($versions) || $versions === []) { | ||
| throw new RuntimeException("A non-empty PHP test matrix is required."); | ||
| } | ||
| foreach ($versions as $version) { | ||
| if (!is_string($version) || preg_match("/\A[0-9]+\.[0-9]+\z/", $version) !== 1) { | ||
| throw new RuntimeException("Unsupported PHP test matrix entry."); | ||
| # PHP evaluates these variables; Bash must leave them literal. | ||
| # shellcheck disable=SC2016 | ||
| if results=$(php -d display_errors=stderr -r ' | ||
| $matrix = json_decode(getenv("TEST_MATRIX"), true, 512, JSON_THROW_ON_ERROR); | ||
| $versions = $matrix["php-version"] ?? null; | ||
| if (!is_array($versions) || $versions === [] || !array_is_list($versions)) { | ||
| throw new RuntimeException("A non-empty PHP test matrix is required."); | ||
| } | ||
| foreach ($versions as $version) { | ||
| if (!is_string($version) || preg_match("/\A[0-9]+\.[0-9]+\z/", $version) !== 1) { | ||
| throw new RuntimeException("Unsupported PHP test matrix entry."); | ||
| } | ||
| } | ||
| } | ||
| if (count($versions) !== count(array_unique($versions))) { | ||
| throw new RuntimeException("Duplicate PHP test matrix entry."); | ||
| } | ||
| $pages = json_decode(file_get_contents(getenv("TEST_JOBS_FILE")), true, 512, JSON_THROW_ON_ERROR); | ||
| $jobs = []; | ||
| foreach ($pages as $page) { | ||
| if (!is_array($page["jobs"] ?? null)) { | ||
| if (count($versions) !== count(array_unique($versions))) { | ||
| throw new RuntimeException("Duplicate PHP test matrix entry."); | ||
| } | ||
| $pages = json_decode(file_get_contents(getenv("TEST_JOBS_FILE")), true, 512, JSON_THROW_ON_ERROR); | ||
| if (!is_array($pages) || $pages === [] || !array_is_list($pages)) { | ||
| throw new RuntimeException("Malformed workflow jobs response."); | ||
| } | ||
| $jobs = array_merge($jobs, $page["jobs"]); | ||
| } | ||
| $results = []; | ||
| foreach ($versions as $version) { | ||
| $name = "Run Tests (" . $version . ")"; | ||
| $matches = array_values(array_filter($jobs, static function ($job) use ($name) { | ||
| return is_string($job["name"] ?? null) | ||
| && ($job["name"] === $name || str_ends_with($job["name"], " / " . $name)); | ||
| })); | ||
| if ($matches === []) { | ||
| throw new RuntimeException("Missing workflow job for " . $version); | ||
| $jobs = []; | ||
| foreach ($pages as $page) { | ||
| if (!is_array($page["jobs"] ?? null) || !array_is_list($page["jobs"])) { | ||
| throw new RuntimeException("Malformed workflow jobs response."); | ||
| } | ||
| $jobs = array_merge($jobs, $page["jobs"]); | ||
| } | ||
| $attempt = getenv("GITHUB_RUN_ATTEMPT"); | ||
| if (!is_string($attempt) || preg_match("/\A[1-9][0-9]*\z/", $attempt) !== 1) { | ||
| throw new RuntimeException("Invalid current workflow attempt."); | ||
| } | ||
| foreach ($matches as $candidate) { | ||
| if ((string) ($candidate["run_id"] ?? "") !== getenv("GITHUB_RUN_ID") | ||
| || !is_int($candidate["run_attempt"] ?? null) || $candidate["run_attempt"] < 1) { | ||
| throw new RuntimeException("Invalid workflow job attempt for " . $version); | ||
| $expected = getenv("TEST_RESULT"); | ||
| if (!in_array($expected, ["success", "failure"], true)) { | ||
| throw new RuntimeException("Invalid matrix dependency result."); | ||
| } | ||
| $results = []; | ||
| $notReady = []; | ||
| $hasNonSuccess = false; | ||
| foreach ($versions as $version) { | ||
| $name = "Run Tests (" . $version . ")"; | ||
| $matches = array_values(array_filter($jobs, static function ($job) use ($name) { | ||
| return is_string($job["name"] ?? null) | ||
| && ($job["name"] === $name || str_ends_with($job["name"], " / " . $name)); | ||
| })); | ||
| foreach ($matches as $candidate) { | ||
| if ((string) ($candidate["run_id"] ?? "") !== getenv("GITHUB_RUN_ID") | ||
| || !is_int($candidate["run_attempt"] ?? null) || $candidate["run_attempt"] < 1 | ||
| || $candidate["run_attempt"] > (int) $attempt) { | ||
| throw new RuntimeException("Invalid workflow job attempt for " . $version); | ||
| } | ||
| } | ||
| // Absence cannot distinguish an unchanged leg from an | ||
| // unobserved retry. Never use a result from an older attempt. | ||
| $current = array_values(array_filter($matches, static fn ($job) => $job["run_attempt"] === (int) $attempt)); | ||
| if ($current === []) { | ||
| $notReady[] = "Missing current-attempt workflow job for " . $version; | ||
| continue; | ||
| } | ||
| if (count($current) !== 1) { | ||
| throw new RuntimeException("Ambiguous current-attempt workflow job for " . $version); | ||
| } | ||
| $job = $current[0]; | ||
| $status = $job["status"] ?? null; | ||
| $conclusion = $job["conclusion"] ?? null; | ||
| if ($conclusion === null && in_array($status, ["queued", "in_progress", "waiting", "pending", "requested", "completed"], true)) { | ||
| $notReady[] = "Incomplete current-attempt workflow job for " . $version; | ||
| continue; | ||
| } | ||
| if ($status !== "completed" || !is_string($conclusion) || preg_match("/\A[a-z_]+\z/", $conclusion) !== 1) { | ||
| throw new RuntimeException("Invalid current-attempt workflow job result for " . $version); | ||
| } | ||
| $hasNonSuccess = $hasNonSuccess || $conclusion !== "success"; | ||
| $results[] = $version . "\t" . ($conclusion === "success" ? "success" : "failure") | ||
| . "\t" . $conclusion; | ||
| } | ||
| $latestAttempt = max(array_column($matches, "run_attempt")); | ||
| $latest = array_values(array_filter($matches, static fn ($job) => $job["run_attempt"] === $latestAttempt)); | ||
| if (count($latest) !== 1) { | ||
| throw new RuntimeException("Ambiguous latest workflow job for " . $version); | ||
| // Independently confirm that the complete terminal snapshot | ||
| // agrees with the matrix dependency outcome. | ||
| if ($notReady === [] && ($expected === "failure") !== $hasNonSuccess) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not publish an earlier attempt's result for a version when the current attempt has no job for that version. The validator chooses the highest visible For example, the matrix dependency fails and PHP 8.3 fails in attempt 1. On the first read, PHP 8.5 attempt 2 is Before the first write, each matrix version must have one of these:
Choose that evidence in the implementation. The current code provides neither, and the aggregate check cannot detect a masked success.
|
||
| $notReady[] = "Workflow jobs do not reflect the test matrix result yet."; | ||
| } | ||
| $job = $latest[0]; | ||
| if (($job["status"] ?? null) !== "completed" | ||
| || !is_string($job["conclusion"] ?? null) || $job["conclusion"] === "" | ||
| || preg_match("/\A[a-z_]+\z/", $job["conclusion"]) !== 1) { | ||
| throw new RuntimeException("Incomplete latest workflow job for " . $version); | ||
| if ($notReady !== []) { | ||
| fwrite(STDERR, implode(PHP_EOL, $notReady) . PHP_EOL); | ||
| exit(75); | ||
| } | ||
| $results[] = $version . "\t" . ($job["conclusion"] === "success" ? "success" : "failure") | ||
| . "\t" . $job["conclusion"]; | ||
| } | ||
| echo implode(PHP_EOL, $results), PHP_EOL; | ||
| ' | while IFS=$'\t' read -r php_version state conclusion; do | ||
| echo implode(PHP_EOL, $results), PHP_EOL; | ||
| '); then | ||
| break | ||
| else | ||
| status=$? | ||
| if [[ "$status" != 75 || "$poll" == 8 ]]; then | ||
| exit "$status" | ||
| fi | ||
| sleep 3 | ||
| fi | ||
| done | ||
|
|
||
| while IFS=$'\t' read -r php_version state conclusion; do | ||
| gh api \ | ||
| --method POST \ | ||
| "repos/${GITHUB_REPOSITORY}/statuses/${TARGET_SHA}" \ | ||
| -f state="${state}" \ | ||
| -f context="Run Tests (${php_version})" \ | ||
| -f description="PHP ${php_version} matrix job result: ${conclusion}." \ | ||
| -f target_url="${TARGET_URL}" | ||
| done | ||
| done <<< "$results" | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update automatic transient recovery to request a full rerun.
The existing retry action still calls
rerun-failed-jobs. GitHub reruns failed jobs and their dependants, but retains successful sibling jobs. (raw.githubusercontent.com)For an opt-in run where only PHP 8.5 fails transiently, a successful automatic retry has no current-attempt PHP 8.3 or 8.4 job. This filter therefore makes publication exit 75 after eight reads. The previous PHP 8.5 failure status remains despite successful test recovery.
Keep this strict filter. Update the automatic retry caller to request all jobs for mirrored test runs. Otherwise, the documented automatic recovery still requires a manual full rerun.