Enable Full CI/CD with Jenkins and VSTS

 

There is a much easier way to do this now. You can read how here Setting up CI/CD with the TFS Plugin for Jenkins

 

In a previous post I showed how you could trigger a release from Jenkins using the Release Management REST API.  Since that post, the REST for Release Management as changed so I wanted to write and update.  Instead of just duplicating that post I decided to take it a step further and have the Jenkins build automatically trigger when I commit code to Visual Studio Team Services (VSTS).  This would give us a full CI/CD pipeline with Jenkins and VSTS. In this post I will show you the pieces needed to enable full CI/CD using Jenkins and VSTS.

Before I begin I will state some assumptions:

  1. You have a working Jenkins installation with the following plugins:
    • Credentials
    • Get client
    • Git
    • Post-Build Script
    • PowerShell
  2. You have a VSTS account
  3. Your code will be in VSTS Git repository
  4. The attached PowerShell file is in the root of your repository

We are going to begin and end this post in Jenkins.  Step one is to create an empty Freestyle project in Jenkins to wire up to VSTS.

  1. Log into Jenkins
  2. Click New Item
    Field Name Value
    Item name Jenkins CI-CD
  3. Select Freestyle project
  4. Click OK

You can leave this tab open and perform the next steps in a new tab of your browser.  We just needed a Jenkins build to configure VSTS against. We will return to this tab to complete the build at the end of the post.

To connect Jenkins to VSTS so it can clone the Git repository and authenticate during our REST calls, we are going to need to create a Personal Access Token.

  1. Log into VSTS
  2. Click your name in the upper right-hand corner
    image_thumb[15]
  3. Click My security
  4. Click Add
  5. Enter appropriate values
  6. Click Create Token
  7. Copy the token value and store it somewhere safe
    image
Now create a Service Endpoint in VSTS to connect to your Jenkins installation.
  1. Log into VSTS
  2. Navigate to a Team Project
  3. Click the Manage Project gear icon in the upper right-hand corner
    image
  4. Click the Services tab
  5. Click New Service Endpoint
  6. Select Jenkins
    Field Name Value
    Connection Name Jenkins
    Server URL {your full Jenkins URL}
    Username {Jenkins user name}
    Password {Jenkins password}
  7. Click OK
Next we will create the release we are going to want to trigger from Jenkins.
  1. Log into VSTS
  2. Navigate to a Team Project
  3. Click Release
  4. Click the Create Release Definition plus button
  5. Select Empty template
  6. Click OK
  7. Enter a name for your definition
  8. Click the Link to a build definition link
  9. Select Jenkins in the Type dropdown
  10. Select the desired Project from the Source (Job) dropdown
  11. Click Link
  12. Click the ellipses of the Default Environment  
  13. Select Deployment conditions…
    image
  14. Select After release creation for Trigger
  15. Click OK
  16. Save your release

Once your release is saved, you can locate the release definition id from the address bar.

image

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://{instance}.vsrm.visualstudio.com/DefaultCollection/{project}/_apis/Release/definitions/{id}

You will need to locate the environment id and the artifact id.

image

Next we are going to connect our Jenkins and VSTS via a Service Hook. This will allow communication so Jenkins can be triggered from a commit to VSTS.

  1. Log into VSTS
  2. Navigate to a Team Project
  3. Click the Manage Project gear icon in the upper right-hand corner
    image
  4. Click Service Hooks
  5. Click the + button
  6. Select Jenkins
  7. Click Next
    Field Name Value
    Trigger on this type of event Code pushed
    Repository {your repository}
    Branch [Any]
    Pushed by a member of group [Any]
  8. Click Next
    Field Name Value
    Perform this action Trigger generic build
    Jenkins base URL {your full Jenkins URL}
    Username {Jenkins username}
    User API token (or password) {Jenkins password}
    Build {Build created above}
  9. Click Finish 
We will now update the Jenkins build.  We will only cover the parts needed to talk to VSTS.  I will leave the build steps to build your project for you to configure.
  1. Return to the Jenkins tab
  2. Field Name Value
    Source Code Management Git
    Repository URL https://{instance}.visualstudio.com/DefaultCollection/{project}/_git/{repository}
  3. Click Add next to Credentials
    image
    Field Name Value
    Kind Username with password
    Scope Global (Jenkins, nodes, items, all child items, etc)
    Username {your vsts username}
    Password {personal access token}
    Description Jenkins Access Token from VSTS
  4. Click Add
  5. Select the new credentials
  6. Click Add post-build action
  7. Select Archive the artifacts
    Field Name Value
    Files to archive **/*
  8. Click Add post-build action
  9. Select Execute a set of scripts
  10. Click Add build step
  11. Select Windows PowerShell
    image
  12. Copy and paste the code below into the Command text box
    $EnvWORKSPACE = 'WORKSPACE'
    $EnvBUILD_NUMBER = 'BUILD_NUMBER'
    $workspace = (get-item env:$EnvWORKSPACE).Value
    $build_number = (get-item env:$EnvBUILD_NUMBER).Value
    invoke-expression ".\TriggerRelease.ps1 -TfsCollectionUri https://{instance}.vsrm.visualstudio.com/DefaultCollection -TeamProject {TeamProject} -ReleaseDefinitionId {ReleaseDefinitionId} -TargetEnvironmentId {TargetEnvironmentId} -BuildNumber $build_number -artifactAlias '{artifactAlias}' -artifactId {artifactId} -personalAccessToken {personalAccessToken}"
  13. Update the Instance, Team Project, Release Definition Id, Target Environment Id, Artifact Alias, Artifact Id, and Personal Access Token
  14. Click Save

Now all you have to do is commit a change to VSTS and Jenkins will build the code and trigger a release. When you do, the Service Hook we created will inform Jenkins, who will start a build.

image

Jenkins will pull the code from the VSTS Git repository using the Personal Access Token we created and build your project.  Finally Jenkins will use the PowerShell script to call the Release Management REST API to trigger the release.

image

TriggerReleaseV2.ps1 (3.8KB)

Comments (6) -

  • Hi,
    I am able to trigger the release but it failed the release .In below log at BuildId it is give value 1 but my jenkins Latest BuildId is 15 using the arguments $build_number it is give value 15 only but in below log how its taking the buildid 1 i not getting it.

    2016-03-18T11:03:52.5225962Z Cleaning artifacts directory: C:\a\3fd575cd8…

    2016-03-18T11:03:52.5345954Z Cleaned artifacts directory: C:\a\3fd575cd8

    2016-03-18T11:03:52.5376211Z Number of linked artifacts to download: 1

    2016-03-18T11:03:52.5389487Z Starting artifacts download...

    2016-03-18T11:03:52.5569646Z Downloading linked artifact: NewHorizonsVSO...

    2016-03-18T11:03:52.5579653Z Downloading artifact of type: Jenkins

    2016-03-18T11:03:52.5591335Z Created artifact folder C:\a\3fd575cd8\NewHorizonsVSO

    2016-03-18T11:03:52.7071226Z Got the Jenkins Artifact Details

    2016-03-18T11:03:52.7080538Z Job Name: NewHorizonsVSO

    2016-03-18T11:03:52.7087889Z BuildId: 1
    • I got the solution in power shell script we are passing json format in that idEmbarassedartifactId should replace with idEmbarassedBuildNumber

      after changing this then my build successfully deployed.
  • Uma
    Can we do the steps mentioned in this article in TFS 2015 on premise?
    I tried but I could not found an option to create personal access token
    • TFS 2015 does not support PAT but 2017 does.
  • Our Jenkins implementation is on premise and not publicly accessible, although it can see out.  Will these steps still work for that type of configuration?

    BTW, love your straightforward writing style and exact steps.
    • I would try with the new TFS Plugin.  I have a post on how to use it.

Pingbacks and trackbacks (1)+

Add comment

Loading