Azure DevOps发布管道每个阶段的部署计数

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

Azure Devops release pipeline deployment count for each stage

问题

我已经编写了一个 PowerShell 脚本,用于从 Azure DevOps 发布管道中的每个发布阶段将成功和失败的部署计数写入 Excel 表格,为了获取过去2周的数据。我的脚本没有返回任何数据,请帮忙。

  1. # 设置 Azure DevOps 组织和项目名称
  2. $orgName = "<org-name>"
  3. $projectName = "<project-name>"
  4. # 设置 Azure DevOps 发布管道 ID 和阶段名称
  5. $pipelineId = <pipeline-id>
  6. $stageNames = @("<stage-name-1>", "<stage-name-2>", "<stage-name-3>")
  7. # 设置 Azure DevOps PAT 和基本 URL
  8. $pat = "<personal-access-token>"
  9. $baseUrl = "https://dev.azure.com/$orgName/$projectName"
  10. # 设置查询的日期范围
  11. $startDate = (Get-Date).AddDays(-14).ToString("yyyy-MM-ddTHH:mm:ssZ")
  12. $endDate = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ssZ")
  13. # 创建 Azure DevOps REST API 查询以检索部署结果
  14. $queryUrl = "$baseUrl/_apis/release/deployments?definitionId=$pipelineId&$top=100&$expand=All&$filter=deploymentStatus eq 'succeeded' or deploymentStatus eq 'failed' and startedTime ge $startDate and startedTime le $endDate"
  15. $headers = @{Authorization = "Bearer $pat"}
  16. $response = Invoke-RestMethod -Uri $queryUrl -Headers $headers -Method Get
  17. # 创建一个新的 Excel 工作簿和工作表
  18. $excel = New-Object -ComObject Excel.Application
  19. $workbook = $excel.Workbooks.Add()
  20. $worksheet = $workbook.Worksheets.Add()
  21. # 将部署结果写入 Excel 工作表
  22. $row = 1
  23. $col = 1
  24. $worksheet.Cells.Item($row, $col) = "阶段"
  25. $worksheet.Cells.Item($row, $col+1) = "成功计数"
  26. $worksheet.Cells.Item($row, $col+2) = "失败计数"
  27. $row++
  28. foreach ($stageName in $stageNames) {
  29. $stageDeployments = $response.value | Where-Object {$_.releaseEnvironment.name -eq $stageName}
  30. $successfulCount = ($stageDeployments | Where-Object {$_.deploymentStatus -eq "succeeded"}).Count
  31. $failedCount = ($stageDeployments | Where-Object {$_.deploymentStatus -eq "failed"}).Count
  32. $worksheet.Cells.Item($row, $col) = $stageName
  33. $worksheet.Cells.Item($row, $col+1) = $successfulCount
  34. $worksheet.Cells.Item($row, $col+2) = $failedCount
  35. $row++
  36. }
  37. # 保存 Excel 工作簿并关闭 Excel 应用程序
  38. $workbook.SaveAs("DeploymentResults.xlsx")
  39. $excel.Quit()

如果脚本没有返回任何数据,请确保你已正确设置了组织名称、项目名称、发布管道 ID、阶段名称和个人访问令牌 (PAT)。同时,确保你的 PAT 具有适当的权限来执行所需的操作。

英文:

