Skip to content

Commit ab3f8ad

Browse files
authored
feat: add self-validating workflow gate jobs (#263)
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 47bde2b commit ab3f8ad

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

.github/workflows/codeql.yml

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

.github/workflows/pr-build.yml

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,40 @@ jobs:
233233
cd test
234234
bash ./build-and-install-distro.sh
235235
bash ./set-up-contract-tests.sh
236-
pytest contract-tests/tests
236+
pytest contract-tests/tests
237+
238+
239+
all-pr-checks-pass:
240+
runs-on: ubuntu-latest
241+
needs: [static-code-checks, build, build-arm, build-x64-musl, build-arm-musl, build-and-scan-images, contract-test]
242+
if: always()
243+
steps:
244+
- name: Checkout to get workflow file
245+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 #5.0.0
246+
247+
- name: Check all jobs succeeded and none missing
248+
run: |
249+
# Check if all needed jobs succeeded
250+
results='${{ toJSON(needs) }}'
251+
if echo "$results" | jq -r '.[] | .result' | grep -v success; then
252+
echo "Some jobs failed"
253+
exit 1
254+
fi
255+
256+
# Extract all job names from workflow (excluding this gate job)
257+
all_jobs=$(yq eval '.jobs | keys | .[]' .github/workflows/pr-build.yml | grep -v "all-pr-checks-pass" | sort)
258+
259+
# Extract job names from needs array
260+
needed_jobs='${{ toJSON(needs) }}'
261+
needs_list=$(echo "$needed_jobs" | jq -r 'keys[]' | sort)
262+
263+
# Check if any jobs are missing from needs
264+
missing_jobs=$(comm -23 <(echo "$all_jobs") <(echo "$needs_list"))
265+
if [ -n "$missing_jobs" ]; then
266+
echo "ERROR: Jobs missing from needs array in all-pr-checks-pass:"
267+
echo "$missing_jobs"
268+
echo "Please add these jobs to the needs array of all-pr-checks-pass"
269+
exit 1
270+
fi
271+
272+
echo "All checks passed and no jobs missing from gate!"

0 commit comments

Comments
 (0)