使用Powershell构建管道 [英] Build Pipeline using powershell

查看:72
本文介绍了使用Powershell构建管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用Azure DevOps和PowerShell.我需要使用PowerShell通过API构建一个Azure管道.我做了一些事情来列出我组织中的项目.请帮助我以类似的方式使用PowerShell通过API来构建管道.

I am currently working in azure DevOps and PowerShell. I need to build an azure pipeline through API using PowerShell. I have done something to list my projects in my organization. Kindly help me to build a pipeline through API similarly using PowerShell.

         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
   }

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

I have my output listing my projects while running this code.

推荐答案

如果您要通过构建管道"来为现有管道排队构建",则

If you mean "queue a build" for an existing pipeline by "build a pipeline",

您可以在下面队列构建api 将您的管道排队运行.

you can call below queue build api to queue your pipeline to run.

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

下面是一个简单的Powershell示例,用于排队构建.

Below is a simple powershell example to queue a build.

$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

如果您的意思是通过构建管道"来创建新管道".您可以检查 此处是通过powershell脚本创建构建定义的示例.

Here is an example to create build definition via powershell scripts.

但是,建议从azure devops门户创建它们,因为

However it is suggested to create them from azure devops portal, as this thread pointed out the reason.

您可以检查自定义管道.您可能需要自定义管道的一些概念性主题,例如任务,选中Microsoft提供的学习教程.

You can check this quickstart to get started with YAML pipelines and then customize your pipeline . There are some conceptual topics that you may need to customize your pipeline such as variables and tasks, check here for more concepts. You can also follow this learning tutorial provided by Microsoft.

这篇关于使用Powershell构建管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