Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Continuous Integration (CI) for .NET Core Projects: .NET Core CLI Part III

DZone's Guide to

Continuous Integration (CI) for .NET Core Projects: .NET Core CLI Part III

Learn how to set up a Jenkins pipeline for continuous integration of your nNet Core projects in this tutorial.

· DevOps Zone ·
Free Resource

Discover how you can reduce your Kubernetes installation from 22 steps to 1.

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

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

  2. 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 of the current post, but you can find all the details for the Pipeline here.

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

Create a 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:In the next window, select the pipeline option and give your pipeline a name:

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 created in the last post. I have pushed the code here.

Checkout

The first step for our CI would be to checkout the code from Git. For that, our pipeline code would be as below:

stage('Checkout') {
 steps {
  git credentialsId: 'userId', url: 'https://github.com/NeelBhatt/SampleCliApp', branch: 'master'
 }
}

Here:

  • git credentialsId: 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 the 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, the next step is to create the Nuget package which we will publish to our desired location. The nuget package is nothing but the glorified zip and it contains the same things 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

The next step is to publish our Nuget packages to storage.

As we have just created the Nuget package, we need some space to store our packages. Nuget.org provides a 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 (more details here).

For this post, we will publish the package to 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 the publish command to publish our packages from Jenkins.

stage('Publish') {
 steps {
  bat "dotnet nuget push **\\nupkgs\\*.nupkg -k yourApiKey -s http://myserver/artifactory/api/nuget/nuget-internal-stable/com/sample"
 }
}

The 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:

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

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 a better approach would be to create a Jenkinsfile in the root of your application in Git as shown below:

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

Click on Build now, and CI should work.

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

I hope this helps.

Download the Kubernetes cheatsheet to learn more about how easy it is to run Kubernetes on any infrastructure with Mesosphere DC/OS

Topics:
devops ,continuous integration ,tutorial ,.net core

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}