Skip to content

Commit

Permalink
feat: Initial release and container publishing pipeline (#19)
Browse files Browse the repository at this point in the history
* Use `make build` from dockerfile

* Use multi stage build

* Move dockerfile to root and branch out

* Add dockerignore

* Slim down images and remove volume from compose

* Fix pgdata and change pg version

* Update readme

* Remove extra dockerfiles

* Change dockerfile contents

* Add remainder makefile commands

* Add release job to CI config

* fix makefile

* Add store_artifacts step

* fix: minor fixes to container build/release workflow

* Reference the aerogear org and add ci-server-agnostic env var

* Remove references to other dockerfiles and document new make targets

* Change postgres image

* fix: use rhscl postgres image

* Also change user for circleci
  • Loading branch information
paolobueno committed Feb 26, 2018
1 parent 42d3df4 commit a157d8e
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 64 deletions.
38 changes: 25 additions & 13 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,43 @@ version: 2
jobs:
build:
docker:
# specify the version
- image: circleci/golang:1.9

- image: postgres:9.5
- image: registry.access.redhat.com/rhscl/postgresql-96-rhel7:latest
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: aerogear_mobile_metrics

# Specify service dependencies here if necessary
# CircleCI maintains a library of pre-built images
# documented at https://circleci.com/docs/2.0/circleci-images/
# - image: circleci/postgres:9.4

POSTGRESQL_PASSWORD: postgres
POSTGRESQL_USER: postgresql
POSTGRESQL_DATABASE: aerogear_mobile_metrics
#### TEMPLATE_NOTE: go expects specific checkout path representing url
#### expecting it in the form of
#### /go/src/github.com/circleci/go-tool
#### /go/src/bitbucket.org/circleci/go-tool
working_directory: /go/src/github.com/aerogear/aerogear-metrics-api
steps:
- checkout
- run: go get github.com/mattn/goveralls
- run: go get -u github.com/golang/dep/cmd/dep
- checkout
- run: dep ensure
- run: make test-integration-cover
- run: /go/bin/goveralls -coverprofile=coverage-all.out -service=circle-ci -repotoken=$COVERALLS_TOKEN
- run: /go/bin/goveralls -coverprofile=coverage-all.out -service=circle-ci -repotoken=$COVERALLS_TOKEN
release:
filters:
# execute only on semver-style tags (vX.X.X)
tags:
only: /^v\d\.\d\.\d/
branches:
# needs to explicitly ignore all branches else gets run every time
ignore: /.*/
docker:
- image: circleci/golang:1.9
working_directory: /go/src/github.com/aerogear/aerogear-metrics-api
steps:
- run: go get github.com/goreleaser/goreleaser
- checkout
- run: make release
- run: make docker_build_release
- run: make docker_push_release
- store_artifacts:
path: dist
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# .goreleaser.yml
# Build customization
builds:
# makefile currently relies on this name
- binary: metrics
main: ./cmd/metrics-api/metrics-api.go
goos:
Expand Down
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM centos:7.4.1708
ARG BINARY=./metrics
EXPOSE 3000

COPY ${BINARY} /opt/metrics
ENTRYPOINT ["/opt/metrics"]
54 changes: 40 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# AeroGear Metrics API

**Please note this repo is a suggested implementation.** We have not agreed on implementing the service with Golang.

This is the server component of the AeroGear metrics service. It is a RESTful API that allows mobile clients to send metrics data which will get stored in a PostgreSQL database. The service is written in Golang.
This is the server component of the AeroGear Metrics Service. It is a RESTful API that allows mobile clients to send metrics data which will get stored in a PostgreSQL database. The service is written in [Golang](https://golang.org/).

## Prerequisites

* [Install Golang](https://golang.org/doc/install)
* [Install the dep package manager](https://golang.github.io/dep/docs/installation.html)
* [Install Docker and Docker Compose](https://docs.docker.com/compose/install/)

## Setup and Build
See the [Contributing Guide](./CONTRIBUTING.md) for more information.


## Local development setup and building

This section documents how to setup a local development environment, if you only wish to run the project with minimal setup, check the [Containerized building and running](#containerized-building-and-running) section.

In Go, projects are typically kept in a [workspace](https://golang.org/doc/code.html#Workspaces) that follows a very specific architecture. Before cloning this repo, be sure you have a `GOPATH` env var that points to your workspace folder, say:

Expand All @@ -35,38 +38,47 @@ It is also possible to build the binaries by simply running:
make build
```

## How to Run
## Running the project

In two different terminals:
### Containerized building and running

1. Start the PostgreSQL container using `docker-compose up`:
You can use [Docker Compose](https://docs.docker.com/compose/) to run the project without requiring any extra setup other than a working docker installation.

```
docker-compose -f deployments/docker/docker-compose.yml up
docker-compose up
```

2. Start the server app using `go run`:
This will run both the `db` and `api` services. If you wish to run only the database use `docker-compose up db`.

The setup targets local development and testing, as such it utilizes the host's TCP ports 3000 for the API service and postgres' default port 5432.

This means that these ports could be in use if you have another postgres instance running or other test web servers.

### Running from source

Utilize the `go run` command to transparently compile and run the project:

```
go run cmd/metrics-api/metrics-api.go
```

You can verify it's running:
You can verify the server is running by GET-ing its health check endpoint:
```
curl http://localhost:3000/healthz
```

The default configuration will allow the application to connect to the PostgreSQL container.

## Docker Build
### Running a locally-built binary in a container

Simply run the following:
In order to run a locally-built binary in a container utilize the main `Dockerfile` via :

```
cd deployments/docker
docker build -t aerogear/aerogear-metrics-api .
make docker_build
```

This will copy a binary from the default output location for `make build_linux` command, and build an image from it.

## Release

Build and publish to github releases using `goreleaser`, see `.goreleaser.yml` for configuration.
Expand All @@ -82,3 +94,17 @@ And make the release:
```
make release
```

### Releasing a built container

This repository also contains a set of commands to push the built container images to the `aerogear` docker hub organization.

These commands should preferrably run in a CD server, but are documented here for completeness and when required to be run locally

```
# Add your dockerhub credentials to the environment variables
DOCKERHUB_USERNAME={} DOCKERHUB_PASSWORD={} make docker_push_master
# Manually add the tag information that would otherwise come from CI server metadata
RELEASE_TAG=v0.0.1 make docker_push_release
```
17 changes: 0 additions & 17 deletions deployments/docker/Dockerfile

This file was deleted.

8 changes: 0 additions & 8 deletions deployments/docker/docker-compose.yml

This file was deleted.

25 changes: 25 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: '3'

services:
db:
image: registry.access.redhat.com/rhscl/postgresql-96-rhel7:latest
ports:
- "5432:5432"
environment:
POSTGRESQL_PASSWORD: postgres
POSTGRESQL_USER: postgresql
POSTGRESQL_DATABASE: aerogear_mobile_metrics
api:
build:
context: .
args:
BINARY: ./dist/linux_amd64/metrics
environment:
PGHOST: db
PGUSER: postgresql
PGPASSWORD: postgres
PGDATABASE: aerogear_mobile_metrics
ports:
- "3000:3000"
depends_on:
- db
44 changes: 35 additions & 9 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ PACKAGES ?= $(shell sh -c "find $(TOP_SRC_DIRS) -name \\*_test.go \
-exec dirname {} \\; | sort | uniq")
BIN_DIR := $(GOPATH)/bin
SHELL = /bin/bash
BINARY = metrics
BINARY ?= metrics

# This follows the output format for goreleaser
BINARY_LINUX_64 = ./dist/linux_amd64/metrics

DOCKER_LATEST_TAG = aerogear/aerogear-metrics-api:latest
DOCKER_MASTER_TAG = aerogear/aerogear-metrics-api:master
RELEASE_TAG ?= $(CIRCLE_TAG)
DOCKER_RELEASE_TAG = aerogear/aerogear-metrics-api:$(RELEASE_TAG)

LDFLAGS=-ldflags "-w -s -X main.Version=${TAG}"

Expand All @@ -13,15 +21,24 @@ setup:
dep ensure

.PHONY: build
build: setup build_binary
build: setup
go build -o $(BINARY) ./cmd/metrics-api/metrics-api.go

.PHONY: build_binary_linux
build_binary_linux:
env GOOS=linux GOARCH=amd64 go build -o $(BINARY) ./cmd/metrics-api/metrics-api.go
.PHONY: build_linux
build_linux: setup
env GOOS=linux GOARCH=amd64 go build -o $(BINARY_LINUX_64) ./cmd/metrics-api/metrics-api.go

.PHONY: build_binary
build_binary:
go build -o $(BINARY) ./cmd/metrics-api/metrics-api.go
.PHONY: docker_build
docker_build: build_linux
docker build -t $(DOCKER_LATEST_TAG) --build-arg BINARY=$(BINARY_LINUX_64) .

.PHONY: docker_build_release
docker_build_release:
docker build -t $(DOCKER_LATEST_TAG) -t $(DOCKER_RELEASE_TAG) --build-arg BINARY=$(BINARY_LINUX_64) .

.PHONY: docker_build_master
docker_build_master:
docker build -t $(DOCKER_MASTER_TAG) --build-arg BINARY=$(BINARY_LINUX_64) .

.PHONY: test-unit
test-unit:
Expand Down Expand Up @@ -65,4 +82,13 @@ clean:
release: setup
goreleaser --rm-dist

.PHONY: build
.PHONY: docker_push_release
docker_push_release:
docker login -u $(DOCKERHUB_USERNAME) -p $(DOCKERHUB_PASSWORD)
docker push $(DOCKER_LATEST_TAG)
docker push $(DOCKER_RELEASE_TAG)

.PHONY: docker_push_master
docker_push_master:
docker login -u $(DOCKERHUB_USERNAME) -p $(DOCKERHUB_PASSWORD)
docker push $(DOCKER_MASTER_TAG)
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func getEnv(key string, defaultVal string) string {
func GetConfig() map[string]string {
return map[string]string{
"DBHost": getEnv("PGHOST", "localhost"),
"DBUser": getEnv("PGUSER", "postgres"),
"DBUser": getEnv("PGUSER", "postgresql"),
"DBPassword": getEnv("PGPASSWORD", "postgres"),
"DBName": getEnv("PGDATABASE", "aerogear_mobile_metrics"),
"SSLMode": getEnv("PGSSLMODE", "disable"),
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestGetConfig(t *testing.T) {
expected := map[string]string{
"DBHost": "localhost",
"DBUser": "postgres",
"DBUser": "postgresql",
"DBPassword": "postgres",
"DBName": "aerogear_mobile_metrics",
"SSLMode": "disable",
Expand Down Expand Up @@ -50,7 +50,7 @@ func TestGetConfigCustomEnvVariables(t *testing.T) {
func TestGetConfigEmptyEnvVariables(t *testing.T) {
expected := map[string]string{
"DBHost": "localhost",
"DBUser": "postgres",
"DBUser": "postgresql",
"DBPassword": "postgres",
"DBName": "aerogear_mobile_metrics",
"SSLMode": "disable",
Expand Down

0 comments on commit a157d8e

Please sign in to comment.