-
Notifications
You must be signed in to change notification settings - Fork 3
254 lines (211 loc) Β· 10.1 KB
/
content-sync.yml
File metadata and controls
254 lines (211 loc) Β· 10.1 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
name: Content Sync
on:
push:
branches: [main]
paths: ['Content/**']
workflow_dispatch:
schedule:
- cron: '0 0 * * *' # Run daily at midnight UTC
jobs:
sync-content:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- name: Count Content Articles
id: count-articles
shell: pwsh
run: |
$count = (Get-ChildItem -Path "Content" -Recurse -File -Filter "*.md").Count
"article-count=$count" >> $env:GITHUB_OUTPUT
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '9.0.x'
- name: Cache NuGet packages
uses: actions/cache@v4
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj', '**/packages.lock.json') }}
restore-keys: |
${{ runner.os }}-nuget-
- name: Cache .NET build outputs
uses: actions/cache@v4
with:
path: |
**/bin
**/obj
key: ${{ runner.os }}-dotnet-build-${{ hashFiles('**/*.csproj', '**/*.cs', '**/Program.cs') }}
restore-keys: |
${{ runner.os }}-dotnet-build-
- name: Restore dependencies
run: dotnet restore ContentLoader/ContentLoader.csproj
- name: Build ContentLoader
run: dotnet build ContentLoader/ContentLoader.csproj --configuration Release --no-restore
- name: Upload Content
id: upload-content
env:
AZURE_STORAGE_CONNECTION_STRING: ${{ secrets.CONTENT_STORAGE_CONNECTION_STRING }}
run: |
# Capture the output from the ContentLoader
output=$(dotnet run --project ContentLoader/ContentLoader.csproj --configuration Release --no-build -- Content 2>&1)
echo "$output" # Extract sync summary numbers using awk for more reliable parsing
added=$(echo "$output" | awk '/Added:/ {print $2}')
updated=$(echo "$output" | awk '/Updated:/ {print $2}')
unchanged=$(echo "$output" | awk '/Unchanged:/ {print $2}')
failed=$(echo "$output" | awk '/Failed:/ {print $2}')
total=$(echo "$output" | awk '/Total:/ {print $2}')
# Fallback to 0 if extraction failed
added=${added:-0}
updated=${updated:-0}
unchanged=${unchanged:-0}
failed=${failed:-0}
total=${total:-0}
# Debug output
echo "Extracted values: added=$added, updated=$updated, unchanged=$unchanged, failed=$failed, total=$total"
# Set outputs for the next steps
echo "added=$added" >> $GITHUB_OUTPUT
echo "updated=$updated" >> $GITHUB_OUTPUT
echo "unchanged=$unchanged" >> $GITHUB_OUTPUT
echo "failed=$failed" >> $GITHUB_OUTPUT
echo "total=$total" >> $GITHUB_OUTPUT
# Check if upload failed
if [ "$failed" != "0" ]; then
echo "Content upload failed with $failed failures"
exit 1
fi
- name: Create Content Sync Summary
if: always()
run: |
# Get the values from the previous step with fallback defaults
added="${{ steps.upload-content.outputs.added }}"
updated="${{ steps.upload-content.outputs.updated }}"
unchanged="${{ steps.upload-content.outputs.unchanged }}"
failed="${{ steps.upload-content.outputs.failed }}"
total="${{ steps.upload-content.outputs.total }}"
article_count="${{ steps.count-articles.outputs.article-count }}"
# Ensure all values are numbers (fallback to 0 if empty)
added=${added:-0}
updated=${updated:-0}
unchanged=${unchanged:-0}
failed=${failed:-0}
total=${total:-0}
article_count=${article_count:-0}
# Debug output
echo "Summary values: added=$added, updated=$updated, unchanged=$unchanged, failed=$failed, total=$total, article_count=$article_count"
# Calculate success rate
if [ "$total" -gt 0 ]; then
success_rate=$(( (total - failed) * 100 / total ))
else
success_rate=0
fi
# Determine sync status
if [ "$failed" = "0" ]; then
sync_status="π’ All Good!"
else
sync_status="π΄ Some Issues"
fi
# Create a beautiful summary with emojis and markdown formatting
cat >> $GITHUB_STEP_SUMMARY << EOF
## π Content Sync Report
| Status | Count | Description |
|--------|-------|-------------|
| β
Added | ${added} | New content files uploaded |
| π Updated | ${updated} | Existing content updated |
| βͺ Unchanged | ${unchanged} | Files with no changes |
| β Failed | ${failed} | Upload failures |
| π **Total** | **${total}** | **Total files processed** |
### π― Summary
- **Success Rate**: ${success_rate}%
- **Content Articles**: ${article_count} total files in repository
- **Sync Status**: ${sync_status}
> π **Copilot That Jawn** content is now synced and ready to help developers level up their AI game!
EOF
- name: Refresh Web App Cache
if: success()
env:
CACHE_REFRESH_API_KEY: ${{ secrets.CACHE_REFRESH_API_KEY }}
run: |
echo "Refreshing web application cache..."
# Wait a moment for the content to be fully uploaded
sleep 5
# Call the cache refresh endpoint with API key authentication
response=$(curl -s -w "%{http_code}" -X POST "https://copilotthatjawn.com/api/cache/refresh" \
-H "X-API-Key: $CACHE_REFRESH_API_KEY" \
-o /tmp/cache_response.json)
if [ "$response" = "200" ]; then
echo "Cache refresh successful"
cat /tmp/cache_response.json
else
echo "Cache refresh failed with HTTP status: $response"
cat /tmp/cache_response.json || echo "No response body"
# Don't fail the workflow if cache refresh fails - it's not critical
echo "Continuing workflow despite cache refresh failure..."
fi
- name: Handle Failure
if: failure()
run: |
echo "Content sync failed. Check the build output and Azure Storage connection string."
exit 1
- name: Update README Badges
if: success()
shell: pwsh
run: |
$date = Get-Date -Format "yyyy--MM--dd"
$articleCount = "${{ steps.count-articles.outputs.article-count }}"
# Create badge URLs using shields.io
$lastUpdateBadge = ""
$articleCountBadge = ""
# Read current README content
$readmeContent = Get-Content README.md -Raw
# Replace existing badges or add new ones at the top of the file
$badgeSection = "${lastUpdateBadge}`n${articleCountBadge}`n"
if ($readmeContent -match "!\[Content Last Updated\].*`n!\[Content Articles\].*`n") {
$readmeContent = $readmeContent -replace "!\[Content Last Updated\].*`n!\[Content Articles\].*`n", $badgeSection
} else {
$readmeContent = $badgeSection + $readmeContent
}
# Write updated content back to README
$readmeContent | Set-Content README.md -NoNewline
- name: Commit README Changes
if: success()
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add README.md
git commit -m "docs: update content sync badges [skip ci]" || exit 0
git push
- name: Comment on PR
if: github.event_name == 'pull_request' && always()
uses: actions/github-script@v7
with:
script: |
const added = '${{ steps.upload-content.outputs.added }}';
const updated = '${{ steps.upload-content.outputs.updated }}';
const unchanged = '${{ steps.upload-content.outputs.unchanged }}';
const failed = '${{ steps.upload-content.outputs.failed }}';
const total = '${{ steps.upload-content.outputs.total }}';
const success = failed === '0';
const successRate = total > 0 ? Math.round(((total - failed) * 100) / total) : 0;
const body = `## π Content Sync Results
| Status | Count | Description |
|--------|-------|-------------|
| β
Added | ${added} | New content files uploaded |
| π Updated | ${updated} | Existing content updated |
| βͺ Unchanged | ${unchanged} | Files with no changes |
| β Failed | ${failed} | Upload failures |
| π **Total** | **${total}** | **Total files processed** |
### π― Results Summary
- **Success Rate**: ${successRate}%
- **Sync Status**: ${success ? 'π’ All content synced successfully!' : 'π΄ Some content failed to sync'}
${success
? '> π **Great job!** All content changes have been successfully synced to the cloud storage.'
: `> β οΈ **Attention needed:** ${failed} file(s) failed to sync. Please check the workflow logs for details.`
}
*This comment was automatically generated by the Content Sync workflow.*`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: body
});