Skip to content

Commit aba7964

Browse files
committed
Kubernetes: Add KubeCheck workflow for validating YAML files and posting results as comments
Signed-off-by: NotHarshhaa <[email protected]>
1 parent 2569164 commit aba7964

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

Diff for: .github/scripts/comment-kubecheck.sh

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
COMMENT="$(cat <<EOF
6+
🧪 **KubeCheck Validation Results**
7+
8+
🕒 Commit Time: \`${COMMIT_TIME}\`
9+
💬 Message: _${COMMIT_MSG}_
10+
11+
${SUMMARY}
12+
13+
---
14+
15+
${RESULTS}
16+
EOF
17+
)"
18+
19+
# Print for debug
20+
echo "$COMMENT"
21+
22+
# Escape for JSON and send
23+
jq -n --arg body "$COMMENT" '{body: $body}' > comment.json
24+
25+
curl -s -X POST "https://api.github.com/repos/${REPO}/commits/${SHA}/comments" \
26+
-H "Authorization: token ${GH_TOKEN}" \
27+
-H "Content-Type: application/json" \
28+
--data-binary @comment.json

Diff for: .github/workflows/kubecheck.yml

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: KubeCheck 🔍
2+
3+
on:
4+
push:
5+
paths:
6+
- '**/*.yaml'
7+
- '**/*.yml'
8+
9+
jobs:
10+
fetch_commit_info:
11+
runs-on: ubuntu-latest
12+
outputs:
13+
sha: ${{ steps.meta.outputs.sha }}
14+
message: ${{ steps.meta.outputs.message }}
15+
timestamp: ${{ steps.meta.outputs.timestamp }}
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v3
19+
20+
- name: Get commit metadata
21+
id: meta
22+
run: |
23+
echo "sha=${GITHUB_SHA}" >> "$GITHUB_OUTPUT"
24+
echo "message=$(git log -1 --pretty=%s)" >> "$GITHUB_OUTPUT"
25+
echo "timestamp=$(git log -1 --format=%cI)" >> "$GITHUB_OUTPUT"
26+
27+
validate_kubeconform:
28+
runs-on: ubuntu-latest
29+
needs: fetch_commit_info
30+
outputs:
31+
summary: ${{ steps.kubeval.outputs.summary }}
32+
results: ${{ steps.kubeval.outputs.results }}
33+
steps:
34+
- name: Checkout code
35+
uses: actions/checkout@v3
36+
37+
- name: Install kubeconform
38+
run: |
39+
curl -L https://github.com/yannh/kubeconform/releases/latest/download/kubeconform-linux-amd64.tar.gz | tar xz
40+
sudo mv kubeconform /usr/local/bin/
41+
42+
- name: Validate Kubernetes YAML
43+
id: kubeval
44+
run: |
45+
set +e
46+
RESULTS=""
47+
PASS_COUNT=0
48+
FAIL_COUNT=0
49+
for file in $(find . -name '*.yaml' -o -name '*.yml'); do
50+
output=$(kubeconform -strict -verbose "$file" 2>&1)
51+
if echo "$output" | grep -q "PASS"; then
52+
emoji="✅"
53+
PASS_COUNT=$((PASS_COUNT + 1))
54+
else
55+
emoji="❌"
56+
FAIL_COUNT=$((FAIL_COUNT + 1))
57+
fi
58+
RESULTS="${RESULTS}${emoji} \`${file}\`\n${output}\n\n"
59+
done
60+
61+
SUMMARY="✅ Passed: ${PASS_COUNT} | ❌ Failed: ${FAIL_COUNT}"
62+
echo "$RESULTS" > validation_output.txt
63+
echo "::set-output name=results::$RESULTS"
64+
echo "::set-output name=summary::$SUMMARY"
65+
66+
- name: Fail if any errors
67+
run: |
68+
if grep -q "❌" validation_output.txt; then
69+
echo "Validation failed."
70+
exit 1
71+
fi
72+
73+
post_comment:
74+
runs-on: ubuntu-latest
75+
needs: [fetch_commit_info, validate_kubeconform]
76+
if: always()
77+
steps:
78+
- name: Comment on commit
79+
env:
80+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
81+
SHA: ${{ needs.fetch_commit_info.outputs.sha }}
82+
COMMIT_MSG: ${{ needs.fetch_commit_info.outputs.message }}
83+
COMMIT_TIME: ${{ needs.fetch_commit_info.outputs.timestamp }}
84+
SUMMARY: ${{ needs.validate_kubeconform.outputs.summary }}
85+
RESULTS: ${{ needs.validate_kubeconform.outputs.results }}
86+
REPO: ${{ github.repository }}
87+
run: chmod +x .github/scripts/comment-kubecheck.sh && bash .github/scripts/comment-kubecheck.sh

0 commit comments

Comments
 (0)