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
I could think of two issue here:-
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.
You need to pass instanceReference.name as well. Here it should be build name.
With TFS17, #2 above should be optional.