如何在DevOps中部署由pytest生成的HTML静态网站作为Web应用程序?

huangapple go评论70阅读模式
英文:

how to deploy as a web app the html static website generated by pytest in devops?

问题

我正在使用Microsoft DevOps Forge构建我的流水线。我想要创建一个测试流水线,将生成的HTML覆盖文件夹导出并部署为Web应用程序,以便开发人员可以监视哪些文件需要维护。

这是我的test.yaml文件:

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"

希望这对你有所帮助。

英文:

I'm building my pipelines in the Microsoft dev Ops forge. I would like to make the test pipeline to export the generated html coverage folder and deploy it as webb app. To allow developer to monitor which files need maintainer love.

Here is my test.yaml file:

trigger:
  branches:
    include:
      - main

pool:
  vmImage: windows-latest

steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: "3.10"
    displayName: "Use Python 3.10"
  - script: python -m pip install toto pytest pytest-cov pytest-deadfixtures
    displayName: "Install dependencies"
  - script: pytest --color=yes --cov --cov-report=html
    displayName: "Test with pytest"
  - task: PublishBuildArtifacts@1
    displayName: "Publish coverage"
    inputs:
      pathToPublish: "./htmlcov/"
      artifactName: "coverage"

答案1

得分: 0

准备静态 Web 应用

部署静态资源的第一步是创建将要部署文件的 Web 应用程序。

要创建静态 Web 应用,请首先访问 Azure 门户:https://portal.azure.com/
然后点击 创建新资源 并选择 静态 Web 应用,然后选择 创建

如果您没有任何订阅,您可能需要申请 Azure 服务的免费试用。不要担心,静态 Web 应用对于开源/娱乐应用是免费的。

使用特定于您的 Azure 订阅和首选参数的值创建一个新的静态 Web 应用程序。

您可以使用现有的资源组或创建一个新的资源组。

部署详细信息 中,点击 "其他",最后选择 创建 + 查看。SWA 部署将启动。复制部署令牌并将部署令牌值粘贴到文本编辑器中,以便在另一个屏幕上使用。然后点击关闭按钮。

有关更详细的信息和一些截图,可以在 此文章 中找到,但请注意自从发布以来一些按钮已更改。

在 Azure DevOps 中创建管道任务

在管道中,我们不需要使用构建产物,因为htmlcov文件夹是由测试生成的。所以在生成之后:

- script: pytest --color=yes --cov --cov-report=html
    displayName: "使用 pytest 运行测试"

添加一个额外的步骤将生成的文件夹传送到应用程序:

- task: AzureStaticWebApp@0
    inputs:
      app_location: "/htmlcov"
      azure_static_web_apps_api_token: $(DEPLOYMENT_TOKEN)

DEPLOYMENT_TOKEN 添加到管道的变量列表中。

不要忘记勾选隐藏变量框。

运行管道,文件将自动传送到应用程序。

英文:

Prepare the static webApp

The first step to deploy your static resource is to create the web application where the files will be deployed.

To create a static web App, first go to the Azure portal: https://portal.azure.com/
There click on create new resource and select Static Web Apps and then Select Create.

> you will may need to request free trial of Azure services if you don't have any subscription. Don't be alamarmed the static web app is free for open-source/recreational application.

Create a new static web app with the values specific to your Azure subscription and your preferred parameter arguments.

> you can use an existing Resource Group or create a new one.

In deployment details click on "other" and finally select create + review. The SWA deployment will start. Copy the deployment token and paste the deployment token value into a text editor for use on another screen. Click the Close button.

More detailed information and some screenshots can be found in this article be aware that some buttons have changed since its publication.

Create the Pipeline Task in Azure DevOps

In the pipeline we don't need to use artifacts as the htmlcov folder is generated by the test. So after generation:

- script: pytest --color=yes --cov --cov-report=html
    displayName: "Test with pytest"

Add an extra step shipping the generated folder to the application:

- task: AzureStaticWebApp@0
    inputs:
      app_location: "/htmlcov"
      azure_static_web_apps_api_token: $(DEPLOYMENT_TOKEN)

Add the DEPLOYMENT_TOKEN to the variable list of the pipeline.

> Don't foget to tick the hidden variable box

Run the pipeline and the files will be automatically shiped to the application.

huangapple
  • 本文由 发表于 2023年7月7日 02:54:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定