Update Calendar and Deploy to GitHub Pages #688
This file contains 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: Update Calendar and Deploy to GitHub Pages | |
on: | |
schedule: | |
- cron: "0 */6 * * *" # Every 6 hours | |
- cron: "0 6 * * *" # Run at 6:00 UTC every day | |
- cron: "0 7 * * *" # Run at 7:00 UTC every day | |
workflow_dispatch: # Allows manual triggering of the workflow | |
push: | |
branches: ["main"] # Triggers deployment when changes are pushed to main | |
permissions: | |
actions: read # Allow reading of workflow run details and artifacts | |
contents: read # Allow reading repository contents | |
jobs: | |
update-calendar: | |
runs-on: windows-latest | |
outputs: | |
changes_detected: ${{ steps.compare.outputs.changes_detected }} | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
# Download the previous script output (if available) | |
- name: Download Previous Script Output | |
uses: dawidd6/action-download-artifact@v6 | |
with: | |
workflow: GeneratePage.yml # Your workflow file name | |
name: script-output # Name of the artifact to download | |
path: previous_script_output.txt | |
branch: main | |
continue-on-error: true # Ignore errors if there's no previous artifact (first run) | |
# Download the previous script output (if available) | |
- name: Download Previous ICS | |
uses: dawidd6/action-download-artifact@v6 | |
with: | |
workflow: GeneratePage.yml # Your workflow file name | |
name: calendar | |
path: previous_calendar.ics | |
branch: main | |
continue-on-error: true # Ignore errors if there's no previous artifact (first run) | |
# Debugging: List files after downloading the artifact | |
- name: List Files After Download | |
run: | | |
Write-Host "Files in current directory:" | |
Get-ChildItem -Recurse | |
shell: pwsh | |
# Create output directory | |
- name: Create Output Directory | |
run: New-Item -ItemType Directory -Path .out -Force | |
shell: pwsh | |
# Run your PowerShell script and capture output | |
- name: Run PowerShell Script and Capture Output | |
run: | | |
if (Test-Path "./previous_calendar.ics/calendar.ics") { | |
Write-Host ::notice::Appending to the previous calendar. | |
./timeTableToIcs.ps1 -appendToPreviousICSat "./previous_calendar.ics/calendar.ics" -OutputFilePath ".out/calendar.ics" -baseUrl ${{ secrets.BASE_URL }} -elementId ${{ secrets.ELEMENT_ID }} -overrideSummaries ${{ secrets.OVERRIDE_SUMMARIES }} -outAllFormats -cookie ${{ secrets.COOKIE }} -tenantId ${{ secrets.TENANT_ID }} | Tee-Object -FilePath script_output.txt -Encoding utf8 | |
} else { | |
./timeTableToIcs.ps1 -OutputFilePath ".out/calendar.ics" -baseUrl ${{ secrets.BASE_URL }} -elementId ${{ secrets.ELEMENT_ID }} -overrideSummaries ${{ secrets.OVERRIDE_SUMMARIES }} -outAllFormats -cookie ${{ secrets.COOKIE }} -tenantId ${{ secrets.TENANT_ID }} | Tee-Object -FilePath script_output.txt -Encoding utf8 | |
} | |
shell: pwsh | |
# Debugging: List files after downloading the artifact | |
- name: List Files After Generation | |
run: | | |
Write-Host "Files in .out/:" | |
Get-ChildItem ./.out/ -Recurse | |
shell: pwsh | |
# Compare new script output with the previous script output | |
- name: Compare Script Outputs | |
id: compare | |
run: | | |
$previousScriptOutputPath = "previous_script_output.txt/script_output.txt" | |
Write-Host "Previous script output path: $previousScriptOutputPath" | |
if (Test-Path $previousScriptOutputPath) { | |
Write-Host "Comparing previous script output with the new one:" | |
# Compare the two files | |
$diff = Compare-Object -ReferenceObject (Get-Content $previousScriptOutputPath) -DifferenceObject (Get-Content script_output.txt) | Out-String | |
if ($diff -ne "") { | |
Write-Host "::group::Differences found in script output:" | |
Write-Host "$diff" | |
Write-Host "::endgroup::" | |
Write-Host "::notice::Changes detected in script output." | |
echo "changes_detected=true" >> $env:GITHUB_OUTPUT | |
} else { | |
Write-Host "::notice::No differences found in script output." | |
echo "changes_detected=false" >> $env:GITHUB_OUTPUT | |
} | |
} else { | |
Write-Host "::notice::No previous script output found. Assuming changes." | |
echo "changes_detected=true" >> $env:GITHUB_OUTPUT | |
} | |
echo "changes_detected=true" >> $env:GITHUB_OUTPUT # TODO: fix comparison | |
shell: pwsh | |
# Upload the new script output as an artifact for future comparison | |
- name: Upload Script Output Artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: script-output | |
path: script_output.txt | |
# Upload the new calendar(s) as an artifact for deployment | |
- name: Upload Artifacts | |
uses: actions/upload-artifact@v4 | |
with: | |
name: calendar | |
path: ./.out/* | |
if-no-files-found: error | |
overwrite: false | |
include-hidden-files: true | |
deploy: | |
needs: update-calendar | |
runs-on: ubuntu-latest | |
if: needs.update-calendar.outputs.changes_detected == 'true' | |
permissions: | |
contents: read | |
pages: write | |
id-token: write | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Download ICS Artifact | |
uses: actions/download-artifact@v4 | |
with: | |
name: calendar | |
path: '.out' | |
# Generate index.html with links to all files in .out directory | |
- name: Generate index.html | |
run: | | |
echo "<!DOCTYPE html>" > .out/index.html | |
echo "<html>" >> .out/index.html | |
echo "<head>" >> .out/index.html | |
echo " <meta charset=\"UTF-8\" />" >> .out/index.html | |
echo " <title>Generated Files</title>" >> .out/index.html | |
# Quick inline CSS | |
echo " <style>body{font-family:sans-serif;margin:1rem;background:#fafafa}li{margin:0.5rem 0}a{color:#007bff;}a:hover{text-decoration:underline;}button{margin-left:0.5rem;background:#007bff;color:#fff;border:none;border-radius:4px;padding:0.3rem 0.6rem;cursor:pointer;}button:hover{background:#0056b3;}</style>" >> .out/index.html | |
# JavaScript for copying link | |
echo " <script>" >> .out/index.html | |
echo " function copyLink(link) {" >> .out/index.html | |
echo " navigator.clipboard.writeText(link);" >> .out/index.html | |
echo " alert('Link copied: ' + link);" >> .out/index.html | |
echo " }" >> .out/index.html | |
echo " </script>" >> .out/index.html | |
echo "</head>" >> .out/index.html | |
echo "<body>" >> .out/index.html | |
echo " <h1>Generated Files</h1>" >> .out/index.html | |
echo " <ul>" >> .out/index.html | |
for file in .out/*; do | |
fname=$(basename "$file") | |
# Skip index.html itself | |
if [ "$fname" != "index.html" ]; then | |
echo " <li>" >> .out/index.html | |
# 1) Clicking the filename now copies its link instead of opening | |
echo " <a href=\"#\" onclick=\"copyLink(window.location.origin + window.location.pathname + '$fname'); return false;\">$fname</a>" >> .out/index.html | |
# 2) The button actually opens (downloads/displays) the file | |
echo " <button onclick=\"window.open('$fname', '_blank');\">Download</button>" >> .out/index.html | |
echo " </li>" >> .out/index.html | |
fi | |
done | |
echo " </ul>" >> .out/index.html | |
echo "</body>" >> .out/index.html | |
echo "</html>" >> .out/index.html | |
shell: bash | |
- name: Setup Pages | |
uses: actions/configure-pages@v5 | |
- name: Upload Artifact | |
uses: actions/upload-pages-artifact@v3 | |
with: | |
path: '.out' # Directory containing the build artifacts | |
- name: Deploy to GitHub Pages | |
id: deployment | |
uses: actions/deploy-pages@v4 |