Continuous Integration(CI) for .Net Core projects: .Net Core CLI Part III

Continous Integration for .Net projects (1)

You can find my all .Net core posts here.

This is the 3rd post on .Net Core CLI commands. You can find first 2 posts below:

Guide to create and run .Net Core application using CLI Tools: .Net Core CLI Part I

Guide to Create and Publish Nuget packages using .Net Core CLI: .Net Core CLI Part II

In this post, we will create a basic Continuous Integration(CI) using .Net Core CLI commands and Jenkins pipeline.

Jenkins Pipeline

What is Jenkins Pipeline?

Jenkins Pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins. Pipeline provides an extensible set of tools for modeling simple-to-complex delivery pipelines “as code” via the Pipeline DSL.

Basically it is a Groovy script based language which is used to describe stages and steps within a pipeline.

We will not go deep into Jenkins Pipeline as it is out of the scope for current post, but you can find all the details for the Pipeline here.

I hope you are little bit aware of Jenkins Pipeline now and I hope you have your basic setup done for Jenkins. You can download the Jenkins from here.

Create Jenkins Pipeline

We will require a Jenkins pipeline for our CI where we will put all code required to run our CI. Let us create our pipeline from scratch.

Open Jenkins and click on New Item:

jen11

In the next window, select the pipeline option and give your pipeline a name:

jen12

Our pipeline is ready, we will come back to the pipeline once we will finalize our Pipeline code.

Let us add different stages which will be required to run our CI.

For this post, we will use the SampleCliApp which we have created in the last post. I have pushed the code here.

Checkout

First step for our CI would be to checkout the code from GIT. For that our pipeline code wold be as below:


stage ('Checkout') {

steps {

git credentialsId: 'userId', url: 'https://github.com/NeelBhatt/SampleCliApp',branch: 'master'

}

}

Here:

  • git credentialsId: is generally the ID by which you will access the git repo
  • url: GIT URL of your project
  • branch: Branch on which you want to set the CI

Once done, the project would be checked out\cloned on the agent which you have specified

Restore

Next step would be the restoring of the packages of the application.

Let us run restore command which will restore the packages if they are not available on your Windows agent:


stage ('Restore Packages') {

steps {

bat "dotnet restore"

}

}

This will restore the missing packages.

Clean

Next step is to clean the solution.

For that, let us add the clean command in the pipeline:


stage ('Clean') {

steps {

bat "dotnet clean"

}

}

This will clean the solution.

Build

Next step is building the solution. We will build our solution on the agent using dotnet cli commands.

For that, let us add the build command in the pipeline:


stage ('Build') {

steps {

bat "dotnet build --configuration Release"

}

}

This will build the solution. It will put dlls and other built files under bin\Debug\netcoreapp2.x

Pack

Once the solution is built, next step is to create the Nuget package which we will publish to our desired location. Nuget package is nothing but the glorified zip and it contains the things same as the published build.

For that, let us add the pack command in the pipeline:


stage ('Build') {

steps {

bat "dotnet pack --no-build --output nupkgs"

}

}

Above command will skip the build step and will put the nuget packages under nupkgs folder.

After this step, the Nuget packages will be created and would be stored under nupkgs folder

Publish

Next step would be to publish our Nuget packages to a storage.

As we have just created the Nuget package, we need some space to store our packages.

Nuget.org provides that space where you can store your Nuget packages but they are public so other developers can also see your packages. If you do not wish to use Nuget.org then there are other options like:

More details here: https://docs.microsoft.com/en-us/nuget/hosting-packages/overview

For this post, we will publish the package to the Artifactory.

Note that I have created an account in Artifactory so if you choose to use Artifactory, please create an account there.

Let us add publish command to publish our packages from the Jenkins.


stage ('Publish') {

steps {

bat "dotnet nuget push **\\nupkgs\\*.nupkg -k yourApiKey -s http://myserver/artifactory/api/nuget/nuget-internal-stable/com/sample"

}

}

Above command will put my nuget packages in the Artifactory folder.

That is it. Now let us put all the pieces together to create the complete Jenkins pipeline

Complete Jenkins Pipeline

The complete Jenkins pipeline looks like below:


pipeline {
agent any
environment {
dotnet = 'path\to\dotnet.exe'
}
stages {
stage ('Checkout') {
steps {
git credentialsId: 'userId', url: 'https://github.com/NeelBhatt/SampleCliApp',branch: 'master'
}
}
stage ('Restore PACKAGES') {
steps {
bat "dotnet restore --configfile NuGet.Config"
}
}
stage('Clean') {
steps {
bat 'dotnet clean'
}
}
stage('Build') {
steps {
bat 'dotnet build --configuration Release'
}
}
stage('Pack') {
steps {
bat 'dotnet pack --no-build --output nupkgs'
}
}
stage('Publish') {
steps {
bat "dotnet nuget push **\\nupkgs\\*.nupkg -k yourApiKey -s            http://myserver/artifactory/api/nuget/nuget-internal-stable/com/sample"
}
}
}
}

Once we have the pipeline created, go back to the Jenkins Pipeline which we have created above and paste our pipeline code into the Configure as shown below:

jen13

Once the code is added, go back to the Jenkins UI and click on Build now.

jen14

This will run the Jenkins job which will do all the things required for CI:

Checkout -> Restore -> Clean -> Build -> Pack -> Publish

JenkinsFile

Currently we have written the Jenkins pipeline code into the Jenkins pipeline configuration but better approach would be to create a Jenkinsfile in the root of your application in Git as shown below:

jen15

Once you add the Jenkins file, just fill the details under Pipeline section as below:

jen16

Click on Build now, CI should work.

We can add Test, Code Scan etc steps for CI but as current post aims basic flow of CI, I have not added it.

Hope it helps.

 

 

 

 

 

Advertisements

One thought on “Continuous Integration(CI) for .Net Core projects: .Net Core CLI Part III

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s