Skip to content

Commit 1d36238

Browse files
authored
feat: add self-validating workflow gate jobs (#269)
Add gate jobs that fail if any workflow job fails OR if any job is missing from the gate's needs array. Prevents both job failures and configuration drift when adding new workflow jobs. Callout: I don't think it's possible to have one gate for both workflows, but it should not be the case that we add more over time. ### Testing: See: aws-observability/aws-otel-python-instrumentation#477 By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent d464802 commit 1d36238

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

.github/workflows/codeql.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,38 @@ jobs:
9090
uses: github/codeql-action/analyze@16df4fbc19aea13d921737861d6c622bf3cefe23 #v2.23.0
9191
with:
9292
category: "/language:${{matrix.language}}"
93+
94+
all-codeql-checks-pass:
95+
runs-on: ubuntu-latest
96+
needs: [analyze]
97+
if: always()
98+
steps:
99+
- name: Checkout to get workflow file
100+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 #5.0.0
101+
102+
- name: Check all jobs succeeded and none missing
103+
run: |
104+
# Check if all needed jobs succeeded
105+
results='${{ toJSON(needs) }}'
106+
if echo "$results" | jq -r '.[] | .result' | grep -v success; then
107+
echo "Some jobs failed"
108+
exit 1
109+
fi
110+
111+
# Extract all job names from workflow (excluding this gate job)
112+
all_jobs=$(yq eval '.jobs | keys | .[]' .github/workflows/codeql.yml | grep -v "all-codeql-checks-pass" | sort)
113+
114+
# Extract job names from needs array
115+
needed_jobs='${{ toJSON(needs) }}'
116+
needs_list=$(echo "$needed_jobs" | jq -r 'keys[]' | sort)
117+
118+
# Check if any jobs are missing from needs
119+
missing_jobs=$(comm -23 <(echo "$all_jobs") <(echo "$needs_list"))
120+
if [ -n "$missing_jobs" ]; then
121+
echo "ERROR: Jobs missing from needs array in all-codeql-checks-pass:"
122+
echo "$missing_jobs"
123+
echo "Please add these jobs to the needs array of all-codeql-checks-pass"
124+
exit 1
125+
fi
126+
127+
echo "All CodeQL checks passed and no jobs missing from gate!"

.github/workflows/pr-build.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,38 @@ jobs:
143143
npm run lint
144144
npm run lint:markdown
145145
npm run lint:readme
146+
147+
all-pr-checks-pass:
148+
runs-on: ubuntu-latest
149+
needs: [static-code-checks, contract-test, lint, build]
150+
if: always()
151+
steps:
152+
- name: Checkout to get workflow file
153+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 #5.0.0
154+
155+
- name: Check all jobs succeeded and none missing
156+
run: |
157+
# Check if all needed jobs succeeded
158+
results='${{ toJSON(needs) }}'
159+
if echo "$results" | jq -r '.[] | .result' | grep -v success; then
160+
echo "Some jobs failed"
161+
exit 1
162+
fi
163+
164+
# Extract all job names from workflow (excluding this gate job)
165+
all_jobs=$(yq eval '.jobs | keys | .[]' .github/workflows/pr-build.yml | grep -v "all-pr-checks-pass" | sort)
166+
167+
# Extract job names from needs array
168+
needed_jobs='${{ toJSON(needs) }}'
169+
needs_list=$(echo "$needed_jobs" | jq -r 'keys[]' | sort)
170+
171+
# Check if any jobs are missing from needs
172+
missing_jobs=$(comm -23 <(echo "$all_jobs") <(echo "$needs_list"))
173+
if [ -n "$missing_jobs" ]; then
174+
echo "ERROR: Jobs missing from needs array in all-pr-checks-pass:"
175+
echo "$missing_jobs"
176+
echo "Please add these jobs to the needs array of all-pr-checks-pass"
177+
exit 1
178+
fi
179+
180+
echo "All checks passed and no jobs missing from gate!"

0 commit comments

Comments
 (0)