Skip to content

Commit 6d66c1e

Browse files
committed
Add workspace-detector workflow
1 parent a03fedc commit 6d66c1e

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
name: workspace-detector
2+
on:
3+
pull_request:
4+
push:
5+
branches:
6+
- workspace-detector
7+
workflow_dispatch:
8+
9+
jobs:
10+
detect-changes:
11+
runs-on: ubuntu-latest
12+
outputs:
13+
changed_workspaces: ${{ steps.detect.outputs.changed_workspaces }}
14+
changed_workspaces_json: ${{ steps.detect.outputs.changed_workspaces_json }}
15+
all_workspaces: ${{ steps.detect.outputs.all_workspaces }}
16+
has_changes: ${{ steps.detect.outputs.has_changes }}
17+
steps:
18+
- uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0
21+
22+
- uses: ./.github/actions/setup-mise
23+
24+
- name: Detect changed workspaces
25+
id: detect
26+
run: |
27+
set -e
28+
29+
# Determine base commit for comparison
30+
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
31+
base_ref="${{ github.event.pull_request.base.sha }}"
32+
else
33+
# For push events, compare with previous commit
34+
if git rev-parse HEAD~1 >/dev/null 2>&1; then
35+
base_ref="HEAD~1"
36+
else
37+
# First commit in repository
38+
base_ref=""
39+
fi
40+
fi
41+
42+
echo "Base ref: $base_ref"
43+
echo ""
44+
45+
# Use pnpm to detect changed workspaces
46+
if [[ -n "$base_ref" ]]; then
47+
# Get list of changed workspaces using pnpm --filter
48+
# The [...$base_ref] syntax filters to packages that have changes since base_ref
49+
changed_output=$(pnpm list --recursive --depth -1 --filter "...[${base_ref}]" --json 2>/dev/null || echo "[]")
50+
else
51+
# First commit - all workspaces are considered changed
52+
changed_output=$(pnpm list --recursive --depth -1 --json 2>/dev/null || echo "[]")
53+
fi
54+
55+
echo "Changed packages output:"
56+
echo "$changed_output" | jq .
57+
echo ""
58+
59+
# Extract workspace paths from pnpm output
60+
changed_workspaces=$(echo "$changed_output" | jq -r '.[].path // empty' | sed "s|^$PWD/||" || echo "")
61+
62+
# Convert to array
63+
readarray -t changed_array <<< "$changed_workspaces"
64+
65+
# Filter out empty entries
66+
filtered_changed=()
67+
for ws in "${changed_array[@]}"; do
68+
if [[ -n "$ws" ]]; then
69+
filtered_changed+=("$ws")
70+
echo "✓ Changed: $ws"
71+
fi
72+
done
73+
74+
echo ""
75+
76+
# Get all workspaces for reference
77+
all_output=$(pnpm list --recursive --depth -1 --json 2>/dev/null || echo "[]")
78+
all_workspaces=$(echo "$all_output" | jq -r '.[].path // empty' | sed "s|^$PWD/||" || echo "")
79+
80+
# Output results
81+
if [[ ${#filtered_changed[@]} -eq 0 ]]; then
82+
echo "No workspace changes detected"
83+
echo "changed_workspaces=" >> "$GITHUB_OUTPUT"
84+
echo "changed_workspaces_json=[]" >> "$GITHUB_OUTPUT"
85+
echo "has_changes=false" >> "$GITHUB_OUTPUT"
86+
else
87+
echo "Changed workspaces: ${filtered_changed[*]}"
88+
# Output as space-separated string
89+
echo "changed_workspaces=${filtered_changed[*]}" >> "$GITHUB_OUTPUT"
90+
# Output as JSON array (compact format, no newlines)
91+
json_array=$(printf '%s\n' "${filtered_changed[@]}" | jq -R . | jq -s -c .)
92+
echo "changed_workspaces_json=$json_array" >> "$GITHUB_OUTPUT"
93+
echo "has_changes=true" >> "$GITHUB_OUTPUT"
94+
fi
95+
96+
# Output all workspaces for reference (compact format, no newlines)
97+
all_workspaces_json=$(echo "$all_workspaces" | grep -v '^$' | jq -R . | jq -s -c . || echo "[]")
98+
echo "all_workspaces=$all_workspaces_json" >> "$GITHUB_OUTPUT"
99+
100+
- name: Display detection results
101+
run: |
102+
echo "## Workspace Change Detection Results" >> "$GITHUB_STEP_SUMMARY"
103+
echo "" >> "$GITHUB_STEP_SUMMARY"
104+
105+
if [[ "${{ steps.detect.outputs.has_changes }}" == "true" ]]; then
106+
echo "### Changed Workspaces" >> "$GITHUB_STEP_SUMMARY"
107+
echo "" >> "$GITHUB_STEP_SUMMARY"
108+
echo '${{ steps.detect.outputs.changed_workspaces_json }}' | jq -r '.[] | "- `" + . + "`"' >> "$GITHUB_STEP_SUMMARY"
109+
else
110+
echo "No workspace changes detected." >> "$GITHUB_STEP_SUMMARY"
111+
fi
112+
113+
echo "" >> "$GITHUB_STEP_SUMMARY"
114+
echo "### All Workspaces" >> "$GITHUB_STEP_SUMMARY"
115+
echo "" >> "$GITHUB_STEP_SUMMARY"
116+
echo '${{ steps.detect.outputs.all_workspaces }}' | jq -r '.[] | "- `" + . + "`"' >> "$GITHUB_STEP_SUMMARY"
117+
118+
# Example job showing how to use the detection results
119+
example-conditional-job:
120+
needs: detect-changes
121+
if: needs.detect-changes.outputs.has_changes == 'true'
122+
runs-on: ubuntu-latest
123+
strategy:
124+
matrix:
125+
workspace: ${{ fromJson(needs.detect-changes.outputs.changed_workspaces_json) }}
126+
steps:
127+
- uses: actions/checkout@v4
128+
- uses: ./.github/actions/setup-mise
129+
- name: Process changed workspace
130+
run: |
131+
echo "Processing workspace: ${{ matrix.workspace }}"
132+
# Add workspace-specific commands here
133+
# For example:
134+
# cd "${{ matrix.workspace }}" && deno check .
135+
# cd "${{ matrix.workspace }}" && deno test
136+
# Or use pnpm filters:
137+
# pnpm --filter "${{ matrix.workspace }}" test
138+
139+
# Test @fedify/cli when packages/cli changes
140+
test-cli-init:
141+
needs: detect-changes
142+
if: contains(needs.detect-changes.outputs.changed_workspaces, 'packages/cli')
143+
runs-on: ubuntu-latest
144+
steps:
145+
- uses: actions/checkout@v4
146+
- uses: ./.github/actions/setup-mise
147+
- name: Run deno task test-init for @fedify/cli
148+
run: |
149+
cd packages/cli
150+
deno task test-init

0 commit comments

Comments
 (0)