Skip to content

Instantly share code, notes, and snippets.

@yusufsyaifudin
Last active February 27, 2024 14:01
Show Gist options
  • Save yusufsyaifudin/1511fe7f649d0b1d21d7a0281196e213 to your computer and use it in GitHub Desktop.
Save yusufsyaifudin/1511fe7f649d0b1d21d7a0281196e213 to your computer and use it in GitHub Desktop.
Docker Compose: Redis, Consul, Mongo, Jaeger with Health check. Sample of gitlab-ci.yml for running Go test (integration test or unit test)
# Inspiration: https://b.agilob.net/test-coverage-in-gitlab-ci-in-a-golang-project
# Navigate to: "Settings" -> "CI/CD" -> "Expand" next to "General pipeline settings"
# 1. Add ^coverage:\s(\d+(?:\.\d+)?%) tas your regex in "Test coverage parsing"
# 2. Go to "Pipeline status" to get the badges code
# This file is a template, and might need editing before it works on your project.
image: golang:1.13.4-alpine
services:
- docker:stable-dind # only docker daemon, so we need to install docker cli and docker compose in separate script
variables:
# Please edit to your GitLab project
REPO_NAME: gitlab.com/username/project-name
DOCKER_HOST: tcp://docker:2375
# The problem is that to be able to use go get, one needs to put
# the repository in the $GOPATH. So for example if your gitlab domain
# is gitlab.com, and that your repository is namespace/project, and
# the default GOPATH being /go, then you'd need to have your
# repository in /go/src/gitlab.com/namespace/project
# Thus, making a symbolic link corrects this.
before_script:
- mkdir -p $GOPATH/src/$REPO_NAME
- ln -svf $CI_PROJECT_DIR/* $GOPATH/src/$REPO_NAME
- cd $GOPATH/src/$REPO_NAME
- apk add --no-cache bash git py-pip python-dev libffi-dev openssl-dev gcc libc-dev make
- apk add --update --no-cache docker openrc # https://github.com/gliderlabs/docker-alpine/issues/183
- pip install docker-compose
- docker-compose -f docker-compose-dev.yml up -d
- rc-update add docker boot # To start the Docker daemon at boot
- sleep 90 # sleep 1.5 minutes, assume docker already started
- docker inspect -f {{.State.Health.Status}} consul # consul
- docker inspect -f {{.State.Health.Status}} jaeger # jaeger
- docker inspect -f {{.State.Health.Status}} redis_cluster # redis
- docker inspect -f {{.State.Health.Status}} mongodb # mongo
after_script:
- docker-compose -f docker-compose-dev.yml down
stages:
- test
format:
stage: test
script:
- make install-dep && make test
version: '3.5'
services:
# access jaeger UI at localhost:16686
jaeger:
image: jaegertracing/all-in-one:1.7
container_name: jaeger
restart: always
healthcheck:
test: ["CMD", "curl", "-X", "GET", "localhost:14269"] # jaeger collector health check ping
interval: 1s
timeout: 3s
retries: 60
ports:
- 5775:5775/udp
- 6831:6831/udp
- 6832:6832/udp
- 5778:5778
- 16686:16686
- 16687:16687
- 14268:14268
- 14269:14269
- 9411:9411
environment:
- COLLECTOR_ZIPKIN_HTTP_PORT=9411
# access consul UI at localhost:8500
consul:
image: bitnami/consul:1
container_name: consul
restart: always
healthcheck:
# return information about the status of the Consul cluster https://www.consul.io/api/status.html
test: ["CMD", "curl", "-X", "GET", "localhost:8500/v1/status/leader"]
interval: 1s
timeout: 3s
retries: 60
volumes:
- ./logs/consul_data:/bitnami
ports:
- '8300:8300'
- '8301:8301'
- '8301:8301/udp'
- '8500:8500'
- '8600:8600'
- '8600:8600/udp'
mongodb:
image: mongo:4.2.0-bionic
container_name: mongodb
restart: always
healthcheck:
test: echo 'db.stats().ok' | mongo localhost:27017/test --quiet
interval: 10s
timeout: 10s
retries: 5
start_period: 40s
environment:
- MONGO_DATA_DIR=/data/db
- MONGO_LOG_DIR=/dev/null
- MONGO_INITDB_DATABASE=gamora
ports:
- 27017:27017
command: mongod
volumes:
- ./logs/mongodb:/data/db
# see https://github.com/riandyrn/docker-redis-cluster
redis_cluster:
image: riandyrn/rediscluster:latest
container_name: redis_cluster
restart: always
healthcheck:
test: ["CMD", "redis-cli", "-p", "7000", "ping"]
interval: 30s
timeout: 10s
retries: 3
environment:
- IP=0.0.0.0
ports:
- "7000-7005:7000-7005"
volumes:
consul_data:
driver: local
PACKAGE_NAME := gitlab.com/username/project-name
COMMIT_ID := `git rev-parse --short HEAD`
build: install-dep generate-doc test
rm -f out
CGO_ENABLED=0 GOOS=darwin go build -a -installsuffix cgo -o out/project-name.$(COMMIT_ID).osx $(PACKAGE_NAME)/cmd/api
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o out/project-name.$(COMMIT_ID) $(PACKAGE_NAME)/cmd/api
compile: install-dep generate-doc
rm -f out
CGO_ENABLED=0 GOOS=darwin go build -a -installsuffix cgo -o out/project-name.$(COMMIT_ID).osx $(PACKAGE_NAME)/cmd/api
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o out/project-name.$(COMMIT_ID) $(PACKAGE_NAME)/cmd/api
test:
@echo "=================================================================================="
@echo "Coverage Test"
@echo "=================================================================================="
go fmt ./... && go test -coverprofile coverage.cov -cover -race ./... # use -v for verbose
@echo "\n"
@echo "=================================================================================="
@echo "All Package Coverage"
@echo "=================================================================================="
go tool cover -func coverage.cov
install-dep:
go get -v -u github.com/golang/dep/cmd/dep
go get -v -u github.com/swaggo/swag/cmd/swag
go get -v -u github.com/go-bindata/go-bindata/...
dep ensure -v
generate-doc:
swag init -g server/server.go
go-bindata -ignore docs.go -ignore swagger.go -pkg docs -o docs/swagger.go docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment