Ruby CI/CD pipeline with Travis
This repository contains a simple Sinatra web application, the unit tests and a
CI/CD pipeline using Travis-CI.
The web application tries to establish a RESTful API that interacts with a
PostgreSQL database. It simply create new “person” records on POST requests and
query by the id on GET requests.
To install the required libraries/3rd-party dependencies, use:
$ make install
This target will install bundler and, with bundler, install the necessary gems.
Alternatively, just use:
$ bundle install
To start the service, use:
$ make run
This target uses rack
, so the service port will be binded, by default, to9292
. Alternatively, just use:
$ ruby src/app.py
To do it directly. In this case, the service port will be binded to 4567
.
The following environment variables are used by the service:
Variable | Description |
---|---|
PG_HOST |
The database hostname |
PG_PORT |
The port number |
PG_USER |
The database username |
PG_DBNM |
The username password |
The service expects an already created database with a table called users
.
The table should have the following schema:
id SERIAL
name VARCHAR(255)
surname VARCHAR(255)
age INT
After connecting to the database:
CREATE DATABASE sinatra;
USE sinatra;
users
CREATE TABLE users (id SERIAL, name VARCHAR(255), surname VARCHAR(255), age INT);
The application exposes three routes:
GET /
Returns a “Hello World“.
GET /users/:id
Connects to the database and query the users
table by the id
. The
response should be similar to:
{
"name": "John",
"surname": "Williams",
"age": "86"
}
POST /users
Connects to the database and inserts a new row into the users
table. The
payload should contains the keys name
(string), surname
(string) and
age
(int).
E.g. (using the http tool):
http POST :4567 name="John" surname="Williams" age=86
To execute the unit tests, use:
$ make test
The pipeline tries to accomplish a flow where, given a new release (a tag), a
new Docker image is generated and pushed to a private Docker registry (in AWS,
with ECR. It’s the “CD“ (continuous delivery) aspect of it. The
“CI“ (continuous integration) is achieved by the unit tests.
push
, pull request
and tag
test
push
on master
or tag
build
tag