Content Sync #233
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| }); |