Skip to content

Update Calendar and Deploy to GitHub Pages #187

Update Calendar and Deploy to GitHub Pages

Update Calendar and Deploy to GitHub Pages #187

Workflow file for this run

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@v2
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@v2
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
}
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 "<html><body><h1>Generated Files</h1><ul>" > .out/index.html
for file in .out/*; do
fname=$(basename "$file")
if [ "$fname" != "index.html" ]; then
echo "<li><a href=\"$fname\">$fname</a></li>" >> .out/index.html
fi
done
echo "</ul></body></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