使用Powershell构建管道

时间:2020-01-21 07:34:06

标签: powershell azure-pipelines

我目前正在使用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
   }

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

2 个答案:

答案 0 :(得分: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脚本创建构建定义的示例。

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

您可以选中此quickstart以开始使用YAML管道,然后选择customize your pipeline。您可能需要定制一些概念性主题,例如variablestasks,请查看here了解更多概念。您还可以遵循Microsoft提供的learning tutorial

答案 1 :(得分:0)

我不确定您在此处所说的管道是什么意思。您可以使用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来访问这些URL并获得结果。

希望有帮助。