Photo by timea dombi / Unsplash

Creating a CI/CD Pipeline with Travis CI, GitHub and Docker Hub for Spring Boot based Web Application

CI/CD Aug 31, 2021

This post briefly captures the process of triggering a Spring Boot application docker image build using Travis CI and pushing the Docker image to Docker Hub when code is pushed from development machine to GitHub.

Introduction

GitHub is a provider of Internet hosting for software development and version control using Git. It offers the distributed version control and source code management functionality of Git, plus its own features.

Travis CI is a hosted continuous integration service used to build and test software projects hosted on GitHub and Bitbucket.

Docker Hub Docker Hub is a service provided by Docker for finding and sharing container images with your team. It is the world’s largest repository of container images with an array of content sources including container community developers, open source projects and independent software vendors (ISV) building and distributing their code in containers.

Goals

  1. Developer pushes code to GitHub
  2. Job is triggered in Travis CI
  3. Travis CI builds Docker Image and pushes to Docker Hub

Minimum Requirements

Getting Started

Sample Project

Spring Boot Minimal Web App is the sample Spring Boot web application i've used to illustrate Integration Testing.

Navigate to http://localhost:8080/ to discover the application URLs.

In the application.properties file present in the resources folder, set the spring.profiles.active value to application-h2db since we will be running the tests before initiating the build process which is dependent on all the tests passing.

Note: mvn package -Dmaven.test.skip=true Command to build the project by skipping all tests.

Noticed an issue with this Sample Project? Open an issue or a PR on GitHub!

Step 1 - Add Dockerfile file to the project.

Step 2 - Add .travis.yml file to the project.

Step 3 - Generate Access Token from Docker Hub

In the Security section of your Docker Hub account Settings page, click on the New Access Token button to generate a new access token with Read, Write, Delete scope. If you prefer using an existing access token, use it.

Note : This access token will be used as the value for the DOCKER_PASS Environment Variables key.

Reference : https://docs.docker.com/docker-hub/access-tokens/#create-an-access-token

Step 4 - Activate GitHub Repository in Travis CI

Login to https://app.travis-ci.com and activate the repository of interest.

Approve & Install Travis CI on the selected repository.

Confirm Travis CI installation by entering the GitHub account password.

In Travis CI dashboard, under the repositories tab click on the settings button associated with the repository.

In the Environment Variables section of Settings tab of the selected repository, add Docker Hub user details.

DOCKER_USER : YOUR_DOCKER_HUB_USERNAME
DOCKER_PASS : YOUR_DOCKER_HUB_ACCESS_TOKEN

Step 5 - Push Code to GitHub

Now that we've configured the sample Spring Boot Application with necessary files (Dockerfile, .travis.yml) and the required services (Travis CI, Docker Hub) with necessary environment variables and access tokens, we can push the code changes from our local development machine to our remote GitHub repository.

git push origin main

Outcome: Travis CI

We can now expect travis ci to run the tests, build the docker image of the application and push the same to dockerhub.

Outcome: Docker Hub

Finally, the lastest docker image of the application must be present in your docker hub registry.

Tags

Anantha Raju C

| Poetry | Music | Cinema | Books | Visual Art | Software Engineering |