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

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

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 = &quot;&lt;org-name&gt;&quot;
$projectName = &quot;&lt;project-name&gt;&quot;

# Set the Azure DevOps release pipeline ID and stage names
$pipelineId = &lt;pipeline-id&gt;                       
$stageNames = @(&quot;&lt;stage-name-1&gt;&quot;, &quot;&lt;stage-name-2&gt;&quot;, &quot;&lt;stage-name-3&gt;&quot;)

# Set the Azure DevOps PAT and base URL
$pat = &quot;&lt;personal-access-token&gt;&quot;
$baseUrl = &quot;https://dev.azure.com/$orgName/$projectName&quot;

# Set the date range for the query
$startDate = (Get-Date).AddDays(-14).ToString(&quot;yyyy-MM-ddTHH:mm:ssZ&quot;)
$endDate = (Get-Date).ToString(&quot;yyyy-MM-ddTHH:mm:ssZ&quot;)

# Create an Azure DevOps REST API query to retrieve the deployment results
$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;
$headers = @{Authorization = &quot;Bearer $pat&quot;}

$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) = &quot;Stage&quot;
$worksheet.Cells.Item($row, $col+1) = &quot;Successful Count&quot;
$worksheet.Cells.Item($row, $col+2) = &quot;Failed Count&quot;
$row++

foreach ($stageName in $stageNames) {
    $stageDeployments = $response.value | Where-Object {$_.releaseEnvironment.name -eq $stageName}
    $successfulCount = ($stageDeployments | Where-Object {$_.deploymentStatus -eq &quot;succeeded&quot;}).Count
    $failedCount = ($stageDeployments | Where-Object {$_.deploymentStatus -eq &quot;failed&quot;}).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(&quot;DeploymentResults.xlsx&quot;)
$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

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:

确定