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.

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

B. “General” Tab

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

C. “Source Code Management” Tab

I. Select “Version Control System”

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

II. Add repository details

a. Repository URL

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

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

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.

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.

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

c. Branches to build

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

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.

E. “Build” Tab

I. Add Build Step, for Nuget Packages Restore

Add “Execute Windows batch command” build step.

In “Command” textbox, enter 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.

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.

III. Add Build Step, for archiving the builds

Add “Execute Windows batch command” build step.

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%"

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.

F. 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.

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

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

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

Previous: Creating & Adding SSH key to Bitbucket

That it !!!