CI with TeamCity, GitHub, Docker

Image for post
Image for post

Many of you must be thinking how CI works or want to try by yourself. Here is a step by step guide which can help creating your own small CI cycle.

Prerequisite : basic knowledge of Docker,GitHub,NodeJs

Installation: Install TeamCity from below link & Download the executable https://www.jetbrains.com/teamcity/download/#section=get

Once Download completed start the installation.

Image for post
Image for post
Select build Agent and Server and click Next

If you face any issue while installing add your user in Local policies under Log on as Service

Image for post
Image for post
add your user under “Local Security Setting”

You will get screen to select the port. I gave 8111.

Image for post
Image for post
A window like below will appear .click on Save.
Image for post
Image for post
Click on OK
Image for post
Image for post
Select “Run TeamCity Server under a user account” and click Next
Image for post
Image for post
Enter credentials and click Next

After this it will grant permission to user , a black window will appear for few seconds. Run both the services and click Next and Click on Finish.

TeamCity is up and running at port at : http://localhost:8111

Image for post
Image for post
Set user name and password and save it somewhere.

Setting up GitHub repository :Before starting with TeamCity configurations let’s check the source code created in GIT.I have developed a small application using NodeJs-Express framework. If you want to create your own application you can use below link or any other helpful website. https://codeforgeek.com/express-nodejs-tutorial/

Also create DockerFile needed for image creation. Make sure your application source code and DockerFile is updated on GitHub.

Image for post
Image for post
Dockerfile. Application is Exposed to port 3000

Configurations in TeamCity: Now TeamCity installed on the machine and Application Source code updated on GIT, Let’s start configuring new Project in TeamCity.

Image for post
Image for post
Click on Create Project
Image for post
Image for post
Please enter full path of your repository in Repository URL and give your GitHub credentials. Click on proceed.
Image for post
Image for post
Enter Project name and Build configuration name and click on Proceed.
Image for post
Image for post
Click on build Steps to add steps in your build.

Here we will create 2 steps. One for creating image and another for running the container for that image.

Let’s start with first step : Create a docker step and provide the details given in image.You can give “Image name: tag” of your choice, make sure to it is in small letters only.

Image for post
Image for post
Click on Save, Build Step will be saved.

Now Let’s test the build step . You need to make sure docker is running on your machine before you test the step. Click on Run button on right top.

Image for post
Image for post
Click on Run button. Build will get generated and Build step will be tested
Image for post
Image for post
To see the build status click on Projects link on left top.

Now the build Step for image creation is tested successfully ,we will add another build step to start the container for the image.

Add build Step with Runner type as command Line.Enter details like below and save the changes. Just make sure you are giving the same image name you have given in first build step.

Image for post
Image for post
Custom script : Docker run -p 3000:3000 — name expresscontainer -d newexpress/node-web-app

We are ready with all the configurations in TeamCity. Let’s do some update on GitHub repository and see if build gets created automatically.You can use multiple ways to update GitHub repository, here I am using git bash. I have cloned the repository . now making changes in test file.

Image for post
Image for post
Save and Close the file.
Image for post
Image for post
git add , commit and Push changes to master.

Changes are updated on git master branch.

After successful git update build gets activated in TeamCity automatically and it will run the build steps which will create image and run the container.

You can see build has been triggered and both the steps have been executed.

Image for post
Image for post
In Build log you can see Step 1/2 & 2/2 executed successfully.
Image for post
Image for post
Also image and container is created.
Image for post
Image for post
The application website is now up and running on port 3000 , same port was given in Docker file mentioned above.

Now once you see everything is fine and it is working but we missed one thing here.What will happen when build gets triggered more than once what will happen with existing running containers…. so, you are right we should stop and remove existing container.

For removing existing container we need to add another build step and make re-ordering. So, existing container will be removed first then new image will be tagged to latest and new container will be started with latest updates in application.

Since I am using windows machine I have created a batch file which contains code to check if container exists and if its there it will be stopped and removed. Add the same file to your GitHub repository at root level.

Batch file Code :

@echo off
FOR /F “tokens=1 delims=” %%I in (‘docker ps -aq -f “name=expresscontainer”’) do set runningContainer=%%I
FOR /F “tokens=1 delims=” %%A in (‘docker ps -aq -f “status=exited” -f “name=expresscontainer”’) do set exitedContainer=%%A
if not [%runningContainer%] == [] (
docker stop expresscontainer
docker rm expresscontainer
echo “Removed”

)else if not [%exitedContainer%] == [] (
docker rm expresscontainer
echo “exited”
)
set “runningContainer=”
set “exitedContainer=”

Image for post
Image for post
Remove.bat
Image for post
Image for post
Image for post
Image for post
All the build steps are ready now.

I hope this blog will help you build you own CI cycle. if you have any suggestion please do let me know.

Image for post
Image for post
Summary of the blog

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store