ASP.Net Web Application Continuous Integration (CI) using Jenkins automation server: Creating Jobs In Jenkins: Getting source code from Bitbucket hosted git repository, polling the SCM to check whether changes were made, restore Nuget packages, build project using MSBuild, publish and archive the build

In this blog post, we will see how we can create a now Job in Jenkins. We will see how we can configure Jenkin’s Job to :

  • Get latest code from Bitbucket hosted git repository
  • Poll the SCM to check whether changes were made (i.e. new commits) and builds the project if new commits where pushed since the last build
  • Restore Nuget packages
  • Build & publish Visual Studio solution/project using MSBuild
  • Archive the builds

Note: You are currently on Part IV of this series, do refer below parts for more details.

Part I: Basic Setup, Installation & Configuration

Part II: Starting & setting up Jenkins

Part III: Creating & Adding SSH key to Bitbucket

A. Create new Job

Click on  “New Item” link.

Create New Job

Enter an item name, select “Freestyle project” & click “OK”.

Freestype project

B. “General”  Tab

Add “Description”. You can leave rest of the field as is.

General

C. “Source Code Management” Tab

I. Select “Version Control System”

My project is using Git version control system, so I have selected “Git”.

Source Code Management

II. Add repository details

a. Repository URL

My Git repository is hosted on Bitbucket. From Bitbucket, I will get the SSH clone url.

Bitbucket SSH Repository Clone Url

Enter the SSH clone url of the repository, to “Repository URL”.

Add credentials

b. Add Credentials

You need to add your repository credential. We have already created the SSH public & private keys for Bitbucket. Also, we have added the public key content to the Bitbucket SSH key. In this step, we will need to add private key content to Jenkins credentials.

SSH - private key

 

  • For “Kind”, select “SSH Username with private key”.
  • For “Username”, enter any name for your credentials.
  • For “Private Key”, select “Enter directly” and enter the contents for your SSH private key in “Key” textbox.
  • Enter the “Passphrase” that you have entered while creating SSH keys.

Click “Add” after filling the details.

Jenkins Credentials Provider - Add Credentials New

Now, select the “Credentials” that you have created. All the connection error will be fixed now.

Pick Credentials

c. Branches to build

Enter “Branch Specifier”. In my case, I need to build the master branch.

Specify Branch to Build

D. “Build Triggers” Tab

a. Select “Poll SCM”

Poll SCM periodically polls the SCM to check whether changes were made (i.e. new commits) and builds the project if new commits where pushed since the last build.

b. Set “Schedule” to  H/5 * * * * to poll every five minutes.

Build Triggers

E. “Build” Tab

I. Add Build Step, for Nuget Packages Restore

Add “Execute Windows batch command” build step.

Build - Execute Windows batch command

In “Command” textbox, enter nuget restore

Nuget Restore

II. Add Build Step, for building a Visual Studio project or solution using MSBuild

Add “Build a Visual Studio project or solution using MSBuild” build step.

Add build step - Build a Visual Studio project or solution using MSBuild

For “MSBuild Version”, select MSBuild-Default

For “Command Line Arguments”, set the following:

/p:Configuration=Release /p:DeployOnBuild=true /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:DeleteExistingFiles=true /p:publishUrl=I:\Jenkins\jenkins_home_dir\workspace\publish\MySampleApplication\Latest

MSBuild will build the solution in “Release” mode and on successful build it will publish the build to “I:\Jenkins\jenkins_home_dir\workspace\publish\MySampleApplication\Latest” directory.

Configure - Build a Visual Studio project or solution using MSBuild

III. Add Build Step, for archiving the builds

Add “Execute Windows batch command” build step.

Build - Execute Windows batch command

In “Command” textbox, set the following:

rem Create TIMESTAMP:
FOR /f %%a IN ('WMIC OS GET LocalDateTime ^| FIND "."') DO SET DTS=%%a
SET TIMESTAMP=%DTS:~0,8%_%DTS:~8,6%
rem Create folder:
md I:\Jenkins\jenkins_home_dir\workspace\publish\MySampleApplication\%TIMESTAMP%
xcopy /E "I:\Jenkins\jenkins_home_dir\workspace\publish\MySampleApplication\Latest" "I:\Jenkins\jenkins_home_dir\workspace\publish\MySampleApplication\%TIMESTAMP%"

Build - Execute Windows batch command-archive

This will create a new folder with current Timestamp as name and copy the contents of build published ("I:\Jenkins\jenkins_home_dir\workspace\publish\MySampleApplication\Latest") in previous step to it.

Latest Build with archives

F. Save the Job

Save the Job

The job you have created is ready now.

G. Build the project

You can trigger the build manually by clicking “Build Now” icon.

Jenkins Build Now

Under “Build History“, you can see that the build is triggered.

Build Started

Click on the build number, to check the details of particular build.

Click the build number and Check the console output

Click on “Console Output”, to check full build logs.

Build Logs

Previous: Creating & Adding SSH key to Bitbucket

That it !!!

You may also like...

3 Responses

  1. April 11, 2019

    […] Part IV: Creating Jobs In Jenkins: Getting source code from Bitbucket hosted git repository, polling the SCM … […]

  2. April 11, 2019

    […] Part IV: Creating Jobs In Jenkins: Getting source code from Bitbucket hosted git repository, polling the SCM … […]

  3. April 11, 2019

    […] Part IV: Creating Jobs In Jenkins: Getting source code from Bitbucket hosted git repository, polling the SCM … […]

Leave a Reply

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