|
| 1 | +name: CI Status Gate |
| 2 | +description: Evaluate required CI job results and fail on unexpected skips or failed jobs. |
| 3 | + |
| 4 | +inputs: |
| 5 | + needs-json: |
| 6 | + description: JSON representation of the calling job's needs context. |
| 7 | + required: true |
| 8 | + requirement-context-json: |
| 9 | + description: JSON representation of get-requirements outputs. |
| 10 | + required: true |
| 11 | + e2e-job-regex: |
| 12 | + description: Regex matching E2E build/test jobs whose skipped result is allowed. Failed or cancelled E2E jobs still fail. |
| 13 | + required: false |
| 14 | + default: '^e2e-' |
| 15 | + event-name: |
| 16 | + description: GitHub event name for the current workflow run. |
| 17 | + required: true |
| 18 | + is-fork: |
| 19 | + description: Whether the current pull request originates from a fork. When true, skipped jobs are treated as allowed skips. |
| 20 | + required: false |
| 21 | + default: 'false' |
| 22 | + |
| 23 | +runs: |
| 24 | + using: composite |
| 25 | + steps: |
| 26 | + - name: Evaluate CI status |
| 27 | + shell: bash |
| 28 | + env: |
| 29 | + NEEDS_JSON: ${{ inputs.needs-json }} |
| 30 | + REQUIREMENT_CONTEXT_JSON: ${{ inputs.requirement-context-json }} |
| 31 | + E2E_JOB_REGEX: ${{ inputs.e2e-job-regex }} |
| 32 | + EVENT_NAME: ${{ inputs.event-name }} |
| 33 | + IS_FORK: ${{ inputs.is-fork }} |
| 34 | + run: | |
| 35 | + set -euo pipefail |
| 36 | +
|
| 37 | + get_requirement() { |
| 38 | + local key="$1" |
| 39 | + jq -nr --arg key "$key" 'env.REQUIREMENT_CONTEXT_JSON | fromjson | .[$key] // "false"' |
| 40 | + } |
| 41 | +
|
| 42 | + sanitize_markdown_cell() { |
| 43 | + local value="$1" |
| 44 | + value="${value//$'\n'/ }" |
| 45 | + value="${value//|/\\|}" |
| 46 | + printf '%s' "$value" |
| 47 | + } |
| 48 | +
|
| 49 | + add_summary_row() { |
| 50 | + local job_name result decision reason |
| 51 | + job_name="$(sanitize_markdown_cell "$1")" |
| 52 | + result="$(sanitize_markdown_cell "$2")" |
| 53 | + decision="$(sanitize_markdown_cell "$3")" |
| 54 | + reason="$(sanitize_markdown_cell "$4")" |
| 55 | +
|
| 56 | + printf '| `%s` | `%s` | %s | %s |\n' \ |
| 57 | + "$job_name" "$result" "$decision" "$reason" >> "$summary_file" |
| 58 | + } |
| 59 | +
|
| 60 | + mark_failure() { |
| 61 | + local message="$1" |
| 62 | + failed="true" |
| 63 | + echo "::error::$message" |
| 64 | + } |
| 65 | +
|
| 66 | + validate_json_type() { |
| 67 | + local variable_name="$1" |
| 68 | + local expected_type="$2" |
| 69 | +
|
| 70 | + if ! jq -en --arg variable_name "$variable_name" --arg expected_type "$expected_type" \ |
| 71 | + '(env[$variable_name] | fromjson | type) == $expected_type' >/dev/null 2>&1; then |
| 72 | + echo "::error::$variable_name is not a valid JSON $expected_type" |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | + } |
| 76 | +
|
| 77 | + require_requirement_key() { |
| 78 | + local key="$1" |
| 79 | +
|
| 80 | + if ! jq -en --arg key "$key" \ |
| 81 | + 'env.REQUIREMENT_CONTEXT_JSON | fromjson | .[$key] != null' >/dev/null 2>&1; then |
| 82 | + echo "::error::REQUIREMENT_CONTEXT_JSON is missing or null for required key: $key" |
| 83 | + exit 1 |
| 84 | + fi |
| 85 | + } |
| 86 | +
|
| 87 | + validate_json_type NEEDS_JSON object |
| 88 | + validate_json_type REQUIREMENT_CONTEXT_JSON object |
| 89 | +
|
| 90 | + for required_key in skip_everything block_merge_for_e2e_readiness; do |
| 91 | + require_requirement_key "$required_key" |
| 92 | + done |
| 93 | +
|
| 94 | + skip_everything="$(get_requirement skip_everything)" |
| 95 | + block_merge_for_e2e_readiness="$(get_requirement block_merge_for_e2e_readiness)" |
| 96 | +
|
| 97 | + if [[ "$block_merge_for_e2e_readiness" == "true" ]]; then |
| 98 | + echo "::error::The 'pr-not-ready-for-e2e' label is still applied. Remove it to trigger E2E tests before merging." |
| 99 | + exit 1 |
| 100 | + fi |
| 101 | +
|
| 102 | + if [[ "$skip_everything" == "true" ]]; then |
| 103 | + echo "skip_everything=true; treating all jobs as passed" |
| 104 | + exit 0 |
| 105 | + fi |
| 106 | +
|
| 107 | + failed="false" |
| 108 | + summary_file="$(mktemp)" |
| 109 | + trap 'if [[ -n "${GITHUB_STEP_SUMMARY:-}" && -f "$summary_file" ]]; then cat "$summary_file" >> "$GITHUB_STEP_SUMMARY"; fi; rm -f "$summary_file"' EXIT |
| 110 | + job_count=0 |
| 111 | +
|
| 112 | + { |
| 113 | + echo "### CI Status Gate" |
| 114 | + echo |
| 115 | + echo "| Job | Result | Decision | Reason |" |
| 116 | + echo "| --- | --- | --- | --- |" |
| 117 | + } >> "$summary_file" |
| 118 | +
|
| 119 | + while IFS=$'\t' read -r job_name result; do |
| 120 | + job_count=$((job_count + 1)) |
| 121 | +
|
| 122 | + case "$result" in |
| 123 | + success) |
| 124 | + add_summary_row "$job_name" "$result" "pass" "job succeeded" |
| 125 | + ;; |
| 126 | + failure|cancelled) |
| 127 | + mark_failure "$job_name finished with result: $result" |
| 128 | + add_summary_row "$job_name" "$result" "fail" "job did not complete successfully" |
| 129 | + ;; |
| 130 | + skipped) |
| 131 | + if [[ "$job_name" =~ $E2E_JOB_REGEX ]]; then |
| 132 | + add_summary_row "$job_name" "$result" "pass" "skipped E2E jobs are allowed" |
| 133 | + elif [[ "$EVENT_NAME" == "merge_group" ]]; then |
| 134 | + add_summary_row "$job_name" "$result" "pass" "merge queue skip is allowed" |
| 135 | + elif [[ "$IS_FORK" == "true" ]]; then |
| 136 | + add_summary_row "$job_name" "$result" "pass" "fork-only skip is allowed" |
| 137 | + else |
| 138 | + mark_failure "$job_name was skipped unexpectedly" |
| 139 | + add_summary_row "$job_name" "$result" "fail" "skip was not expected" |
| 140 | + fi |
| 141 | + ;; |
| 142 | + *) |
| 143 | + mark_failure "$job_name has unknown result: $result" |
| 144 | + add_summary_row "$job_name" "$result" "fail" "job result is unknown" |
| 145 | + ;; |
| 146 | + esac |
| 147 | + done < <(jq -nr 'env.NEEDS_JSON | fromjson | to_entries[] | [.key, (.value.result // "")] | @tsv') |
| 148 | +
|
| 149 | + if [[ "$job_count" -eq 0 ]]; then |
| 150 | + echo "::error::NEEDS_JSON does not contain any jobs" |
| 151 | + exit 1 |
| 152 | + fi |
| 153 | +
|
| 154 | + if [[ "$failed" == "true" ]]; then |
| 155 | + exit 1 |
| 156 | + fi |
| 157 | +
|
| 158 | + echo "All required jobs passed" |
0 commit comments