Show app version in Blazor WebAssembly
I'm currently working on SkyDocs, and I have a pretty nice setup with GitHub actions. Each commit is build and deployed to Sia Skynet. It's easy and fast to test new versions of my app.
But which version of the app am I currently using? I want to show the current version of the app inside the app. I'll walk you through the setup I currently have.
The GitHub Action that deploys each commit on master and each PR is located here: https://github.com/michielpost/SkyDocs/blob/master/.github/workflows/skynet_publish.yml
GitHub Action
I'm using the GitHub Slug Action from https://github.com/rlespinasse/github-slug-action
It injects GitHub environment variables like GITHUB_REF_SLUG
for better version names.
For example, the branch name
refs/heads/feat/new_feature
becomes feat-new-feature
. This can be used as a version name, (1.0.0-feat-new-feature)
I don't want the branch name in my version when I deploy the master
branch. That's why I have this part in the workflow:
- name: "Set master version"
if: ${{ github.ref == 'refs/heads/master'}}
run: echo "APP_VERSION=$APP_VERSION" >> $GITHUB_ENV
env:
APP_VERSION: $(git rev-parse --short "$GITHUB_SHA")
APP_VERSION
is set only to the short commit SHA.
This results in a version like 1.0.0-a30d213
for master branches.
Pull requests get the version: 1.0.0-new-feature-a30d213
Building
This is the command I use to build my app:
run: dotnet publish SkyDocs.Blazor -o publish/SkyDocs.Blazor -c Release --version-suffix ${{ env.APP_VERSION }}
It gives the APP_VERSION environment variable to the version-suffix argument.
In my .csproj
file, I only have the VersionPrefix set:
<VersionPrefix>2.0.0</VersionPrefix>
Showing the version
Using this line, I can get the version at runtime:
Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion
Result
Version: 2.0.0-2eeb71b
Check out: https://skydocs.hns.siasky.net/