Implementing your first Build Pipeline for Asp.Net Core using TeamCity Cloud & Docker Hub
Tags: docker, aspnetcore, teamcity, ci
Published: 2021-06-09 06:07:33

Hey guys, so recently I had this requirement which required me to integrate a build process for .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 anther machine and there it was our weatherforecast 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 https://www.jetbrains.com/teamcity/cloud/

* Docker Hub account with a repository to publish the image of the asp.net core solution https://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 the one I am using here https://github.com/rajat-srivas/dotnetcore-teamcity-docker

* 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 project

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8bu0vh9ngr43jjsnjhzs.png)


* 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 (https://github.com/rajat-srivas/dotnetcore-teamcity-docker), and credentials to access the repository
 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1tw5b8l8bw0208fp595t.png)


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

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yp1hrw9yu0sd5cozxrok.png)


* 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8p9pj0lt7vwddpeomg1v.png)


### 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 .net core solution was published
* **Docker Image Push** => Pushes the image on Docker hub

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rj43q76zja6ggmgh3mr.png)


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

#### .Net Build

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e05a4ixmjq93zbw3ovmz.png)

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

#### .Net Publish

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/77qnk7ll1w0elmml0a8s.png)


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

#### Docker Build

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

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zkf2ww483mq7n8fvb3f3.png)


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

#### Docker Push

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c2i7lmct3jr0w4c6ffxw.png)


* 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 success, the image should be published in the docker repository specified 😄🍻

![Great success Gif](https://media.giphy.com/media/a0h7sAqON67nO/giphy.gif)


![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqhad02firsw0kzukv9p.png)

 
## Running the App using the Docker Hub Image

Finally lets 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:<version>** 
* Run the container using the command below
**Docker run -p 80:80 --name <ContainerName> rajatsrivas/testdeploy:<version>**

## Conclusion

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

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2c8wpzv9wwzp5vays06.png)

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