So you want to trigger a VSTS release from an external build system

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.

Comments (9) -

  • Anvesh Kasani

    2/12/2016 12:32:43 PM | Reply

    trigger a VSTS release from Jenkins build it is able to trigger the release but it not able to pick the artifacts in the release which i configure

  • DI

    2/23/2016 6:17:27 AM | Reply

    Hi Donovan,

    We use release management in TFS 2015 update 2 RC1. I'm testing manually trigger release with a linked jenkins artifact. I can select jenkins job artifact and manually create a release in TFS. But release always fails with a "Input string was not in correct format" error at the "Download artifacts" step. The jenkins job has archive artifacts steps.

    2016-02-23T06:07:09.4972820Z Starting artifacts download...
    2016-02-23T06:07:09.5129098Z Error: System.FormatException: Input string was not in a correct format.
    2016-02-23T06:07:09.5129098Z    at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
    2016-02-23T06:07:09.5129098Z    at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
    2016-02-23T06:07:09.5129098Z    at Microsoft.TeamFoundation.DistributedTask.Plugin.Release.AgentArtifactDefinitionExtensions.GetJenkinsArtifactDetails(AgentArtifactDefinition agentArtifactDefinition, IEndpointService endpointService, IVariableService variableService, String teamProjectId, IDictionary`2 savedSettings)...

    The linked artifact shows in the release summary is: JenkinTfsReleaseTest / JenkinTfsReleaseTest #5 (Jenkins)

    Have you seen this error before? Do you know what might have caused it?

    Thanks,
    Di

    • Donovan

      2/23/2016 7:59:21 AM | Reply

      That just happened in the service as well. I think we fixed it in VSTS but it will have to be corrected in next release to TFS.  

  • Di

    2/26/2016 5:51:44 PM | Reply

    Thanks, Donovan! Will the fix make it to TFS update 2 RC2?

    Di

  • Di

    3/14/2016 11:39:48 PM | Reply

    Hi Donovan,

    We updated to RC2. Now I'm getting a different error, the "Deploy" step failed right away after selecting a version from the Artifacts version drop-down and manually triggering the release.

    The error message is quite confusing. "  
    VS402925: BuildId '2016-03-14_16-28-12' is not valid. Make sure that the build id is valid and try again." I don't know where it is getting that BuildId, our Jenkins job # is just a number. Do you have idea that what might be missing from my set up?

    Thanks,
    Di

  • Chinal Patel

    11/8/2016 2:17:36 AM | Reply

    I am trying to give build trigger for Jenkins from vsts. I want to run my UFT script job from Jenkins once vsts successfully releases. But somehow cannot connect Jenkins to vsts n vice versa.
    Is there any detail steps to follow or any suggestions please?

Add comment

Loading