Skip to content

Deploy Blazor to GitHub Pages #1

Deploy Blazor to GitHub Pages

Deploy Blazor to GitHub Pages #1

Workflow file for this run

name: Deploy Blazor to GitHub Pages
on:
push:
branches: [ main, master ]
paths:
- 'BlazorTextDiff.Web/**'
- 'BlazorTextDiff/**'
workflow_dispatch:
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false
env:
DOTNET_VERSION: '9.0.x'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Install .NET workloads
run: dotnet workload install wasm-tools
- name: Generate version number
id: version
run: |
UTC_DATE=$(date -u '+%Y.%m.%d')
START_OF_DAY=$(date -u -d "today 00:00:00" '+%s')
CURRENT_TIME=$(date -u '+%s')
SECONDS_SINCE_MIDNIGHT=$(( (CURRENT_TIME - START_OF_DAY) / 2 ))
VERSION="$UTC_DATE.$SECONDS_SINCE_MIDNIGHT"
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Generated version: $VERSION"
- name: Setup Pages
uses: actions/configure-pages@v5
- name: Restore dependencies
run: |
dotnet workload restore
dotnet restore
- name: Build Blazor WebAssembly
run: dotnet publish BlazorTextDiff.Web/BlazorTextDiff.Web.csproj -c Release -o dist/ -p:Version=${{ steps.version.outputs.version }} -p:PublishUrl=/BlazorTextDiff/
- name: Add .nojekyll file
run: touch dist/wwwroot/.nojekyll
- name: Add version info to site
working-directory: dist/wwwroot
run: |
BUILD_DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)
echo "{\"version\":\"${{ steps.version.outputs.version }}\",\"buildDate\":\"$BUILD_DATE\",\"commit\":\"${{ github.sha }}\",\"run\":\"${{ github.run_id }}\"}" > version.json
sed -i '/<\/head>/i \ \ \ \ <!-- Build: ${{ steps.version.outputs.version }} | Commit: ${{ github.sha }} | Run: ${{ github.run_id }} -->' index.html
- name: Change base-tag in index.html for GitHub Pages
working-directory: dist/wwwroot
run: sed -i 's|<base href="/" />|<base href="/BlazorTextDiff/" />|g' index.html
- name: Fix service-worker-assets.js hashes (if exists)
working-directory: dist/wwwroot
run: |
jsFile=$(find . -name 'service-worker-assets.js' | head -1)
if [ ! -z "$jsFile" ]; then
echo "Updating $jsFile"
sed -i 's|"hash": "[^"]*"|"hash": "sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="|g' "$jsFile"
fi
- name: Copy index.html to 404.html for SPA routing
working-directory: dist/wwwroot
run: cp index.html 404.html
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: dist/wwwroot
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4