【问题标题】:Build Pipeline using powershell使用 powershell 构建管道
【发布时间】:2023-03-17 21:12:02
【问题描述】:

我目前在 Azure DevOps 和 PowerShell 工作。我需要使用 PowerShell 通过 API 构建一个 azure 管道。我已经做了一些事情来列出我组织中的项目。请帮助我使用 PowerShell 类似地通过 API 构建管道。

         function GetUrl() {
param(
    [string]$orgUrl, 
    [hashtable]$header, 
    [string]$AreaId
)

# Build the URL for calling the org-level Resource Areas REST API for the RM APIs
$orgResourceAreasUrl = [string]::Format("{0}/_apis/resourceAreas/{1}?api-preview=5.0-preview.1", 
$orgUrl, $AreaId)

# Do a GET on this URL (this returns an object with a "locationUrl" field)
$results = Invoke-RestMethod -Uri $orgResourceAreasUrl -Headers $header

# The "locationUrl" field reflects the correct base URL for RM REST API calls
if ("null" -eq $results) {
    $areaUrl = $orgUrl
}
else {
    $areaUrl = $results.locationUrl
}

return $areaUrl
}


 $orgUrl = "https://dev.azure.com/<my organization>/"

 $personalToken = "<my path>"
 Write-Host "Initialize authentication context" -ForegroundColor Yellow
 $token = 
  [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
   $header = @{authorization = "Basic $token"}

   $coreAreaId = "79134c72-4a58-4b42-976c-04e7115f32bf"
   $tfsBaseUrl = GetUrl -orgUrl $orgUrl -header $header -AreaId $coreAreaId

   $projectsUrl = "$($tfsBaseUrl)_apis/projects?api-version=5.0"

   $projects = Invoke-RestMethod -Uri $projectsUrl -Method Get -ContentType "application/json" - 
   Headers $header

   $projects.value | ForEach-Object {
   Write-Host $_.name
   }

在运行此代码时,我的输出列出了我的项目。

【问题讨论】:

    标签: powershell azure-pipelines


    【解决方案1】:

    如果您的意思是通过“构建管道”为现有管道“排队构建”,

    您可以拨打下面的电话queue build api 来排队您的管道以运行。

    POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.1

    下面是一个简单的 powershell 示例,用于对构建进行排队。

    $body = '
    { 
            "definition": {
                "id": number
            } 
    }
    '
    $token =   [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
    $header = @{authorization = "Basic $token"}
    
    
    $Uri = "https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.1"
    $buildresponse = Invoke-RestMethod -Method Post -ContentType "application/json" -Uri $Uri -Body $body -Headers $header
    write-host $buildresponse
    

    如果您的意思是通过“构建管道”来“创建新管道”。您可以查看build definition create Api

    Here 是通过 powershell 脚本创建构建定义的示例。

    但是建议从 azure devops 门户创建它们,因为 thread 指出了原因。

    您可以查看此 quickstart 以开始使用 YAML 管道,然后查看 customize your pipeline 。您可能需要一些概念性主题来自定义您的管道,例如variablestasks,查看here 了解更多概念。也可以关注微软提供的这个learning tutorial

    【讨论】:

    • 这有助于通过 API 对我的管道进行排队。谢谢
    【解决方案2】:

    我不确定您在这里所说的管道是什么意思。您可以使用MS Documentation 站点中提供的任何 API。

    对于您可以使用的项目:

    function get_projects {
        do
        {
            $uri="https://dev.azure.com/$Org/_apis/projects?continuationToken=$ContinuationToken&api-version=5.1"
            $ProjSets=Invoke-WebRequest -Uri $Uri -Method Get -ContentType "application/json" -Headers $header
            $continuationToken = $ProjSets.Headers.'x-ms-continuationtoken'
            $ProjectSet=$projset.content | ConvertFrom-Json
            $projects+=$ProjectSet.value
            }while ($continuationToken)
            write-host "$continuationToken" -ForegroundColor Cyan
            $projects.name
            $projects.count
    
    }
    
    get_projects
    

    要获取经过身份验证的用户有权访问的组织中的所有项目,您可以使用以下命令:

    GET https://dev.azure.com/{organization}/_apis/projects?api-version=5.1
    

    或者这个加上额外的参数:

    GET https://dev.azure.com/{organization}/_apis/projects?stateFilter={stateFilter}&$top={$top}&$skip={$skip}&continuationToken={continuationToken}&getDefaultTeamImageUrl={getDefaultTeamImageUrl}&api-version=5.1
    

    您可以使用Invoke-RestMethodInvoke-WebRequest 来点击这些网址并获得结果。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 2019-08-22
      • 2021-04-20
      • 1970-01-01
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多