Tuesday 24 October 2017

Trigger Release from PS in TFS 2015.2

Can you trigger a release in TFS 2015.2 from another release definition execution? Was the request from one of the teams. To fulfill this requirement, if you are using VSTS or TFS2017,  you can use the extension “VSTS Trigger” available in marketplace. But this is not usable in TFS 2015.2 as it does not have the Personal Access Token feature. The PowerShell script described in this post will let you trigger a release from another release.

Parameters of the script

  • User = should be a user added to team project “Release Administrators” security group and should have permission to view project level information.
  • Password = Password of the user
  • collectionUri = Team foundation server collection url
  • TeamProject = Team Project Name
  • ReleaseDefId = Release Definition Id
  • TriggerDescription = Description for triggering release
  • ArtifactAlias = Alias of the artifact
  • ArtifactId = Id of the artifact to use

The script (available here to download)

param(
    [Parameter(Mandatory=$true)]
    [string] $User,
    [Parameter(Mandatory=$true)]
    [string] $Password,
    [Parameter(Mandatory=$true)]
    [string] $collectionUri,
    [Parameter(Mandatory=$true)]
    [string] $TeamProject,
    [Parameter(Mandatory=$true)]
    [string] $ReleaseDefId,
    [Parameter(Mandatory=$true)]
    [string] $TriggerDescription,
    [Parameter(Mandatory=$true)]
    [string] $ArtifactAlias,
    [Parameter(Mandatory=$true)]
    [string] $ArtifactId
)


$securePassword = $Password | ConvertTo-SecureString -AsPlainText -Force   
$credential = New-Object System.Management.Automation.PSCredential($User, $securePassword)       


$Uri = $collectionUri +'/' + $TeamProject + '/_apis/Release/releases?api-version=2.0-preview'

$ReleaseMetadata = '{"definitionId": ' + $ReleaseDefId + ',"description": "' + $TriggerDescription + '","artifacts": [{"alias": "' + $ArtifactAlias + '","instanceReference": {"id": "' + $ArtifactId + '","name": "'+ $ArtifactAlias + '_' + $ArtifactId + '"}}],"isDraft": false,"reason": "none","manualEnvironments": null}';

$ReleaseResponse = Invoke-RestMethod -Method Post -Credential $credential -ContentType application/json -Uri $Uri -Body $ReleaseMetadata

$ReleaseResponse


You can execute this script with PowerShell as shown in below example.

.\TriggerRelease.ps1 -User 'yourdoamin\username' -Password 'userpassword' -collectionUri 'http://yourtfs:8080/tfs/yourcollection' -TeamProject 'yourteamprojectname' -ReleaseDefId 'releasedefinitionid' -TriggerDescription 'Creating using PS' -ArtifactAlias 'buildaliasusedinrelease' -ArtifactId 'buildid' -Verbose

This would trigger a release in TFS 2015.2

image

You can use the same script in another release dentition as inline PowerShell script or as a PowerShell task after making the script here available in the source control repository. This script can be further enhanced with TFS REST API to obtain required build names and IDs etc. with the required logic.

No comments:

Popular Posts