SHARE
TWEET

Start-ReleaseManagerBuild

RJSN Nov 23rd, 2016 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.         .NOTES
  3.         #------------------------------------------------------------------------------------------------------------
  4.         # Date          : 08-01-2016
  5.         # Script name       : Start-ReleaseManagerBuild.ps1
  6.         # Description       : Starts a build from a TFS build.
  7.         #
  8.         # Created by        : Ralph Jansen
  9.         # Extra module      :
  10.         # Copyright         : ©2015 all rights reserved.
  11.         # History           : RJA 08-01-2016 Initial version
  12.         #
  13.         #------------------------------------------------------------------------------------------------------------
  14.        
  15.         .DESCRIPTION
  16.         Starts a build from a TFS build.
  17.        
  18. #>
  19.  
  20. param(
  21.     [string]$releasetemplatename = $Args[0],
  22.     [string]$releasename = $Args[1],  
  23.     [string]$targetstagename = $Args[2])  
  24.  
  25. cls
  26.  
  27. $username = "<domain>\<username>"
  28. $password = "<password>"
  29. $secstr = New-Object -TypeName System.Security.SecureString
  30. $password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
  31. $credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
  32.  
  33.  
  34. $rmServer = "tfs"
  35.  
  36. $builddefinition = [System.Uri]::EscapeDataString($env:TF_BUILD_BUILDDEFINITIONNAME)
  37. $build = [System.Uri]::EscapeDataString($env:TF_BUILD_BUILDNUMBER)
  38.  
  39. $baseEndpointUrl = "http://$($rmServer):1000/account/releaseManagementService/_apis/releaseManagement"
  40. $configurationServiceUrl = "$baseEndpointUrl/ConfigurationService"
  41. $releaseDefinitionServiceUrl = "$baseEndpointUrl/ReleaseDefinitionService"
  42. $orchestratorServiceUrl = "$baseEndpointUrl/OrchestratorService"
  43.  
  44. $escapedUsername = [System.Uri]::EscapeDataString($credentials.UserName)
  45. $responseXml = Invoke-RestMethod -Method Post -Credential $credentials -Uri "$configurationServiceUrl/GetUserByUserName?userName=$escapedUsername&api-version=3.0"
  46. $userId = $responseXml.Result.User.Id
  47.  
  48.  
  49. $body = "<Filter StatusId='2' IsDeleted='0' UserId='$userId' />"
  50. $responseXml = Invoke-RestMethod -Method Post -Credential $credentials -Uri "$releaseDefinitionServiceUrl/ListReleaseDefinitions?api-version=3.0" -Body $body
  51. $applicationVersionElt = $responseXml.ApplicationVersionList.ApplicationVersion |  
  52. Where-Object { $_.Name -eq "$releaseTemplateName" }
  53. $applicationVersionId = $applicationVersionElt.Id
  54. $releasePathId = $applicationVersionElt.ReleasePathId
  55.  
  56.  
  57. $body = "<Filter ApplicationVersionId='$applicationVersionId' LockRequested='1' LockRequestedById='$userId' />"
  58. $responseXml = Invoke-RestMethod -Method Post -Credential $credentials -Uri "$configurationServiceUrl/GetApplicationVersion?api-version=3.0" -Body $body
  59. $componentNames = $responseXml.ApplicationVersion.Components.Component.Name
  60.  
  61.  
  62. $responseXml = Invoke-RestMethod -Method Post -Credential $credentials -Uri "$configurationServiceUrl/GetReleasePath?id=$releasePathId&api-version=3.0"
  63. $targetStageElt = $responseXml.ReleasePath.Stages.Stage |  
  64. Where-Object { $_.StageTypeName -eq $targetStageName }
  65. $targetStageId = $targetStageElt.Id
  66.  
  67.  
  68. if ("$releaseName" -eq $null) {$releaseName = "Release: $([DateTime]::Now.ToString('G'))"}
  69.  
  70.  
  71. $genericProperties = @(
  72.  
  73.  """ReleaseName"":""$releaseName""",
  74.  
  75.  """ReleaseBuild"":null",
  76.  
  77.  """ReleaseBuildChangeset"":null",
  78.  
  79.  """TargetStageId"":""$targetStageId"""
  80.  
  81. )
  82.  
  83. $componentProperties = ($componentNames | ForEach-Object { """$($_):Build"":""$build""" })
  84. $json = "{" + ($genericProperties + $componentProperties -join ",`n") + "}"
  85.  
  86. $escapedReleaseTemplateName = [System.Uri]::EscapeDataString($releaseTemplateName)
  87. $propertyBag = [System.Uri]::EscapeDataString($json)
  88. $releaseId = Invoke-RestMethod -Method Post -Credential $credentials -Uri "$orchestratorServiceUrl/InitiateRelease?releaseTemplateName=$escapedReleaseTemplateName&deploymentPropertyBag=$propertyBag&api-version=3.0"
  89.  
  90.  
  91. $statusId = Invoke-RestMethod -Credential $credentials -Uri "$orchestratorServiceUrl/ReleaseStatus?releaseId=$releaseId"
  92. $statusMapping = @("?", "NotStarted", "InProgress", "Released", "Stopped", "Rejected", "Abandoned" )
  93. $statusMapping[$statusId]
RAW Paste Data
Challenge yourself this year...
Learn something new in 2017
Top