Skip to content

Commit

Permalink
Adds Travis CI integration (#15)
Browse files Browse the repository at this point in the history
* Adds basic Travis CI pipeline

* Add Coveralls integration

* Installs goveralls in before CI pipeline

* Refactor in Makefile targets naming

Changes the naming and structure of Makefile targets to make them more
readable.

* Add prefixes to targets based on their context:
  - db- for database related targets
  - test- for test related targets

* Add script to fetch the migrate binary only if its not installed

* Dinamically handles the platform where the target is being run. Currently
supports two platforms:
  - Darwin - development machines
  - Linux - TravisCI jobs

* Updates README badges to original repo

* Adds CodeClimate badge to README

* Improvements in Makefile style and solve PRs comments

* Add TODO in Makefile for future improvements
  • Loading branch information
gerson-scanapieco committed Oct 4, 2019
1 parent da5b616 commit d18358e
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 59 deletions.
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: go
go:
- '1.13'
services:
- docker
cache:
directories:
- "$GOPATH/pkg"
before_install:
- go get github.com/mattn/goveralls
install:
- make ci/install
script:
- make ci/test
after_success:
- goveralls -coverprofile=coverage.out -service=travis-ci
156 changes: 97 additions & 59 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,88 +1,114 @@
testable_packages=$(shell go list ./... | egrep -v 'constants|mocks|testing')
project=$(shell basename $(PWD))
project_test=${project}-test
pg_dep=$(project)_postgres_1
test_packages=`find . -type d -name "docker_data" -prune -o \
-type f -name "*.go" ! \( -path "*vendor*" \) -print \
| sed -En "s/([^\.])\/.*/\1/p" | uniq`
database=postgres://postgres:$(project)@localhost:8432/$(project)?sslmode=disable
database_test=postgres://postgres:$(project)@localhost:8432/$(project_test)?sslmode=disable
platform=darwin
test_db_name=${project}-test
pg_docker_image=$(project)_postgres_1
db_url=postgres://postgres:$(project)@localhost:8432/$(project)?sslmode=disable
db_test_url=postgres://postgres:$(project)@localhost:8432/$(test_db_name)?sslmode=disable
uname_S=$(shell uname -s)

export GO111MODULE=on
# TravisCI runs in Linux instances, while developers run in MacOS machines
ifeq ($(uname_S), Darwin)
platform := darwin
else
platform := linux
endif

setup: setup-project setup-deps
# TODO(gerson-scanapieco): Pass arguments to make targets to reduce duplication, e.g. db/create and db/create-test
# becoming make db/create DB_URL=...

setup-project:
@go mod download
.PHONY: all
all: setup/migrate download-mod db/setup

setup-deps:
@make deps
@make migrate
.PHONY: ci/install
ci/install: setup/migrate download-mod db/setup-test

# run this if you don't have migrate
setup-migrate:
@curl -L https://github.com/golang-migrate/migrate/releases/download/v4.4.0/migrate.$(platform)-amd64.tar.gz | tar xvz
@mv migrate.$(platform)-amd64 /usr/local/bin/migrate
.PHONY: build
build:
@mkdir -p bin && go build -o ./bin/$(project) .

deps:
@mkdir -p docker_data && docker-compose up -d postgres
@until docker exec $(pg_dep) pg_isready; do echo 'Waiting Postgres...' && sleep 1; done
@docker exec $(pg_dep) createuser -s -U postgres $(project) 2>/dev/null || true
@docker exec $(pg_dep) createdb -U $(project) $(project) 2>/dev/null || true
.PHONY: docker/build
docker/build:
@docker build -t $(project) .

deps-test:
@mkdir -p docker_data && docker-compose up -d postgres
@until docker exec $(pg_dep) pg_isready; do echo 'Waiting Postgres...' && sleep 1; done
@docker exec $(pg_dep) createuser -s -U postgres $(project) 2>/dev/null || true
@docker exec $(pg_dep) createdb -U $(project) $(project_test) 2>/dev/null || true
@make migrate-test
.PHONY: run
run:
@reflex -c reflex.conf -- sh -c ./bin/Will.IAM start-api

.PHONY: test
test: db/setup-test test/unit test/integration db/stop-test

# Installs the golang-migrate dependency if its not already installed.
.PHONY: setup/migrate
setup/migrate:
ifeq ($(shell command -v migrate),)
@echo "Installing migrate..."
@curl -L https://github.com/golang-migrate/migrate/releases/download/v4.4.0/migrate.$(platform)-amd64.tar.gz | tar xvz
@mv migrate.$(platform)-amd64 $(GOPATH)/bin/migrate
@echo "Done"
else
@echo "migrate is already installed. Skipping..."
endif

.PHONY: download-mod
download-mod:
@go mod download

stop-deps:
.PHONY: compose-down
compose-down:
@docker-compose down

stop-deps-test:
@make drop-test
@make stop-deps
.PHONY: db/setup
db/setup: db/up db/create-user db/create db/migrate

build:
@mkdir -p bin && go build -o ./bin/$(project) .
.PHONY: db/setup-test
db/setup-test: db/up db/create-user db/create-test db/migrate-test

build-docker:
@docker build -t $(project) .
.PHONY: db/up
db/up:
@mkdir -p docker_data && docker-compose up -d postgres
@until docker exec $(pg_docker_image) pg_isready; do echo 'Waiting Postgres...' && sleep 1; done
@sleep 2

run:
@reflex -c reflex.conf -- sh -c ./bin/Will.IAM start-api
.PHONY: db/create-user
db/create-user:
@docker exec $(pg_docker_image) createuser -s -U postgres $(project) 2>/dev/null || true

.PHONY: db/create
db/create:
@docker exec $(pg_docker_image) createdb -U $(project) $(project) 2>/dev/null || true

migrate:
@migrate -path migrations -database ${database} up
.PHONY: db/create-test
db/create-test:
@docker exec $(pg_docker_image) createdb -U $(project) $(test_db_name) 2>/dev/null || true
@sleep 2

migrate-test:
@migrate -path migrations -database ${database_test} up
.PHONY: db/stop-test
db/stop-test: db/drop-test compose-down

drop:
@migrate -path migrations -database ${database} drop
.PHONY: db/drop-test
db/drop-test:
@migrate -path migrations -database ${db_test_url} drop

drop-test:
@migrate -path migrations -database ${database_test} drop
.PHONY: db/migrate
db/migrate:
@migrate -path migrations -database ${db_url} up

test:
@make deps-test
@make test-fast
@make stop-deps-test
.PHONY: db/migrate-test
db/migrate-test:
@migrate -path migrations -database ${db_test_url} up

test-fast:
@make migrate-test
@make unit
@make integration
@make drop-test
.PHONY: db-drop
db-drop:
@migrate -path migrations -database ${db_url} drop

unit:
.PHONY: test/unit
test/unit:
@echo "Unit Tests"
@go test ${testable_packages} -tags=unit -coverprofile unit.coverprofile -v
@make gather-unit-profiles

integration:
.PHONY: test/integration
test/integration:
@echo "Integration Tests"
@ret=0 && for pkg in ${testable_packages}; do \
echo $$pkg; \
Expand All @@ -91,6 +117,16 @@ integration:
done; exit $$ret
@make gather-integration-profiles

.PHONY: ci/test
ci/test:
@echo "Unit Tests - START"
@go test ${testable_packages} -tags=unit -covermode=count -coverprofile=coverage.out -v -p 1
@echo "Unit Tests - DONE"
@echo "Integration Tests - START"
@go test ${testable_packages} -tags=integration -covermode=count -coverprofile=coverage.out -v -p 1
@echo "Integration Tests - DONE"

.PHONY: gather-unit-profiles
gather-unit-profiles:
@mkdir -p _build
@echo "mode: count" > _build/coverage-unit.out
Expand All @@ -100,6 +136,7 @@ gather-unit-profiles:
@find . -type d -name "docker_data" -prune -o \
-name "*.coverprofile" -exec rm {} +

.PHONY: gather-integration-profiles
gather-integration-profiles:
@mkdir -p _build
@echo "mode: count" > _build/coverage-integration.out
Expand All @@ -108,3 +145,4 @@ gather-integration-profiles:
do tail -n +2 $$f >> _build/coverage-integration.out; done'
@find . -type d -name "docker_data" -prune -o \
-name "*.coverprofile" -exec rm {} +

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Will.IAM

[![Build Status](https://travis-ci.org/topfreegames/Will.IAM.svg?branch=master)](https://travis-ci.org/topfreegames/Will.IAM)
[![Coverage Status](https://coveralls.io/repos/github/topfreegames/Will.IAM/badge.svg?branch=master)](https://coveralls.io/github/topfreegames/Will.IAM?branch=master)
[![Maintainability](https://api.codeclimate.com/v1/badges/d89ff8b1c3a43d13e040/maintainability)](https://codeclimate.com/github/topfreegames/Will.IAM/maintainability)

Will.IAM solves identity and access management.

* Authentication with Google as OAUTH-2 provider.
Expand Down
2 changes: 2 additions & 0 deletions api/auth_middleware_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build integration

package api_test

import (
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kylelemons/godebug v1.1.0
github.com/magiconair/properties v1.8.1 // indirect
github.com/mattn/goveralls v0.0.3 // indirect
github.com/ogier/pflag v0.0.1 // indirect
github.com/onsi/ginkgo v1.10.1 // indirect
github.com/onsi/gomega v1.7.0 // indirect
Expand All @@ -36,5 +37,6 @@ require (
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect
golang.org/x/net v0.0.0-20190628185345-da137c7871d7 // indirect
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb // indirect
golang.org/x/tools v0.0.0-20191001184121-329c8d646ebe // indirect
mellium.im/sasl v0.2.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/goveralls v0.0.3 h1:GnFhBAK0wJmxZBum88FqDzcDPLjAk9sL0HzhmW+9bo8=
github.com/mattn/goveralls v0.0.3/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
Expand Down Expand Up @@ -258,7 +260,11 @@ golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190624190245-7f2218787638 h1:uIfBkD8gLczr4XDgYpt/qJYds2YJwZRNw4zs7wSnNhk=
golang.org/x/tools v0.0.0-20190624190245-7f2218787638/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20191001184121-329c8d646ebe h1:hFr8KcN0dM0/dqbUW0KZYN+YXJeZBpBWIG9ZkMuX1vQ=
golang.org/x/tools v0.0.0-20191001184121-329c8d646ebe/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
Expand Down

0 comments on commit d18358e

Please sign in to comment.