Skip to content

Commit 8ad0b24

Browse files
authored
feat: add self-validating workflow gate jobs (#1213)
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 my contribution is made under the terms of the Apache 2.0 license.
1 parent a4d69a7 commit 8ad0b24

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,38 @@ jobs:
6060

6161
- name: Perform CodeQL Analysis
6262
uses: github/codeql-action/analyze@16df4fbc19aea13d921737861d6c622bf3cefe23 #v3.30.3
63+
64+
all-codeql-checks-pass:
65+
runs-on: ubuntu-latest
66+
needs: [analyze]
67+
if: always()
68+
steps:
69+
- name: Checkout to get workflow file
70+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 #5.0.0
71+
72+
- name: Check all jobs succeeded and none missing
73+
run: |
74+
# Check if all needed jobs succeeded
75+
results='${{ toJSON(needs) }}'
76+
if echo "$results" | jq -r '.[] | .result' | grep -v success; then
77+
echo "Some jobs failed"
78+
exit 1
79+
fi
80+
81+
# Extract all job names from workflow (excluding this gate job)
82+
all_jobs=$(yq eval '.jobs | keys | .[]' .github/workflows/codeql.yml | grep -v "all-codeql-checks-pass" | sort)
83+
84+
# Extract job names from needs array
85+
needed_jobs='${{ toJSON(needs) }}'
86+
needs_list=$(echo "$needed_jobs" | jq -r 'keys[]' | sort)
87+
88+
# Check if any jobs are missing from needs
89+
missing_jobs=$(comm -23 <(echo "$all_jobs") <(echo "$needs_list"))
90+
if [ -n "$missing_jobs" ]; then
91+
echo "ERROR: Jobs missing from needs array in all-codeql-checks-pass:"
92+
echo "$missing_jobs"
93+
echo "Please add these jobs to the needs array of all-codeql-checks-pass"
94+
exit 1
95+
fi
96+
97+
echo "All CodeQL checks passed and no jobs missing from gate!"

.github/workflows/pr-build.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,37 @@ jobs:
219219
working-directory: lambda-layer
220220
run: ./build-layer.sh
221221

222+
all-pr-checks-pass:
223+
runs-on: ubuntu-latest
224+
needs: [changelog-check, testpatch, build, build-lambda]
225+
if: always()
226+
steps:
227+
- name: Checkout to get workflow file
228+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 #5.0.0
229+
230+
- name: Check all jobs succeeded and none missing
231+
run: |
232+
# Check if all needed jobs succeeded
233+
results='${{ toJSON(needs) }}'
234+
if echo "$results" | jq -r '.[] | .result' | grep -v success; then
235+
echo "Some jobs failed"
236+
exit 1
237+
fi
238+
239+
# Extract all job names from workflow (excluding this gate job)
240+
all_jobs=$(yq eval '.jobs | keys | .[]' .github/workflows/pr-build.yml | grep -v "all-pr-checks-pass" | sort)
241+
242+
# Extract job names from needs array
243+
needed_jobs='${{ toJSON(needs) }}'
244+
needs_list=$(echo "$needed_jobs" | jq -r 'keys[]' | sort)
245+
246+
# Check if any jobs are missing from needs
247+
missing_jobs=$(comm -23 <(echo "$all_jobs") <(echo "$needs_list"))
248+
if [ -n "$missing_jobs" ]; then
249+
echo "ERROR: Jobs missing from needs array in all-pr-checks-pass:"
250+
echo "$missing_jobs"
251+
echo "Please add these jobs to the needs array of all-pr-checks-pass"
252+
exit 1
253+
fi
254+
255+
echo "All checks passed and no jobs missing from gate!"

0 commit comments

Comments
 (0)