Skip to content

Commit dfecc71

Browse files
committed
github: Add filter to only run jobs for changed plugins
Signed-off-by: René Dudfield <[email protected]>
1 parent d3ac8d7 commit dfecc71

1 file changed

Lines changed: 45 additions & 8 deletions

File tree

.github/workflows/headlamp-plugin-github-workflow.yaml

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,64 @@ on:
55
branches: [main]
66
pull_request:
77
branches: [main]
8-
8+
99
permissions:
1010
contents: read
1111

1212
env:
1313
HEADLAMP_PLUGIN_VERSION: latest
1414

1515
jobs:
16-
# Get list of folders containing headlamp plugins
16+
# Get list of folders containing changed headlamp plugins
1717
# We need a separate step to be able to pass the list to the matrix of the build job
18-
find_plugin_dirs:
18+
find_changed_plugin_dirs:
1919
runs-on: ubuntu-latest
2020
outputs:
2121
dirs: ${{ steps.dirs.outputs.dirs }}
2222
steps:
23-
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # 4.1.7
24-
- id: dirs
25-
run: echo "dirs=$(grep -m1 -lR @kinvolk/headlamp-plugin ./*/package.json | xargs -n1 dirname | jq --raw-input --slurp --compact-output 'split("\n")[:-1]')" >> ${GITHUB_OUTPUT}
23+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
24+
with:
25+
fetch-depth: 0 # Fetch all history
26+
27+
- name: Fetch base branch
28+
run: git fetch origin main
29+
30+
- name: Find changed Headlamp plugin directories
31+
id: dirs
32+
run: |
33+
# Get all top-level directories that had changes
34+
changed_dirs=$(git diff --name-only origin/main...HEAD | awk -F/ '{print $1}' | sort -u)
35+
echo "Changed directories: $changed_dirs"
36+
37+
# If .github/ changed, run all plugins
38+
if echo "$changed_dirs" | tr ' ' '\n' | grep -q '^\.github$'; then
39+
echo "Changes detected in .github/ directory, running all plugins."
40+
changed_dirs=$(grep -lR 'headlamp-plugin' ./*/package.json | xargs -n1 dirname | sort -u)
41+
echo "Changed directories: $changed_dirs"
42+
fi
43+
44+
plugin_dirs=()
45+
for dir in $changed_dirs; do
46+
pkg="$dir/package.json"
47+
if [ -f "$pkg" ] && grep -q 'headlamp-plugin' "$pkg"; then
48+
plugin_dirs+=("$dir")
49+
fi
50+
done
2651
52+
# Output as JSON array for matrix
53+
if [ ${#plugin_dirs[@]} -eq 0 ]; then
54+
echo "No changed Headlamp plugins found."
55+
final_json="[]"
56+
else
57+
echo "Changed Headlamp plugin directories: ${plugin_dirs[*]}"
58+
final_json=$(printf '%s\n' "${plugin_dirs[@]}" | jq --raw-input --slurp --compact-output 'split("\n")[:-1]')
59+
fi
60+
echo "Final plugin_dirs JSON: $final_json"
61+
echo "dirs=$final_json" >> "$GITHUB_OUTPUT"
62+
2763
build:
28-
needs: find_plugin_dirs
64+
needs: find_changed_plugin_dirs
65+
if: needs.find_changed_plugin_dirs.outputs.dirs != '[]'
2966
runs-on: ubuntu-latest
3067

3168
defaults:
@@ -36,7 +73,7 @@ jobs:
3673
fail-fast: false
3774
matrix:
3875
node-version: [18.x]
39-
dir: ${{ fromJson(needs.find_plugin_dirs.outputs.dirs) }}
76+
dir: ${{ fromJson(needs.find_changed_plugin_dirs.outputs.dirs) }}
4077

4178
steps:
4279
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # 4.1.7

0 commit comments

Comments
 (0)