在Azure管道中验证Swagger验证器徽章

时间:2020-10-26 07:33:14

标签: azure-devops azure-pipelines

在Azure管道中,是否有一种方法/任务来使用 Swagger验证器徽章来验证swagger API。 https://github.com/swagger-api/validator-badge

让我知道如何实现此目标,或者我可以使用任何天蓝色的任务。

1 个答案:

答案 0 :(得分:1)

您可以使用脚本任务来调用Swagger验证程序URL。然后检查响应的内容长度。请参阅以下Powershell任务示例:

steps:
- powershell: |
   
   $url ="http://validator.swagger.io/validator?url={YOUR_URL}"
   
   (curl $url).RawContentLength 

如果您想在Swagger验证器返回无效徽章时使任务失败。您可以添加更多脚本以使Powershell任务失败。将下面的脚本添加到上面的任务中。

$length = (curl $url).RawContentLength 

if($length -ne 1599)
{  
    exit 1
}

如果要在“管道”页面上显示“徽章”。您可以使用脚本任务将图像内容写入文件,然后使用Publish HTML任务将标志发布到构建摘要页面。您需要build this task and upload加入您的devops组织。参见以下示例:

steps:
- powershell: |
          
    Add-Content -Path "$(System.DefaultWorkingDirectory)\testhtml.html" -Value "<img src='http://validator.swagger.io/validator?url={YOUR_URL}'>"


- task: Farrellsoft.publish-html-task.publish-html-task.publishHtml@1
  displayName: 'Publish HTML'
  inputs:
    artifactName: swagger
    htmlFilePath: '$(System.DefaultWorkingDirectory)\testhtml.html'

请参见以下示例结果:

enter image description here