-
Notifications
You must be signed in to change notification settings - Fork 1
78 lines (64 loc) · 2.39 KB
/
sync.yml
File metadata and controls
78 lines (64 loc) · 2.39 KB
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
name: GitHub Sync (Markdown Only - Raw Content)
on:
push:
branches:
- main
pull_request:
jobs:
sync_changes:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Install jq (if not already available)
run: sudo apt-get install jq -y
- name: Get changed and deleted Markdown files
id: changes
run: |
echo "Detecting changed Markdown files..."
git fetch origin ${{ github.event.before }}
CHANGED_FILES=$(git diff --diff-filter=ACMRT --name-only ${{ github.event.before }} ${{ github.sha }} | grep '\.md$' || true)
DELETED_FILES=$(git diff --diff-filter=D --name-only ${{ github.event.before }} ${{ github.sha }} | grep '\.md$' || true)
echo "changed_files<<EOF" >> $GITHUB_ENV
echo "$CHANGED_FILES" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "deleted_files<<EOF" >> $GITHUB_ENV
echo "$DELETED_FILES" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Prepare sync payload
id: prepare
run: |
echo "Preparing payload..."
mkdir temp_sync
FILES_JSON="[]"
for file in ${{ env.changed_files }}; do
if [ -f "$file" ]; then
content=$(jq -Rs '.' < "$file")
FILES_JSON=$(jq --arg path "$file" --argjson content "$content" '. + [{path: $path, content: $content}]' <<< "$FILES_JSON")
fi
done
DELETED_JSON="[]"
for file in ${{ env.deleted_files }}; do
DELETED_JSON=$(jq --arg path "$file" '. + [$path]' <<< "$DELETED_JSON")
done
echo $FILES_JSON > temp_sync/files.json
echo $DELETED_JSON > temp_sync/deleted.json
- name: Sync to API
run: |
echo "Sending sync request..."
FILES=$(cat temp_sync/files.json)
DELETED=$(cat temp_sync/deleted.json)
curl -X POST "${{ secrets.SYNC_ENDPOINT_URL }}/github/sync" \
-H "Authorization: Bearer ${{ secrets.SYNC_API_KEY }}" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"repo": "${{ github.repository }}",
"files": $FILES,
"deleted": $DELETED
}
EOF