我正试图通过编程创建新的构建流水线。我正在寻找Azure DevOps REST API来执行此操作,但我没有找到使用REST API创建流水线的合适示例。请给予建议。
POST https://dev.azure.com/{organization}/{project}/_apis/build/definitions?api-version=5.0
GET https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=5.0
Definitions - Create
来创建一个新管道。不,如果您想使用REST API创建一个YAML类型的构建定义,它将不会在我们的代码库中创建相应的YAML文件,因为此API“Definitions - Create”仅将请求主体发送到流水线而不操作我们的代码库。目前,它支持创建链接到Git存储库中YAML文件的构建定义。如果您想自动创建yaml,可以查看REST API Pushes - Create。创建构建定义后,它会像 Azure Repos 一样创建 azure-pipelines.yml 文件并存储在存储库中吗?
我知道这个问题很老了,但我想分享一下我在2023年使用PowerShell自动将DevOps从本地迁移到Azure DevOps(云)时是如何做的。我们的代码库中有基于yaml的构建文件(azure-pipelines.yml
)。
我发现创建基于yaml的构建(流水线)所需提交的正文应该是以下JSON:
{
"configuration": {
"path": "azure-pipelines.yml",
"repository": {
"id": "YourRepoGUID",
"type": "azureReposGit"
},
"type": "yaml"
},
"name": "WHATEVER_YOU_WANT_HERE"
}
我正在使用VSTeam PS Module来帮助在PowerShell中很好地封装API调用。
首先,我们使用Set-VSTeamAccount
、Set-VSTeamApiVersion
和Set-VSTeamDefaultProject
设置团队、API和项目。我使用Get-VSTeamGitRepository
获取存储库ID,更新JSON片段中的名称和存储库ID,然后使用InvokeVSTeamRequest
提交一个POST请求来创建。我找不到Add-VSTeamPipeline,也无法让Add-VSTeamBuildDefinition工作,而且它需要一个文件。我已经为经典构建(基于GUI/非YAML)使用了Add-VSTeamBuildDefinition,所以我知道它对这些构建有效。
Set-VSTeamAccount <your settings>
Set-VSTeamApiVersion AzD2019U1
Set-VSTeamDefaultProject <your project>
$repoId = Get-VSTeamGitRepository -Name <yourreponame> | select -ExpandProperty id
$buildJson = '{"configuration": {"path": "azure-pipelines.yml","repository": {"id": "","type": "azureReposGit"},"type": "yaml"},"name": ""}'
$jsonObj = ConvertFrom-Json $buildJson
$jsonObj.configuration.repository.id = $repoId
$jsonObj.name = "<YourNamingConvention>"
$jsonRaw = $jsonObj | ConvertTo-Json -Compress
$buildDefMeta = Invoke-VSTeamRequest -resource pipelines -version '7.0' -method Post -body $jsonRaw -JSON | ConvertFrom-Json
azure-pipelines.yml
文件并存储在代码库中? - Kumaresh Babu N S