Automating build and test of Go applications with Jenkins

Wilson Tan
Level Up Coding
Published in
6 min readJun 8, 2020

--

Have you ever experienced a situation where your development team has deployed a piece of code and ended up with errors in another part of the system? More often than not, we all have been in this problem before.

So how does one reduce the occurrence of such a situation? Do you have many testers in your organization to test every single function again and again whenever a new piece of code is released?

As we strive to deliver good quality software at speed, there is a need for proper need for continuous integration and continuous delivery (CI/CD) tools.

Continuous integration and continuous delivery (CI/CD) workflow

What is CI/CD?

A CI/CD pipeline helps you to automate the software delivery processes, from initializing code build, running automated tests to deploying to staging, or production environment.

A typical CI/CD workflow looks like below:

  1. An engineer makes some changes to the application code.
  2. When the code is ready, it’s pushed to Git repository and a pull request is created for senior’s review.
  3. CI automatically triggers the build and test to ensure that it does not break the system.
  4. Once the build and test pass, the senior proceeds to review the code and do the merging.
  5. The artifact is then released and deployed to staging or production environment.
  6. Information is collected from the site to provide feedback to the team to know the status and impact of the latest build version.
  7. Any new feature or bug is reported back to the team and added to backlog.

What are the available CI/CD tools?

With the rise in demand and need of CI/CD workflow, there have been many tools made available such as:

  1. Jenkins — https://www.jenkins.io/
  2. Travis CI — https://travis-ci.org/
  3. CircleCI — https://circleci.com/
  4. TeamCity — https://www.jetbrains.com/teamcity/
  5. GitLab — https://about.gitlab.com/

And many many more…

Today, we are going to look at automating the build and test of a Go application using Jenkins.

Jenkins is an open source Java-based automation tool that supports building, deploying, and automating any software development projects. It is easy to install and it comes with many plugins (thanks to the active community!) that you can use in your automation process.

Installing Jenkins

Jenkins can be installed on various platforms, be it in your local machine, in the cloud, or on Docker platform. If you choose to install Jenkins directly on the machine, Java must be installed first.

In this example, we will look at installing Jenkins on Docker platform and of course, the prerequisite is to have Docker installed.

  1. Pull the Docker image by running docker pull jenkins/jenkins:lts
  2. Create a container using the Docker image by running docker run -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts
  3. Verify that Jenkins is up and running by visiting localhost:8080
  4. You will then be greeted with a page (like below) asking for administrator password which you can find in the docker logs

5. Install the suggested plugins.

6. Create the first admin user.

7. You will then be greeted with the Jenkins dashboard.

Jenkins dashboard

Setting up Jenkins job

Once Jenkins is up and running, we can proceed to set up a job to automatically run the build and test of our Go application (which is hosted in a private GitHub repository in this example).

First, we have to install the Go plugin which set up Golang tools for the build. To do that, click on Manage Jenkins on the left hand menu followed by Manage Plugins then search for ‘Go Plugin’ under the Available tab (I have mine installed so it appears in Installed tab).

Go plugin on Jenkins

Then, we will have to add the Go version that we are interested in the Global Tool Configuration (in Manage Jenkins as well). Give it a name which you will need to refer to later on in setting up Jenkins job.

Adding Go as global tool

Once that is setup, we can start creating our Jenkins job, which in this example, we will be looking at pipeline. It can be triggered by clicking on the New Item on the left hand menu followed by selecting Pipeline.

Then we can start creating our pipeline. In the Build Triggers tab, there’s various option. You can create a webhook on your GitHub repository which can trigger the build on Jenkins whenever there is a pull request or you can trigger the build based on another build outcome on the Jenkins. We can also schedule it to run using cron expression.

Build trigger for the pipeline

Next up is the pipeline script where we define our steps to be carried out in each stage. There are two ways for Jenkins to retrieve the pipeline script, either from the configuration itself or pulling from the repository.

Jenkins pipeline

In the screenshot above, we will be using the Jenkinsfile defined in our repository. Let’s dive into pipeline script.

Jenkinsfile to build and test Go application

Explanation of the Jenkinsfile:

  1. In the beginning, we will define the agent as any. This means Jenkins will assign any available executor regardless of how it is being labeled or configured.
  2. In the tools, we will specify the build tool where in this case we will be using ‘go1.14’ that we have defined earlier on in Global Tool Configuration.
  3. Then we will set up the environment variables just like how we set it up whenever we run a Go application in our local machine.
  4. We will then define our stages: Pre Test where we install all the dependencies, Build where we will compile and build our Go application, Test where run unit testing etc. You can also add other stages too.
  5. Last but not least is the post section which can be run on various condition such as always, unstable, success, failure and changed after all the stages have been executed. In this example, we will trigger an email notification regardless of the build status.

Running Jenkins Job

Once the job is being setup, we can execute the run now either manually via the Build / Build with Parameters button or automatically via scheduled run or triggering webhook in your GitHub repository.

Example of the overview of the Jenkins pipeline

The above shows an example of overview of the Jenkins pipeline. At a glance, we can see the build history in the past as well as the failing stages. We can also dig deeper into the logs in the particular stage to see what’s failing.

There’s a lot more that one can achieve using Jenkins (thanks to the variety of plugins), for example deploying to staging environment upon successful build and test or notifying the team via Slack or sending the build status back to GitHub to state that the PR is safe to be merged.

We will come back again for more tips and tricks! Till next time, cheers!

--

--