I have written s PowerShell script to write successful and failed deployment count onto excel sheet from azure DevOps release pipeline each release stage for 2 weeks of data. My script is not returning any data, please help..

  1. # Set the Azure DevOps organization and project name
  2. $orgName = &quot;&lt;org-name&gt;&quot;
  3. $projectName = &quot;&lt;project-name&gt;&quot;
  4. # Set the Azure DevOps release pipeline ID and stage names
  5. $pipelineId = &lt;pipeline-id&gt;
  6. $stageNames = @(&quot;&lt;stage-name-1&gt;&quot;, &quot;&lt;stage-name-2&gt;&quot;, &quot;&lt;stage-name-3&gt;&quot;)
  7. # Set the Azure DevOps PAT and base URL
  8. $pat = &quot;&lt;personal-access-token&gt;&quot;
  9. $baseUrl = &quot;https://dev.azure.com/$orgName/$projectName&quot;
  10. # Set the date range for the query
  11. $startDate = (Get-Date).AddDays(-14).ToString(&quot;yyyy-MM-ddTHH:mm:ssZ&quot;)
  12. $endDate = (Get-Date).ToString(&quot;yyyy-MM-ddTHH:mm:ssZ&quot;)
  13. # Create an Azure DevOps REST API query to retrieve the deployment results
  14. $queryUrl = &quot;$baseUrl/_apis/release/deployments?definitionId=$pipelineId&amp;$top=100&amp;$expand=All&amp;$filter=deploymentStatus eq &#39;succeeded&#39; or deploymentStatus eq &#39;failed&#39; and startedTime ge $startDate and startedTime le $endDate&quot;
  15. $headers = @{Authorization = &quot;Bearer $pat&quot;}
  16. $response = Invoke-RestMethod -Uri $queryUrl -Headers $headers -Method Get
  17. # Create a new Excel workbook and worksheet
  18. $excel = New-Object -ComObject Excel.Application
  19. $workbook = $excel.Workbooks.Add()
  20. $worksheet = $workbook.Worksheets.Add()
  21. # Write the deployment results to the Excel worksheet
  22. $row = 1
  23. $col = 1
  24. $worksheet.Cells.Item($row, $col) = &quot;Stage&quot;
  25. $worksheet.Cells.Item($row, $col+1) = &quot;Successful Count&quot;
  26. $worksheet.Cells.Item($row, $col+2) = &quot;Failed Count&quot;
  27. $row++
  28. foreach ($stageName in $stageNames) {
  29. $stageDeployments = $response.value | Where-Object {$_.releaseEnvironment.name -eq $stageName}
  30. $successfulCount = ($stageDeployments | Where-Object {$_.deploymentStatus -eq &quot;succeeded&quot;}).Count
  31. $failedCount = ($stageDeployments | Where-Object {$_.deploymentStatus -eq &quot;failed&quot;}).Count
  32. $worksheet.Cells.Item($row, $col) = $stageName
  33. $worksheet.Cells.Item($row, $col+1) = $successfulCount
  34. $worksheet.Cells.Item($row, $col+2) = $failedCount
  35. $row++
  36. }
  37. # Save the Excel workbook and close the Excel application
  38. $workbook.SaveAs(&quot;DeploymentResults.xlsx&quot;)
  39. $excel.Quit()

答案1

得分: 1

I am able to get the stage names of a release definition/pipeline, but still unable to write the successful and failed count of deployments from each stage for a week data onto an excel sheet.

  1. # 定义组织、项目和发布定义 ID 的变量
  2. $organization = "<org name>"
  3. $project = "<project name>"
  4. $releaseDefinitionId = "<release definition ID>"
  5. # 使用个人访问令牌 (PAT) 进行 Azure DevOps 鉴权
  6. $personalAccessToken = "<PAT>"
  7. $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
  8. $headers = @{Authorization = "Basic $base64AuthInfo"}
  9. # 通过 ID 获取发布定义
  10. $releaseDefinitionUrl = "https://vsrm.dev.azure.com/$($organization)/$($project)/_apis/release/definitions/$($releaseDefinitionId)?api-version=6.0"
  11. $releaseDefinition = Invoke-RestMethod -Uri $releaseDefinitionUrl -Headers $headers -Method Get
  12. # 从发布定义中获取阶段名称
  13. $stageNames = $releaseDefinition.environments.name
  14. # 输出阶段名称
  15. Write-Host "阶段名称:"
  16. $stageNames
  1. <details>
  2. <summary>英文:</summary>
  3. I am able to get the stage names of a release defination/pipeline, but still unable to write the succcessful and failed count of deployments from each stage for a week data onto excel sheet.

Define variables for the organization, project, and release definition ID

$organization = "<org name>"
$project = "<project name>"
$releaseDefinitionId = "<release defination ID>"

Authenticate with Azure DevOps using a Personal Access Token (PAT)

$personalAccessToken = "<PAT>"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
$headers = @{Authorization = "Basic $base64AuthInfo"}

Get the release definition by ID

$releaseDefinitionUrl = "https://vsrm.dev.azure.com/$($organization)/$($project)/_apis/release/definitions/$($releaseDefinitionId)?api-version=6.0"
$releaseDefinition = Invoke-RestMethod -Uri $releaseDefinitionUrl -Headers $headers -Method Get

Get the stage names from the release definition

$stageNames = $releaseDefinition.environments.name

Output the stage names

Write-Host "Stage names:"
$stageNames

huangapple
  • 本文由 发表于 2023年3月12日 17:06:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712066.html
匿名

发表评论

匿名网友

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

确定