TFS API Create Release with powershell

折月煮酒 提交于 2019-12-23 20:11:38

问题


I'm trying to queue a build and create a release using powershell. I'm able to successfully queue a build but unfortunately, the Continuous Deployment is not being triggered for the release.

My hope is that I can do both of these in a powershell script that will allow me to release the application. I'm using TFS 2015 Update 3

I've been working from an article posted here: http://blog.nwcadence.com/vststfs-rest-api-the-basics-and-working-with-builds-and-releases/

In summary in performing the following:

  • Calling api to return a list of release
  • Performing a query of the list of releases and returning the id based on the release name
  • Setting the release definition id
  • Calling the api to return the specific release definition info
  • Setting the alias for the artifacts
  • Setting the artifacts id
  • Setting the ReleaseUri
  • Constructing the Json string for artifacts
  • Setting json with the combination of required info
  • Calling api to kick off the creation of a release passing in the json

My script:


$releaseDef = Invoke-RestMethod -Method Get -UseDefaultCredentials -Uri "$Uri/$defaultCollection/$TeamProject/_apis/release/definitions?api-version=2.2-preview.1"
$id = $releaseDef.value | Where-Object { $_.name -eq $releaseName} | select id
$releaseDefId = $id.id

$release = Invoke-RestMethod -Method Get -UseDefaultCredentials -ContentType "application/json" -Uri "$Uri/$defaultCollection/$TeamProject/_apis/release/definitions/$releaseDefId`?api-version=2.2-preview.1"

$alias = $release.artifacts.alias
$aliasId = $release.artifacts.id

$releaseUri = "$Uri/$defaultCollection/$TeamProject/_apis/release/releases?api-version=2.2-preview.1"

$jsonReleaseString = "{""alias"": ""$alias"", ""instanceReference"" : ""id"" : ""$aliasId""}}"

$jsonRelease = @"
{
    "definitionId": $releaseDefId,
    "description": $buildNbr,
    "artifacts": [
    $jsonReleaseString
    ]
}

$releaseResponse = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType "application/json" -Uri $releaseUri -Body $jsonRelease

All appears to be ok until I hit the last statement. The error I'm receiving is:


{"$id":"1","innerException":null,"message":"VS402903: The parameter with name releaseStartMetadata should be an ReleaseStartMetadata, but the specified value is not convertible to 
ReleaseStartMetadata","typeName":"System.InvalidOperationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089","typeKey":"InvalidOperationException","errorCode":0,"eventId":0}
At line:1 char:12
+ $release = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType "a ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

回答1:


I could think of two issue here:-

  1. In case your release definition is linked with some artifact then instanceReference.id should be the version id of the artifact. I meant it should be build number, if the linked artifact is build.

  2. You need to pass instanceReference.name as well. Here it should be build name.

With TFS17, #2 above should be optional.




回答2:


I found the answer. There were two issue. One required placing all values in quotes"". The other was related to the buildid and name as name is required for on prem but was using wrong id for artifacts. Here is my json:

{ "definitionId": 17, "description": "ContentManagement-Dev", "artifacts": [ {"alias": "ContentManagement-Dev", "instanceReference" : { "id": "12569", "name": "ContentManagement-Dev_1.0.0.12569"} } ] }



来源:https://stackoverflow.com/questions/41216277/tfs-api-create-release-with-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!