Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

We are going to discuss the basic workflow of Continuous Integration using TeamCity, Docker, and GitHub with the help of .NET Core Web API.

Agenda

  • Introduction of TeamCity
  • Features of TeamCity
  • TeamCity Continuous Integration
  • Step-by-step implementation of .NET Core Web API
  • TeamCity Installation
  • Build steps and other configurations of TeamCity

Prerequisites

  • GitHub
  • TeamCity
  • Docker Desktop with Docker Hub Login Credentials
  • Visual Studio 2022
  • .NET Core 6 SDK

Introduction of TeamCity

  • TeamCity is the general-purpose CI/CD Tool which is which automates all sorts of application builds, releases, and deployment with minimum configuration
  • It supports all continuous integration and continuous deployment-related things right from build, test, and deploy in the software development
  • CI/CD is the main part of DevOps and many tools in the market are used by many organizations like Jenkins, TeamCity, Azure DevOps, and many more.
  • This makes it easier for a developer to just focus on software development for better productivity by integrating changes rather than focusing on the build, testing and deploying of the application

Features of TeamCity

  • Integration with multiple IDE’s
  • Easy to configure and Installation
  • Multi-platforms support
  • User-Friendly Interface
  • Build History
  • Code Quality Tracking

TeamCity Continuous Integration

  • In the continuous integration process, we integrate all the latest code changes which are committed by the developer into the git and any other.

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

  • In real-time multiple developers are working on the same branch and EOD will commit their work to the git.
  • Later on, the Team City server will trigger the build pipeline whenever the new code is committed, run a test case, and create an error-free build for many environments like QA, DEV, and Production as per our needs.
  • If some build and unit test cases failed then the user will get notified about that and again the new code will come and this process is continuously work
  • If you want to learn more about Jenkins then read the official documentation (https://www.jetbrains.com/help/teamcity/teamcity-documentation.html)

Step-by-step implementation of .NET Core Web API

GitHub URL

https://github.com/Jaydeep-007/TeamCityDemo.git

Step 1

Create a new .NET Core Web API

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 2

Configure application

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 3

Provide additional information

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 4

Project structure

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 5

Edit Program class

var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
//{
   app.UseSwagger();
   app.UseSwaggerUI();
//}
app.UseAuthorization();
app.MapControllers();
app.Run();

Step 6 - Add Docker file

Right-click on the solution and add Docker Support (Note: Make sure Docker Desktop is running on your system)

If you don’t have a docker desktop then download it from

https://docs.docker.com/desktop/install/windows-install/

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

It will create one Docker file. Put that file inside the root folder of the project solution

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["TeamCityDemo/TeamCityDemo.csproj", "TeamCityDemo/"]
RUN dotnet restore "TeamCityDemo/TeamCityDemo.csproj"
COPY . .
WORKDIR "/src/TeamCityDemo"
RUN dotnet build "TeamCityDemo.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "TeamCityDemo.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TeamCityDemo.dll"]

Step 7

Push your code into Git Repository which we are going to be using inside TeamCity

TeamCity Installation

Step 1

Download TeamCity using the following URL

https://www.jetbrains.com/teamcity/download/

Step 2

Open the EXE and follow the steps I showed in the following images

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

 

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Build steps and other configurations of TeamCity

Step 1

If you want to start the TeamCity server on your own then run the following command inside C:\TeamCity\bin

.\runAll bat start

and to stop the server using following command

.\runAll bat stop

Step 2

Open the TeamCity server URL http://localhost:8111/ in the browser

Step 3

Add login credentials. If you don’t know credentials then go to the C:\TeamCity\logs\teamcity-server.log

And search “Super user authentication” and copy its token Id and put it inside the password section on the login page and leave the username empty

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 4

You can see the TeamCity dashboard after login

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 5

Create a new project and add the git repository URL and credentials

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 6

Click on Proceed and it will create a project

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 7

Click on configure build project and we create our build steps not the detected one by TeamCity

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 8

Before that go to the connection section of our project and configure the docker registry with docker credentials after that click on test and save it

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 9

Also, add the Build Feature with our docker registry

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 10

Now, we are going to add different build steps for creating our application build which later on publishes on docker

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 11

Add .NET Build Step

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 12

Add .NET Publish Step

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 13

Add Docker Build Step

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 14

Add Docker Push Step

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 15

Finally, now you can run your build

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Here you can see whether your build details like changes and build log to track build steps are working properly or not. Also, the git changes with that build are started and trigger automatically whenever you commit something inside the branch it will create a new build and push the latest on to the docker repository

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 16

Now open the Docker Desktop and pull your latest image on local machine

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 17

Run image after specifying container name and port

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 18

Now, you can see the image is running inside the docker container

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Step 19

Open application

Continuous Integration Of .NET Core Application Using TeamCity, GitHub, And Docker

Conclusion

Here we discussed TeamCity and Continuous Integration using GitHub, Docker, and .NET Core right from installation and configuration step by step.

Happy learning!


Similar Articles