TFS API Create Release with powershell

1.4k views Asked by At

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
2

There are 2 answers

2
chandan kumar anjani On

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.

0
Larry Steele On

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"} } ] }