You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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.

Step 1: Shell Script to Increment Version Variables

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.

Step 2: Update Your csproj File

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
Step 4: Integrate into TFS 2017 Build Pipeline

Now wire this up to your TFS build:

  1. In your build definition, add a Shell Script task (use a Command Line task with Git Bash if you're on a Windows agent).
  2. Paste the full combined script into the task's "Script" field, or save it as version-updater.sh in your repo and point the task to that file.
  3. Ensure this task runs before your "Build Solution" task—so the build uses the updated version number.
  4. 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).
Troubleshooting Tips
  • Windows Agent Compatibility: If you're using a Windows build agent, sed might not be available by default. Use Git Bash as your shell, or switch to a PowerShell version of the script (replace sed with Get-Content/Set-Content with 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:20:17