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.
If you face any issue while installing add your user in Local policies under Log on as Service
You will get screen to select the port. I gave 8111.
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
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.
Configurations in TeamCity: Now TeamCity installed on the machine and Application Source code updated on GIT, Let’s start configuring new Project in TeamCity.
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.
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.
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.
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.
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.
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 :
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
)else if not [%exitedContainer%] ==  (
docker rm expresscontainer
I hope this blog will help you build you own CI cycle. if you have any suggestion please do let me know.