Skip to content

Commit 5d1833b

Browse files
committed
Kubernetes: Enhance KubeCheck workflow with improved YAML validation and comment formatting
Signed-off-by: NotHarshhaa <[email protected]>
1 parent aba7964 commit 5d1833b

File tree

2 files changed

+56
-16
lines changed

2 files changed

+56
-16
lines changed

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,25 @@
22

33
set -e
44

5+
# Fallback defaults (optional safety)
6+
COMMIT_TIME="${COMMIT_TIME:-Unknown}"
7+
COMMIT_MSG="${COMMIT_MSG:-(no message)}"
8+
SUMMARY="${SUMMARY:-No summary provided.}"
9+
RESULTS="${RESULTS:-}"
10+
11+
# Format commit message to escape backticks or special chars
12+
ESCAPED_MSG=$(echo "$COMMIT_MSG" | sed 's/`/\\`/g')
13+
14+
# If no validation results, say so
15+
if [[ -z "$RESULTS" ]]; then
16+
RESULTS="_No files were validated in this run._"
17+
fi
18+
519
COMMENT="$(cat <<EOF
620
🧪 **KubeCheck Validation Results**
721
822
🕒 Commit Time: \`${COMMIT_TIME}\`
9-
💬 Message: _${COMMIT_MSG}_
23+
💬 Message: _${ESCAPED_MSG}_
1024
1125
${SUMMARY}
1226

Diff for: .github/workflows/kubecheck.yml

+41-15
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ jobs:
2828
runs-on: ubuntu-latest
2929
needs: fetch_commit_info
3030
outputs:
31-
summary: ${{ steps.kubeval.outputs.summary }}
32-
results: ${{ steps.kubeval.outputs.results }}
31+
summary: ${{ steps.kubeval.outputs.summary || steps.kubeval_skip.outputs.summary }}
32+
results: ${{ steps.kubeval.outputs.results || steps.kubeval_skip.outputs.results }}
3333
steps:
3434
- name: Checkout code
3535
uses: actions/checkout@v3
@@ -39,14 +39,28 @@ jobs:
3939
curl -L https://github.com/yannh/kubeconform/releases/latest/download/kubeconform-linux-amd64.tar.gz | tar xz
4040
sudo mv kubeconform /usr/local/bin/
4141
42+
- name: Detect changed YAML files
43+
id: detect_changes
44+
run: |
45+
git fetch origin ${{ github.event.before }}
46+
CHANGED_FILES=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep -E '\.ya?ml$' || true)
47+
if [ -z "$CHANGED_FILES" ]; then
48+
echo "No Kubernetes YAML changes found. Skipping validation."
49+
echo "changed=false" >> $GITHUB_OUTPUT
50+
else
51+
echo "$CHANGED_FILES" > changed_yamls.txt
52+
echo "changed=true" >> $GITHUB_OUTPUT
53+
fi
54+
4255
- name: Validate Kubernetes YAML
4356
id: kubeval
57+
if: steps.detect_changes.outputs.changed == 'true'
4458
run: |
4559
set +e
4660
RESULTS=""
4761
PASS_COUNT=0
4862
FAIL_COUNT=0
49-
for file in $(find . -name '*.yaml' -o -name '*.yml'); do
63+
for file in $(cat changed_yamls.txt); do
5064
output=$(kubeconform -strict -verbose "$file" 2>&1)
5165
if echo "$output" | grep -q "PASS"; then
5266
emoji="✅"
@@ -60,10 +74,22 @@ jobs:
6074
6175
SUMMARY="✅ Passed: ${PASS_COUNT} | ❌ Failed: ${FAIL_COUNT}"
6276
echo "$RESULTS" > validation_output.txt
63-
echo "::set-output name=results::$RESULTS"
64-
echo "::set-output name=summary::$SUMMARY"
77+
78+
echo "results<<EOF" >> $GITHUB_OUTPUT
79+
echo "$RESULTS" >> $GITHUB_OUTPUT
80+
echo "EOF" >> $GITHUB_OUTPUT
81+
82+
echo "summary=${SUMMARY}" >> $GITHUB_OUTPUT
83+
84+
- name: Set summary for skipped validation
85+
if: steps.detect_changes.outputs.changed == 'false'
86+
id: kubeval_skip
87+
run: |
88+
echo "summary=✅ No Kubernetes YAML files changed. Skipped validation." >> $GITHUB_OUTPUT
89+
echo "results=" >> $GITHUB_OUTPUT
6590
6691
- name: Fail if any errors
92+
if: steps.detect_changes.outputs.changed == 'true'
6793
run: |
6894
if grep -q "❌" validation_output.txt; then
6995
echo "Validation failed."
@@ -75,13 +101,13 @@ jobs:
75101
needs: [fetch_commit_info, validate_kubeconform]
76102
if: always()
77103
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
104+
- name: Comment on commit
105+
env:
106+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
107+
SHA: ${{ needs.fetch_commit_info.outputs.sha }}
108+
COMMIT_MSG: ${{ needs.fetch_commit_info.outputs.message }}
109+
COMMIT_TIME: ${{ needs.fetch_commit_info.outputs.timestamp }}
110+
SUMMARY: ${{ needs.validate_kubeconform.outputs.summary }}
111+
RESULTS: ${{ needs.validate_kubeconform.outputs.results }}
112+
REPO: ${{ github.repository }}
113+
run: chmod +x .github/scripts/comment-kubecheck.sh && bash .github/scripts/comment-kubecheck.sh

0 commit comments

Comments
 (0)