如何在TFS 2017构建作业中自动递增ASP.NET Core版本并展示
Got it, let's break this down into actionable steps to get your automated versioning and footer injection working smoothly in TFS 2017 CI. I'll cover the shell script logic, updating your csproj, modifying index.html, and integrating everything into your build pipeline.
First, we'll write a script that reads your existing TFS variables, increments the patch version (adjustable if you want to target major/minor instead), and updates the variables for subsequent build tasks.
# Grab current version values from TFS environment variables (fallback to defaults if unset) CURRENT_MAJOR=${MAJOR:-1} CURRENT_MINOR=${MINOR:-0} CURRENT_PATCH=${PATCH:-0} # Increment patch version (swap this to MINOR/MAJOR if you need different versioning logic) NEW_PATCH=$((CURRENT_PATCH + 1)) NEW_MAJOR=$CURRENT_MAJOR NEW_MINOR=$CURRENT_MINOR # Optional: Reset patch to 0 if minor version was manually updated (uncomment if needed) # if [ "$NEW_MINOR" -ne "$CURRENT_MINOR" ]; then # NEW_PATCH=0 # fi # Push updated variables back to TFS so they persist for other build tasks echo "##vso[task.setvariable variable=Major;]$NEW_MAJOR" echo "##vso[task.setvariable variable=Minor;]$NEW_MINOR" echo "##vso[task.setvariable variable=Patch;]$NEW_PATCH" # Log the new version for debugging echo "Successfully updated version to: $NEW_MAJOR.$NEW_MINOR.$NEW_PATCH"
This script uses TFS's built-in ##vso[task.setvariable] command to update variables in the build context, so downstream tasks (like building your project) will use the fresh version number.
Next, we'll modify the script to overwrite the version values in your csproj file. We'll use sed to target and replace the relevant XML elements:
# Define the full version prefix string NEW_VERSION_PREFIX="$NEW_MAJOR.$NEW_MINOR.$NEW_PATCH" # Update the VersionPrefix element in your csproj sed -i "s/<VersionPrefix>.*<\/VersionPrefix>/<VersionPrefix>$NEW_VERSION_PREFIX<\/VersionPrefix>/" YourProjectName.csproj # Optional: Update the default Major/Minor/Patch values in csproj (so future builds use these as fallbacks) sed -i "s/<Major.*<\/Major>/<Major Condition=\" '\$(Major)' == '' \">$NEW_MAJOR<\/Major>/" YourProjectName.csproj sed -i "s/<Minor.*<\/Minor>/<Minor Condition=\" '\$(Minor)' == '' \">$NEW_MINOR<\/Minor>/" YourProjectName.csproj sed -i "s/<Patch.*<\/Patch>/<Patch Condition=\" '\$(Patch)' == '' \">$NEW_PATCH<\/Patch>/" YourProjectName.csproj
Make sure to replace YourProjectName.csproj with the actual path/name of your project file.
Finally, we'll add logic to insert the new version into your index.html footer. First, add a placeholder in your footer where you want the version to appear—for example:
<footer> <p>© 2024 Your Project</p> <p>Version: <!-- VERSION_PLACEHOLDER --></p> </footer>
Then add this to your shell script to replace the placeholder:
# Replace the placeholder in index.html with the new version sed -i "s/<!-- VERSION_PLACEHOLDER -->/$NEW_VERSION_PREFIX/" index.html
If your footer uses a specific element (like <span id="app-version"></span>), adjust the sed pattern to match that instead:
sed -i "s/<span id=\"app-version\"><\/span>/<span id=\"app-version\">$NEW_VERSION_PREFIX<\/span>/" index.html
Now wire this up to your TFS build:
- In your build definition, add a Shell Script task (use a Command Line task with Git Bash if you're on a Windows agent).
- Paste the full combined script into the task's "Script" field, or save it as
version-updater.shin your repo and point the task to that file. - Ensure this task runs before your "Build Solution" task—so the build uses the updated version number.
- Confirm your Major/Minor/Patch variables are set in the build definition (you mentioned you already added these, double-check they have default values like 1/0/0).
- Windows Agent Compatibility: If you're using a Windows build agent,
sedmight not be available by default. Use Git Bash as your shell, or switch to a PowerShell version of the script (replacesedwithGet-Content/Set-Contentwith regex replaces). - Variable Persistence: TFS 2017 doesn't automatically save updated variables back to the build definition. If you need versions to persist across builds, you can extend the script to update variables via the TFS REST API, or store the latest version in a text file in your repo that the script reads from first.
内容的提问来源于stack exchange,提问作者Boubakr Chieb

