如何在Microsoft DevOps中将pytest生成的HTML静态覆盖率报告部署为Web应用
实现Azure DevOps测试流水线导出并部署Coverage报告为Web应用
现有流水线配置(已翻译为中文)
trigger: branches: include: - main pool: vmImage: windows-latest steps: - task: UsePythonVersion@0 inputs: versionSpec: "3.10" displayName: "使用Python 3.10" - script: python -m pip install toto pytest pytest-cov pytest-deadfixtures displayName: "安装依赖包" - script: pytest --color=yes --cov --cov-report=html displayName: "用pytest执行测试" - task: PublishBuildArtifacts@1 displayName: "发布覆盖率报告" inputs: pathToPublish: "./htmlcov/" artifactName: "coverage"
扩展流水线实现Web应用部署
要把生成的htmlcov文件夹部署为可访问的Web应用,可在现有流水线后添加以下步骤:
1. 下载已发布的覆盖率工件
先获取之前发布的coverage工件:
- task: DownloadBuildArtifacts@0 displayName: "下载覆盖率工件" inputs: buildType: 'current' downloadType: 'single' artifactName: 'coverage' downloadPath: '$(System.ArtifactsDirectory)'
2. 部署到Azure静态Web应用
如果使用Azure静态Web Apps,添加部署任务:
- task: AzureStaticWebApp@0 displayName: "部署覆盖率报告到Azure静态Web应用" inputs: app_location: '$(System.ArtifactsDirectory)/coverage' skip_app_build: true azure_static_web_apps_api_token: $(AZURE_STATIC_WEB_APP_TOKEN)
注意:需提前在Azure DevOps库中配置名为AZURE_STATIC_WEB_APP_TOKEN的变量,存储静态Web应用的部署令牌。
3. 替代方案:部署到Azure App Service
如果使用Azure App Service,可使用以下步骤:
- task: AzureRmWebAppDeployment@4 displayName: "部署覆盖率报告到Azure App Service" inputs: ConnectionType: 'AzureRM' azureSubscription: '<你的Azure订阅连接>' appType: 'webApp' WebAppName: '<你的App Service名称>' packageForLinux: '$(System.ArtifactsDirectory)/coverage' enableCustomDeployment: true DeploymentType: 'webDeploy'
最终完整流水线示例
trigger: branches: include: - main pool: vmImage: windows-latest steps: - task: UsePythonVersion@0 inputs: versionSpec: "3.10" displayName: "使用Python 3.10" - script: python -m pip install toto pytest pytest-cov pytest-deadfixtures displayName: "安装依赖包" - script: pytest --color=yes --cov --cov-report=html displayName: "用pytest执行测试" - task: PublishBuildArtifacts@1 displayName: "发布覆盖率报告" inputs: pathToPublish: "./htmlcov/" artifactName: "coverage" # 新增部署步骤 - task: DownloadBuildArtifacts@0 displayName: "下载覆盖率工件" inputs: buildType: 'current' downloadType: 'single' artifactName: 'coverage' downloadPath: '$(System.ArtifactsDirectory)' - task: AzureStaticWebApp@0 displayName: "部署覆盖率报告到Azure静态Web应用" inputs: app_location: '$(System.ArtifactsDirectory)/coverage' skip_app_build: true azure_static_web_apps_api_token: $(AZURE_STATIC_WEB_APP_TOKEN)
内容的提问来源于stack exchange,提问作者Pierrick Rambaud
相关产品推荐
相关产品推荐

