Version Patching .Net Core Applications

How many time have you been investigating a bug just find out that after all the deployed version was wrong? Its not uncommon to support multiple version of the same application for different clients and it is important to have a way to look at your artifact and find out what version it was build on or even a have a git ref for the commit it was build from.

Here is an example of a script that is using git describe command to identify the version and patch the .csproj file with the relevant information.

I tried to comment as much as possible to explain the intention.

# Comment\ Uncomment this for trace output
# set -o xtrace

# Here we search for all .csproj files that are using .net core
# you can change the start path to scan from a different folder in the repo
allProjFiles=$(find . -name *.csproj | xargs grep -rnwl -E "(netcoreapp[0-9]+\.[0-9]+)|netstandard[0-9]+\.[0-9]+")

# git describe tags will return the current tag of the reposiotry
# it assumes your using samever approach with #.#.# format
# commit count will be used as the revision number to auto-increment on every commit made
tag=$(git describe --tags --always --long)
commitCount=$(git rev-list --count HEAD)
commiter=$(git config

echo "tag: $tag"
echo "commit count: $commitCount"
#If no tag has been added only the sha1 will be returned

#This will be the version in the format <major>.<minor>.<build number>.<revision>
major=$(echo $tag | cut -f1 -d'.')
minor=$(echo $tag | cut -f2 -d'.')
build=$(echo $tag | cut -f3 -d'.')

# this will be set as the assembly description
description="$(date +'%Y-%m-%d %H:%M:%S')  \| ${commiter} \| Tag:${tag}"
echo "Identified Version: $version"
echo "Identified Description: $description"

# here we loop over all .csproj files and replace the Version property and AssemblyTitle to add description of commit hash and other usful info
for projFilePath in $allProjFiles; do
	echo "Patching project: $projFilePath"
	sed -i "s|\(<Version>\)[^<]*\(<\/Version>\)|\1$version\2|gi" $projFilePath
	sed -i "s|\(<AssemblyTitle>\)[^<]*\(<\/AssemblyTitle>\)|\1$description\2|gi" $projFilePath

The script above can run as part of the Build process as a pre-build event or as a build step in your favorite CI tool.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.