Using Jenkins to pull and build .Net applications from BitBucket

  1. Download and install the latest, native package, of the Jenkins Windows Installer from http://jenkins-ci.org (version used for this instruction: 1.595).
  2. Open up the Jenkins dashboard (localhost:8080).
  3. Go to “Manage Jenkins > Manage Plugins” and select the following plugins to install:
    • Git Client
    • Git Server
    • Git Plugin
    • BitBucket Plugin
    • MSBuild Plugin
  4. Install Git Extensions from http://sourceforge.net/projects/gitextensions – Use all defaults.
  5. Install Git Bash from http://git-scm.com/download/win – During the installation select “Use Git from the Windows command Prompt” on the “Adjusting your Path environment page”.
  6. I would suggest that you restart the machine at this point, this forces a restart of Jenkins (obviously). I suppose you could just restart Jenkins but hey….
  7. When Jenkins is installed on Windows, it runs the service a the local service account. This user will need to have .ssh setup to include the known_hosts and id_rsa files (See here on how to do this: https://gist.github.com/tehfoo/3708738#generate-rsa-key-pair-on-your-jenkins-server)
  8. Once these files have ben created, copy the entire .ssh directory to C:\Windows\SysWOW64\config\systemprofile. If you chose to run the service as a specific user, put the .ssh folder in the profile of that user (i.e. C:\Users\TheUser).
  9. Under global configuration for Jenkins, make sure to set the path to git as <InstallDirectory>\cmd\git.exe (NOT just git.exe)
  10. Go to the Jenkins dashboard and select “New Item”.
  11. Put in a name that suits you and select “FreeStyle Project”. Click Ok.
  12. On the configure page, under “Source Code Management”, select “Git” and put in the SSH url of your BitBucket repo.
  13. Set the credentials to “none”.
  14. Schedule your build (“Build Now”) and your should see that succeed.

On to setting up the build:

  1. Firstly download NuGet.exe from https://nuget.codeplex.com/releases copy the exe to somewhere you’ll find it.
  2. Install the latest version of the .Net framework and locate MSBuild.exe (take a look in the “framework” folder).
  3. Setup MSBuild on “Manage Jenkins > Configure System” by specifying a name and the full path to the MSBuild (noted in point 2).
  4. The purpose of NuGet here is to restore packages for the project and a bunch of parameters can be passed to nuget indicating package sources and the like. Personally, I prefer for all this to be setup in a config file instead. Take a look at the Nuget Command Line Reference if you’d like to use the command line options, but you can copy my settings (below), adjust for your need and store in a file named <Something>.config (remove the proxy settings if you don’t need those). This config file can then be passed in using -ConfigFile.  A note that passwords are hashed and will need to be set using the command line.
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
     <packageSources>
     <add key="Alternative package source" value="http://packages.nuget.org/v1/FeedService.svc/" />
     <add key="NuGet official package source" value="https://nuget.org/api/v2/" />
     </packageSources>
     <activePackageSource>
     <add key="All" value="(Aggregate source)" />
     </activePackageSource>
     <packageRestore>
     <!-- Allow NuGet to download missing packages -->
     <add key="enabled" value="True" />
    
     <!-- Automatically check for missing packages during build in Visual Studio -->
     <add key="automatic" value="True" />
     </packageRestore>
     <config>
     <add key="HTTP_PROXY" value="mycorpproxy" />
     <add key="HTTP_PROXY.USER" value="domain\user" />
     <add key="HTTP_PROXY.PASSWORD" value="the password as set by the command line option" />
     </config>
    </configuration>
  5. On the build configuration of the project in Jenkins, add the following steps under “Build” (They should be in this order as well):
    • “Execute Windows batch command” : <NuGet.exe Path>\NuGet.exe restore <name of sln/ csproj file of your project> -ConfigFile <Config Location>\<The config file from point 4>
    • “Build a Visual Studio project or solution using MSBuild”:

And that’s it. If you’ve followed all the steps correctly, you’ll have a Jenkins instance setup and ready to build .Net applications. Click Build Now and off you go….

 

See these sites for reference:

One thought on “Using Jenkins to pull and build .Net applications from BitBucket”

Leave a Reply

Your email address will not be published. Required fields are marked *