-
-
Notifications
You must be signed in to change notification settings - Fork 0
94 lines (75 loc) · 3.01 KB
/
validate-workflows.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
name: Validate Workflows
permissions:
contents: read
on:
pull_request:
paths:
- .github/workflows/**
jobs:
validate:
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
- name: Cache cargo registry
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-registry-
- name: Install zizmor
run: cargo install --force zizmor
- name: Validate all workflows
working-directory: .github/workflows
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BASE_REF: ${{ github.base_ref }}
run: |
# Exit on any error
set -euo pipefail
# Initialize error flag
has_errors=0
# Validate inputs
if [ -z "${BASE_REF:-}" ]; then
echo "::error::BASE_REF is not set"
exit 1
fi
# Properly escape the base ref for use in git commands
base_ref=$(printf '%q' "$BASE_REF")
# Get list of changed files with error handling
changed_files=$(git diff --name-only "origin/${base_ref}" 2>/dev/null | \
grep -E '^\.github/workflows/[^/]+\.yml$' || true)
if [ -z "$changed_files" ]; then
echo "No workflow files changed"
exit 0
fi
# Loop through changed workflow files
while IFS= read -r file; do
[ -z "$file" ] && continue
# Safely handle filenames
filename=$(basename -- "$file")
# Skip non-yaml files
if [[ ! "$filename" =~ \.(ya?ml)$ ]]; then
continue
}
echo "Validating $filename..."
if ! zizmor "$filename" 2>/dev/null; then
echo "::error::Validation failed for $filename"
has_errors=1
fi
done <<< "$changed_files"
# Exit with error if any validation failed
if [ "$has_errors" -eq 1 ]; then
echo "::error::One or more workflow validations failed"
exit 1
fi
echo "All workflows validated successfully"