Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .NOTES
- #------------------------------------------------------------------------------------------------------------
- # Date : 23-11-2016
- # Script name : Start-ReleaseManagerBuild.ps1
- # Description : Starts a build from a TFS build for Release Management 2015 (U3)
- #
- # Created by : Ralph Jansen
- # Extra module :
- # Copyright : ©2015 All rights reserved.
- # History : RJA 08-01-2016 Initial version
- #
- #------------------------------------------------------------------------------------------------------------
- .DESCRIPTION
- Starts a build from a TFS build.
- #>
- param(
- [string]$releasetemplatename = $Args[0],
- [string]$releasename = $Args[1],
- [string]$targetstagename = $Args[2])
- cls
- $username = "<account>"
- $password = "<password>"
- $secstr = New-Object -TypeName System.Security.SecureString
- $password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
- $credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
- $rmServer = "<servername>"
- $builddefinition = [System.Uri]::EscapeDataString($env:TF_BUILD_BUILDDEFINITIONNAME)
- $build = [System.Uri]::EscapeDataString($env:TF_BUILD_BUILDNUMBER)
- $baseEndpointUrl = "http://$($rmServer):1000/account/releaseManagementService/_apis/releaseManagement"
- $configurationServiceUrl = "$baseEndpointUrl/ConfigurationService"
- $releaseDefinitionServiceUrl = "$baseEndpointUrl/ReleaseDefinitionService"
- $orchestratorServiceUrl = "$baseEndpointUrl/OrchestratorService"
- $escapedUsername = [System.Uri]::EscapeDataString($credentials.UserName)
- $responseXml = Invoke-RestMethod -Method Post -Credential $credentials -Uri "$configurationServiceUrl/GetUserByUserName?userName=$escapedUsername&api-version=3.0"
- $userId = $responseXml.Result.User.Id
- $body = "<Filter StatusId='2' IsDeleted='0' UserId='$userId' />"
- $responseXml = Invoke-RestMethod -Method Post -Credential $credentials -Uri "$releaseDefinitionServiceUrl/ListReleaseDefinitions?api-version=7.0" -Body $body
- $applicationVersionElt = $responseXml.ApplicationVersionList.ApplicationVersion |
- Where-Object { $_.Name -eq "$releaseTemplateName" }
- $applicationVersionId = $applicationVersionElt.Id
- $releasePathId = $applicationVersionElt.ReleasePathId
- $body = "<Filter ApplicationVersionId='$applicationVersionId' LockRequested='1' LockRequestedById='$userId' />"
- $responseXml = Invoke-RestMethod -Method Post -Credential $credentials -Uri "$configurationServiceUrl/GetApplicationVersion?api-version=7.0" -Body $body
- $componentNames = $responseXml.ApplicationVersion.Components.Component.Name
- $responseXml = Invoke-RestMethod -Method Post -Credential $credentials -Uri "$configurationServiceUrl/GetReleasePath?id=$releasePathId&api-version=7.0"
- $targetStageElt = $responseXml.ReleasePath.Stages.Stage |
- Where-Object { $_.StageTypeName -eq $targetStageName }
- $targetStageId = $targetStageElt.Id
- if ("$releaseName" -eq $null) {$releaseName = "Release: $([DateTime]::Now.ToString('G'))"}
- $genericProperties = @(
- """ReleaseName"":""$releaseName""",
- """ReleaseBuild"":null",
- """ReleaseBuildChangeset"":null",
- """TargetStageId"":""$targetStageId"""
- )
- $componentProperties = ($componentNames | ForEach-Object { """$($_):Build"":""$build""" })
- $json = "{" + ($genericProperties + $componentProperties -join ",`n") + "}"
- $escapedReleaseTemplateName = [System.Uri]::EscapeDataString($releaseTemplateName)
- $propertyBag = [System.Uri]::EscapeDataString($json)
- $releaseId = Invoke-RestMethod -Method Post -Credential $credentials -Uri "$orchestratorServiceUrl/InitiateRelease?releaseTemplateName=$escapedReleaseTemplateName&deploymentPropertyBag=$propertyBag&api-version=3.0"
- $statusId = Invoke-RestMethod -Credential $credentials -Uri "$orchestratorServiceUrl/ReleaseStatus?releaseId=$releaseId"
- $statusMapping = @("?", "NotStarted", "InProgress", "Released", "Stopped", "Rejected", "Abandoned" )
- $statusMapping[$statusId]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement