Continuous Integration of Golang App using Circleci

Phati Sawant
4 min readMay 12, 2021

--

Performing automated tests on a github repository every time code is pushed to the remote.

What is continuous integration?

When multiple contributors are working on the same project or a repository and a lot of code is being pushed to the remote it becomes difficult for the admin to review and merge all those changes to the master branch making him difficult to focus on contributing towards the project. Continuous integration is technique by which these managerial tasks can be automated and every time some changes are made to the code the test cases are run and it would make it easy for the reviewer to make a decision while merging the code and ensure that the application doesn’t break.

Circleci

It is a CI/CD tool that is used to run automated tests and build and deploy applications to cloud. It easily integrates with github and gitlab so we can easily set up our projects from there. We are going to learn how to run automated tests in this tutorial using github, circleci and golang.

What we will need?

  1. Golang installed on your machine
  2. Github account
  3. Circleci account

Lets get started

I. Creating a github repository

  1. Login to your github account and create a new repository.

2. Clone the repository to your local machine by opening a terminal and executing the below command.

II . Writing golang application

Initialize a go module to manage our dependencies easily.

We will be creating a simple rest API in golang that will have some mathematics calculation end points, like add, sub etc. and we will test those math functions(). Yes we could have just written a normal function and a test function instead of creating a rest API, but I am also planning to extend this blog to show continuous deployment, for which we will need a server.

Create a folder api in the root directory of the project and inside that create a file named response.go and paste the below code.

Create another folder in the root directory of the project and name it service and create two files add.go and add_test.go, add below code to the files.

Create a folder called handler and inside that create a file called handler.go and add the below code to it.

Now create a folder server and inside that create a file router.go, add below code to the file.

Now finally add main.go file to the root directory.

Now install all the dependencies by executing the following command in the terminal.

Now lets run the tests using the following command.

Now that our test cases are running locally we can now set up our project on circleci, sign in to circleci using your github account. Under the projects section click on Set up project for the desired project.

Now select Go as the programming language from the options and then in the next options choose Use Existing Config.

Now click on start building.

Now you will see that the test cases will fail on circleci, but no worries, this is because we have not added the config.yml to our project. We will do that soon.

Now finally lets create a folder .circleci to the root directory of our project and to it add config.yml file

Now commit and push the code to the remote and see that the test cases will run automatically on Circleci.

Wrap up

Now we know how Continuous integration helps in ensuring that only correct code and features are integrated into the final build and our application is bug free. Circleci is one of such tools and it saves us a lot of time as the test cases are run on cloud and are pretty fast.

--

--