This post is out of date with the latest update. See the new post
here.
The goal of this post is to trigger a release in VSTS using PowerShell from an external build system. For this example, you will be using Jenkins. I assume you have a working Jenkins installation with a Project that you wish to deploy using Release Management in VSTS. I further assume that your Project includes the Archive the artifacts build step. You will need to make sure you add the PowerShell and Post-Build Script Plug-ins in Jenkins.
Begin by creating a Service Endpoint in VSTS to connect to your Jenkins installation.
- Log in to VSTS
- Click the Manage Project gear icon in the upper right-hand corner
- Click the Services Tab
- From the New Service Endpoint dropdown, select Jenkins
- Enter any name you like for the Connection Name
- Enter the Server URL, Username, and Password for your Jenkins installation
- Click OK
Next we will create the Release we are going to want to trigger from Jenkins.
- From VSTS click Release
- Click the Create Release Definition plus button
- Select Empty template
- Click OK
- Enter a name for your release
- Click the Link to a build definition link
- Select Jenkins in the Type dropdown
- Select the desired Project from the Source (Job) dropdown
- Click Link
- Save your release
Once your release is saved, you can locate the release definition id from the address bar.
We will use this number to make a REST API call to gain access to the other ids we need to trigger a release. Using the definition id, access this URL:
https://<account>.vsrm.visualstudio.com/DefaultCollection/<project>/_apis/ReleaseManagement/definitions/<ID>
You will need to locate the environment id and the artifact id.
Note: It was just a coincidence that all my ids were the same.
Before we leave VSTS, let’s create a Personal Access Token which we will use to authenticate during our REST calls.
- Click your name in the upper right hand corner
- Click My Profile
- Click the Security tab
- Click Add
- Enter appropriate values
- Click Create Token
- Copy the token value and store it somewhere safe
Note: You will not be able to display that value again.
Attached to this post is a PowerShell script you will need to check in to the root of your code repository used in your Jenkins Project.
You can now go to Jenkins and update your project with a post build step to call this PowerShell. Configure your project and add the Execute a set of scripts post-build action. Under the Build steps section select Windows PowerShell. Enter the following code into the Command text box:
$EnvBUILD_NUMBER = 'BUILD_NUMBER'
$build_number = (get-item env:$EnvBUILD_NUMBER).Value
invoke-expression ".\Trigger.ps1 -domain <vstsDomain> -teamProject <vstsTeamProject> -releaseDefinitionId <releaseDefinitionId> -targetEnvironmentId <targetEnvironmentId> -userName <vstsUsername> -password <AlternateCredentialsPassword or Access Token> -artifactId <artifactId> -buildNumber $build_number"
You can also provide -collection if your project is not in the DefaultCollection. Now simply build your Jenkins project. Once the post build action executes, you can see the release in Release Management.