英文:
Azure Devops release pipeline deployment count for each stage
问题
我已经编写了一个 PowerShell 脚本,用于从 Azure DevOps 发布管道中的每个发布阶段将成功和失败的部署计数写入 Excel 表格,为了获取过去2周的数据。我的脚本没有返回任何数据,请帮忙。
# 设置 Azure DevOps 组织和项目名称
$orgName = "<org-name>"
$projectName = "<project-name>"
# 设置 Azure DevOps 发布管道 ID 和阶段名称
$pipelineId = <pipeline-id>
$stageNames = @("<stage-name-1>", "<stage-name-2>", "<stage-name-3>")
# 设置 Azure DevOps PAT 和基本 URL
$pat = "<personal-access-token>"
$baseUrl = "https://dev.azure.com/$orgName/$projectName"
# 设置查询的日期范围
$startDate = (Get-Date).AddDays(-14).ToString("yyyy-MM-ddTHH:mm:ssZ")
$endDate = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ssZ")
# 创建 Azure DevOps REST API 查询以检索部署结果
$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"
$headers = @{Authorization = "Bearer $pat"}
$response = Invoke-RestMethod -Uri $queryUrl -Headers $headers -Method Get
# 创建一个新的 Excel 工作簿和工作表
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Add()
$worksheet = $workbook.Worksheets.Add()
# 将部署结果写入 Excel 工作表
$row = 1
$col = 1
$worksheet.Cells.Item($row, $col) = "阶段"
$worksheet.Cells.Item($row, $col+1) = "成功计数"
$worksheet.Cells.Item($row, $col+2) = "失败计数"
$row++
foreach ($stageName in $stageNames) {
$stageDeployments = $response.value | Where-Object {$_.releaseEnvironment.name -eq $stageName}
$successfulCount = ($stageDeployments | Where-Object {$_.deploymentStatus -eq "succeeded"}).Count
$failedCount = ($stageDeployments | Where-Object {$_.deploymentStatus -eq "failed"}).Count
$worksheet.Cells.Item($row, $col) = $stageName
$worksheet.Cells.Item($row, $col+1) = $successfulCount
$worksheet.Cells.Item($row, $col+2) = $failedCount
$row++
}
# 保存 Excel 工作簿并关闭 Excel 应用程序
$workbook.SaveAs("DeploymentResults.xlsx")
$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..
# Set the Azure DevOps organization and project name
$orgName = "<org-name>"
$projectName = "<project-name>"
# Set the Azure DevOps release pipeline ID and stage names
$pipelineId = <pipeline-id>
$stageNames = @("<stage-name-1>", "<stage-name-2>", "<stage-name-3>")
# Set the Azure DevOps PAT and base URL
$pat = "<personal-access-token>"
$baseUrl = "https://dev.azure.com/$orgName/$projectName"
# Set the date range for the query
$startDate = (Get-Date).AddDays(-14).ToString("yyyy-MM-ddTHH:mm:ssZ")
$endDate = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ssZ")
# Create an Azure DevOps REST API query to retrieve the deployment results
$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"
$headers = @{Authorization = "Bearer $pat"}
$response = Invoke-RestMethod -Uri $queryUrl -Headers $headers -Method Get
# Create a new Excel workbook and worksheet
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Add()
$worksheet = $workbook.Worksheets.Add()
# Write the deployment results to the Excel worksheet
$row = 1
$col = 1
$worksheet.Cells.Item($row, $col) = "Stage"
$worksheet.Cells.Item($row, $col+1) = "Successful Count"
$worksheet.Cells.Item($row, $col+2) = "Failed Count"
$row++
foreach ($stageName in $stageNames) {
$stageDeployments = $response.value | Where-Object {$_.releaseEnvironment.name -eq $stageName}
$successfulCount = ($stageDeployments | Where-Object {$_.deploymentStatus -eq "succeeded"}).Count
$failedCount = ($stageDeployments | Where-Object {$_.deploymentStatus -eq "failed"}).Count
$worksheet.Cells.Item($row, $col) = $stageName
$worksheet.Cells.Item($row, $col+1) = $successfulCount
$worksheet.Cells.Item($row, $col+2) = $failedCount
$row++
}
# Save the Excel workbook and close the Excel application
$workbook.SaveAs("DeploymentResults.xlsx")
$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.
# 定义组织、项目和发布定义 ID 的变量
$organization = "<org name>"
$project = "<project name>"
$releaseDefinitionId = "<release definition ID>"
# 使用个人访问令牌 (PAT) 进行 Azure DevOps 鉴权
$personalAccessToken = "<PAT>"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
$headers = @{Authorization = "Basic $base64AuthInfo"}
# 通过 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
# 从发布定义中获取阶段名称
$stageNames = $releaseDefinition.environments.name
# 输出阶段名称
Write-Host "阶段名称:"
$stageNames
<details>
<summary>英文:</summary>
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论