如何在VSTS构建后将类图发布至TFS项目页面?
Great question—this is a common need for keeping stakeholders aligned with code architecture. Here's a step-by-step approach to automate extracting your class diagrams and publishing them to your Azure DevOps (formerly VSTS/TFS) project after every build:
第一步:从代码中提取类图
Assuming your class diagrams are stored as Visual Studio .cd files in your codebase, you have two simple options to export them to viewable formats (like PNG or HTML):
选项1:用Visual Studio命令行导出(最直接)
If your build agent has Visual Studio or Visual Studio Build Tools installed, you can use devenv.exe to export diagrams directly in your pipeline:
- Add a Command Line task to your build pipeline.
- Use this command (adjust paths to match your solution and diagram location):
devenv.exe $(Build.SourcesDirectory)/YourSolution.sln /command "ClassDiagram.ExportDiagram YourProject/ClassDiagrams/OrderSystem.cd /output:$(Build.ArtifactStagingDirectory)/OrderSystem.png /format:png"
- For multiple diagrams, wrap this in a PowerShell script to batch process all
.cdfiles:
$outputDir = "$(Build.ArtifactStagingDirectory)/ClassDiagrams" mkdir $outputDir -Force # Find all .cd files in your repo $diagramFiles = Get-ChildItem -Path $(Build.SourcesDirectory) -Filter *.cd -Recurse foreach ($file in $diagramFiles) { $relativePath = $file.FullName.Substring($(Build.SourcesDirectory).Length + 1) $outputFile = Join-Path $outputDir "$($file.BaseName).png" devenv.exe $(Build.SourcesDirectory)/YourSolution.sln /command "ClassDiagram.ExportDiagram $relativePath /output:$outputFile /format:png" }
选项2:使用预导出的图片(最省心)
If your team already exports class diagrams to PNG/JPG alongside the .cd files, just add a Copy Files task to copy these images to $(Build.ArtifactStagingDirectory)—no extra conversion needed.
第二步:发布到Azure DevOps项目页面
Once you have your diagram files in the build artifact staging directory, choose where to publish them:
发布到Wiki(推荐给分析师/测试人员查阅)
Azure DevOps Wiki supports Markdown, so you can automate updating a dedicated page with your diagrams:
- First, add a Publish Build Artifacts task to push your exported diagrams to a build artifact (name it something like
ClassDiagrams). - Add an Azure CLI task to your pipeline (make sure the build service account has Wiki edit permissions):
- Use this command to update a Wiki page with your diagram (replace placeholders with your org/project/wiki details):
az devops wiki page update --org https://dev.azure.com/YourOrg --project YourProject --wiki YourWikiName --path "Architecture/Class Diagrams" --content "### Latest Class Diagrams\n\n\n\n" --add-attachments $(Build.ArtifactStagingDirectory)/ClassDiagrams/*.png - Alternatively, use a Marketplace extension like Wiki Updater to simplify the process without writing CLI commands.
发布到仪表板
Add your diagrams as visual widgets to your project dashboard:
- After publishing diagrams as build artifacts, navigate to your project dashboard.
- Click Edit > Add widget, then select the Markdown widget.
- In the widget settings, paste a link to your diagram (use the artifact URL, which looks like this):
Note: For a permanent link, upload diagrams to Wiki attachments or Azure Blob Storage instead of using build-specific artifact URLs./artifacts?artifactName=ClassDiagrams&filePath=OrderSystem.png&api-version=7.1-preview.5)
发布到项目主页
Customize your project's landing page to display diagrams:
- Go to your project homepage, click Customize (top-right).
- Add a Markdown section, then embed diagram links the same way you did for the dashboard. You can use the Wiki attachment URLs or permanent storage links for consistency.
关键注意事项
- Permissions: Ensure the build service account (e.g.,
YourProject Build Service) has permissions to edit Wiki pages, publish artifacts, and modify dashboard widgets. - Consistency: If you have multiple diagrams, use consistent naming conventions to make batch processing and publishing easier.
- Agent Setup: For the
devenv.exemethod, confirm your build agent has Visual Studio Build Tools installed with the "Class Designer" workload.
内容的提问来源于stack exchange,提问作者Patrick

