簡體   English   中英

使用 powershell 構建流水線

[英]Build Pipeline using powershell

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

在運行此代碼時,我的輸出列出了我的項目。

如果您的意思是通過“構建管道”為現有管道“排隊構建”,

您可以調用下面的隊列構建 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

如果您的意思是“構建管道”是指“創建新管道”。 您可以檢查構建定義 create Api

是一個通過 powershell 腳本創建構建定義的示例。

但是,建議從 azure devops 門戶創建它們,因為該線程指出了原因。

您可以查看此快速入門以開始使用 YAML 管道,然后自定義您的管道 您可能需要一些概念性主題來自定義您的管道,例如變量任務,請在此處查看更多概念。 您也可以按照 Microsoft 提供的此學習教程進行操作。

我不確定您在這里所說的管道是什么意思。 您可以使用MS 文檔站點中提供的任何 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 並獲得結果。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM