Setting up CI/CD with the TFS Plugin for Jenkins

Today I am going to show you how to configure Jenkins with VSTS using the TFS Plugin for Jenkins. Using this plugin makes it very easy to use Jenkins with VSTS to create a full CI/CD pipeline.

I do not cover installing Jenkins or any details. I assume that if you are reading this you already know how to use Jenkins.  If you are new to CI/CD just use VSTS it requires no installation and you can build Java, Node.js, .Net out of the box. If you are already using Jenkins I will show you how to wire it to VSTS.

You are going to need the following

With the above we are ready to configure a project to pull from VSTS, build in Jenkins and deploy with VSTS.

  1. Create a Team Project
    image
  2. Now import a Git repo
    image
  3. Copy the Git repo URL
    image

With our VSTS Team project created and source imported we can switch over to Jenkins.

  1. Add a new project to Jenkins to build your code
  2. For Source Code Management select Git
  3. Copy the Git repo URL in the Repository URL
  4. Click Add then Jenkins next to the Credentials drop down
    image
  5. Enter the following
    Field Name Value
    Domain Global credentials (unrestricted)
    Kind Username with password
    Scope Global (Jenkins, nodes, items, all child items, etc)
    Username [Leave Blank]
    Password [VSTS Personal Access Token]
    ID VSTS
    Description VSTS
  6. Click Add
  7. Add a Archive the artifacts post-build action to archive output of build
  8. Save the project
    We will return to the project once we have the build firing so we can trigger the release

With the Jenkins project in place we can configure VSTS to start this build on every commit using a Service Hook.

  1. In VSTS add a new Service Hook
    image
  2. Click Create subscription
  3. Select Jenkins
  4. Click Next
  5. Select Code pushed for Trigger on this type of event
  6. Click Next
  7. Enter the following
    Field Name Value
    Jenkins base URL [URL to Jenkins Server]
    User name [Your user name]
    User API token (or password) [Your password]
    Build [Build you want to trigger]
    Integration level TFS plugin for Jenkins
    The the rest blank
  8. Click Test
    The build in Jenkins should start
    image
  9. Make sure the build succeeds
    image
  10. In VSTS click Close
  11. Click Finish
    image

With the build working we can create our release.

  1. Select Releases from the Build & Release menu
    image
  2. Click New definition
  3. Select Empty
  4. Click Next
  5. Select Jenkins and click Manage
    image
  6. Click New Service Endpoint and select Jenkins
    image
  7. Enter the following
    Field Name Value
    Connection name Jenkins
    Sever URL URL to your Jenkins Server
    Accept untrusted SSL certificates [Depends on your Jenkins configuration]
    Username [Jenkins user name]
    Password [Jenkins password]
  8. Click Verify connection
    SNAGHTMLe7c8912
  9. Click OK
  10. Return to the Create release definition dialog
    Click Build then Jenkins to force the new connection to show in dropdown
  11. Select Jenkins
  12. Select the project you want deploy
  13. Click Create
  14. Rename the release
    image
  15. Click Save
    Configuring your release depends on the type of project you are building. The goal of this post is to wire up the parts so when you commit a change Jenkins is used to build the code and start a release in VSTS. So, I will leave the release empty.

With a release created we can now return to Jenkins to have it trigger this release when the build completes.

  1. Configure your Jenkins project
  2. Add a Trigger release in TFS/Team Services Post-build Action
    image
  3. Enter the following
    Field Name Value
    Collection url https://[your VSTS].visualstudio.com
    Team Project [Name of project you created]
    Release definition  
    Username [Leave Blank]
    Password or PAT [Personal Access Token]
  4. Click Save
  5. Click Build Now

Once the build is complete a release well be started.
image

Using the TFS Plugin for Jenkins makes it very easy to setup CI/CD between VSTS and Jenkins.

Comments (19) -

  • sumedh

    3/22/2017 10:18:39 AM | Reply

    Great article.

    I am curious about how do I trigger next build using same artifacts that my current build used.
    In other words, I want to trigger my staging build using the same artifacts which my dev build used.

    I am able to trigger my staging build after dev build completes, but it (staging) uses the repository directly to get the latest changes. My problem here is if somebody merges the changes in-between these builds, there is a chance that staging might pick up the latest changes and not the changes with Dev was successful.

    Any help appreciated.
    TY

    • Donovan

      3/22/2017 11:28:41 AM | Reply

      Why are you building the same code again? Should build once and simply deploy same binaries to each environment.

  • Carlos Torrão

    3/25/2017 11:06:57 AM | Reply

    I don't know if it is only happening to me. But the trigger in Jenkins to VSTS obly works correctly when the Release Definition only have a Jenkins Artifact. If that definition has 2 or more artifacts (e.g. Git) it fails when triggering in Jenkins side.

    Except that, it is a great integration for autimation.

    • Donovan

      3/25/2017 12:42:40 PM | Reply

      Is your Jenkins artifact marked as primary?

      • Carlos Torrão

        3/26/2017 9:58:32 AM | Reply

        Yes. Jenkins artifact is marked as primary artifact.

  • Eldad

    3/27/2017 6:23:47 AM | Reply

    Hi,

    I am trying to set up with TFS 2015  via Jenkins with TFS plug in 5.2.
    I am getting an error :
    java.lang.NullPointerException on

    • Donovan

      3/27/2017 11:21:25 AM | Reply

      Are you using a private or hosted agent?

  • Eldad

    3/27/2017 1:43:58 PM | Reply

    Hi,

    TFS 2015

    Jenkins 1.655

    TFS Plug in 5.2.0

    I am setting up CI/CD with the TFS Plugin for Jenkins. I configured in my TFS 2015 the connection to my Jenkins Server , Connection was set up fine.

    But when I trigger in my job post build action using TFS plug in I am getting an Error:

    java.lang.NullPointerException

    tfs.rm.ReleaseManagementCI.CreateRlease(RleaseManagementCI.java:131 java.lang.Null

    Any suggestion how to tackle such issue ?

    Thanks In Advanced for your help.

    • Gopinath

      4/2/2017 3:32:23 AM | Reply

      @Eldad
      Can you please share your logs with rm_customer_queries@microsoft.com

  • Ivan

    4/20/2017 9:17:42 PM | Reply

    How can I add Jenkins in the list option of New Service Endpoints ?

    • Donovan

      4/21/2017 1:34:37 AM | Reply

      It should just be there. You don't have to install anything for it.

      • Ivan

        4/21/2017 6:55:21 PM | Reply

        For some reason I just can see options Azure, Chef, Generic, Github and SubVersion

  • Jo

    7/6/2018 4:54:32 AM | Reply

    Why do you use Jenkins instead of using TFS 2017?  Isn't it a CICD tool by itself?

    • Donovan

      7/10/2018 3:39:34 PM | Reply

      This was only to show customers that it is possible to use them together. I would never select Jenkins over VSTS or TFS. But many people already have an existing investment in Jenkins. If that is the case you don't have to throw all that away. You can use them both together.

  • SK

    4/19/2019 6:09:55 AM | Reply

    We are trying to setup server side TFS-Git hook so that when ever there is pre git push event happen we want to verify that Commit messages follow specific format.  (For example #<Jira Id> <String comment)

    I am not finding any straight forward solution for this.  (I dont know where is .git\<hooks> directory on server where i can copy my custom hook script and achieve this.

  • teja

    8/27/2019 4:40:41 PM | Reply

    Trying to do release from Jenkins to 2015 TFS, but after build is success the release has not happening. Below is the error. Please help me with this issue.

    java.lang.NullPointerException
      at hudson.plugins.tfs.rm.ReleaseManagementCI.CreateRelease(ReleaseManagementCI.java:206)
      at hudson.plugins.tfs.rm.ReleaseManagementCI.perform(ReleaseManagementCI.java:177)
      at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
      at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
      at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
      at hudson.model.Build$BuildExecution.post2(Build.java:186)
      at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
      at hudson.model.Run.execute(Run.java:1835)
      at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
      at hudson.model.ResourceController.execute(ResourceController.java:97)
      at hudson.model.Executor.run(Executor.java:429)
    Build step 'Trigger release in TFS/Team Services' marked build as failure
    Finished: FAILURE

  • aditya

    5/18/2020 6:33:42 AM | Reply

    Hi , I am using Jenkins for Build and TFS for Release Management.
    I am using VSTS as SCM
    Everytime ,I am building from Jenkins I get a Success and Release is Triggered , but I am getting the below error while Release is called
    2020-05-16T13:58:55.0173125Z ##[section]Starting: Download Artifacts
    2020-05-16T13:58:55.2423181Z Downloading artifact
    2020-05-16T13:58:55.2703219Z Creating artifacts directory: E:\TFSAgent\_work\r5\a
    2020-05-16T13:58:55.3093253Z Created artifacts directory: E:\TFSAgent\_work\r5\a
    2020-05-16T13:58:55.3203486Z Starting artifacts download...
    2020-05-16T13:58:55.3313203Z Downloading linked artifact PricingCICD of type Jenkins...
    2020-05-16T13:58:55.3463377Z Ensuring artifact folder E:\TFSAgent\_work\r5\a\PricingCICD exists and is clean.
    2020-05-16T13:58:55.3653408Z Received Jenkins Artifact Details
    2020-05-16T13:58:55.3663248Z Job Name: PricingCICD
    2020-05-16T13:58:55.3663248Z BuildId: 34
    2020-05-16T13:58:57.8274285Z Preparing to get artifacts info from Jenkins server
    2020-05-16T13:58:58.6814428Z An error occurred during download: Microsoft.VisualStudio.Services.Agent.Worker.Release.Artifacts.ArtifactDownloadException: Unexpected error occurred while downloading artifacts
       at Microsoft.VisualStudio.Services.Agent.Worker.Release.Artifacts.JenkinsArtifact.<DownloadAsync>d__15.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.VisualStudio.Services.Agent.Worker.Release.ReleaseJobExtension.<>c__DisplayClass34_2.<<DownloadArtifacts>b__2>d.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.VisualStudio.Services.Agent.Worker.Release.RetryExecutor.<ExecuteAsync>d__20.MoveNext()
    2020-05-16T13:58:58.7514444Z ##[error]Unexpected error occurred while downloading artifacts
    2020-05-16T13:58:58.7824441Z ##[section]Finishing: Download Artifacts

Pingbacks and trackbacks (1)+

Add comment

Loading