Implementing your first Build Pipeline for Asp.Net Core using TeamCity Cloud & Docker Hub

Implementing your first Build Pipeline for Asp.Net Core using TeamCity Cloud & Docker Hub

ยท

4 min read

Hey guys, so recently I had this requirement which required me to integrate a build process for the .Net Core app using the Github, TeamCity Cloud server, and Docker Hub.

So I researched about it and went on to implement it, and here's in brief what I did.

  • I used the out-of-box project created by Visual Studio and manually added a docker file in it.

  • I then went on to create a free trial account on the TeamCity cloud server where I used this docker file to build, publish and then push the docker image to my Docker Hub repository

  • Finally I pulled the docker image on another machine and there it was our weather forecast API up and running ๐Ÿš€ ๐Ÿš€.

Since I did could find a proper resource for implementing the same I thought why not give it a go myself. So here I am trying to explain the steps I followed.

Pre-requisite

  • Account on Team City Cloud Server jetbrains.com/teamcity/cloud

  • Docker Hub account with a repository to publish the image of the asp.net core solution hub.docker.com

  • Docker Desktop Installed on local to pull and run the deployed image

  • Sample Dot Net core solution with a docker file. You can refer to the one I am using here github.com/rajat-srivas/dotnetcore-teamcity..

  • I am also assuming you know about docker and are here to understand how to implement this integration

Project Setup On TeamCity Cloud Server

  • Login into your team city cloud server
  • Click on Project and then create the project

image

  • Next we will be connecting our TeamCity server to the GitHub repository so as it can pull up the code every time there is a new commit

  • Provide the repository URL (github.com/rajat-srivas/dotnetcore-teamcity..), and credentials to access the repository

image

  • Provide the project name and click on Proceed
  • Next we need to connect our Docker Hub repository to team city.
  • Go to the Projects setting page and click on Connections.
  • Click on Add Connection and configure Docker Registry

image

  • Go back to the build page
  • Click on Add Build Feature and select Docker Support.
  • Click on Add Registry Connection and use the docker connection created

image

Implementing the Build Pipeline ๐Ÿ—

  • Click on Build Steps to start creating the build pipeline
  • For the current solution we need four build steps:
  • Net Build => Build the .net solution
  • Net Publish => Publishes the .Net Solution in the specified output directory
  • Docker Image Build => Reads the specified docker file to create the docker image which contains the content of the folder where the .net core solution was published
  • Docker Image Push => Pushes the image on Docker hub

image

  • Refer to the above image to create the builds as mentioned
  • Lets now move on the configure the individual steps

.Net Build

image

  • Here we just need to set the Command as Build and provide the name of the solution from the repository

.Net Publish

image

  • Next we need to specify the folder where the files will be published. This needs to be the same as the COPY command source in the docker file.

Docker Build

  • Here rajatsrivas/testdeploy is the repository name on the Docker hub in which my image will be published

image

  • I'm using the build.counter flag to automatically version the docker image generated to keep track of it with the actual build which was triggered

Docker Push

image

  • Now here the only thing left to do in the build configuration is to push the image which was created to our Docker Hub repo

Trigger/Run Build Manually

  • Click on Run at the top right corner to trigger the build.
  • Once the build is successful, the image should be published in the docker repository specified ๐Ÿ˜„๐Ÿป

Great success Gif

image

Running the App using the Docker Hub Image

Finally, let's try and run the application using the image on our docker repository

  • Pull the image from docker hub using the command. docker pull rajatsrivas/testdeploy:
  • Run the container using the command below Docker run -p 80:80 --name rajatsrivas/testdeploy:

Conclusion

That's it, we have successfully implemented our first build pipeline for an Asp.Net core project using Github, TeamCity, and Docker Hub.

image

Hope you find these steps useful and you can use them to build upon your own more complex build pipelines. Keep learning keep building...

Did you find this article valuable?

Support Rajat Srivastava by becoming a sponsor. Any amount is appreciated!

ย