|
| 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