Skip to content

Instantly share code, notes, and snippets.

@apostergiou
Forked from ahawkins/Makefile
Created September 12, 2018 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apostergiou/e6183befd228086ac7cfca6be1740f98 to your computer and use it in GitHub Desktop.
Save apostergiou/e6183befd228086ac7cfca6be1740f98 to your computer and use it in GitHub Desktop.
Example Makefile
RUBY_IMAGE:=$(shell grep FROM Dockerfile | cut -f2 -d' ')
DYNAMODB_IMAGE:=dynamodb:latest # original value ommitted
APP_IMAGE:=inventory_service/app
TAG:=$(shell git rev-parse --short HEAD)
REGISTRY:=example.registry.com # original value omitted
.DEFAULT_GOAL:= build
# NOTE: the actual value for market is not important, however it's required to
# start any process. It may also not be used for all things this variable might
# be used in. It's easier to add it here, then to create a separate variable to
# hold this value that must be set for all docker things.
DOCKER_RUN:=docker run --rm -it -e MARKET=bikroy
APP:=tmp/app
ENVIRONMENT:=tmp/environment
DOCKER_IMAGES:=tmp/docker_images
$(DOCKER_IMAGES):
mkdir -p $(@D)
touch $@
Gemfile.lock: Gemfile
docker run --rm -t -v $(CURDIR):/data \
$(RUBY_IMAGE) \
bundle package --all --gemfile /data/Gemfile
touch $@
.PHONY: thrift-gems
thrift-gems: $(APP)
rm -rf vendor/cache/thrift_server-ruby-*
$(DOCKER_RUN) -v $(CURDIR):/app \
$(APP_IMAGE) script/bundle-update thrift_server
$(APP): Dockerfile Gemfile.lock $(DOCKER_IMAGES)
docker build -t $(APP_IMAGE) .
docker inspect -f '{{.Id}}' $(APP_IMAGE) >> $(DOCKER_IMAGES)
mkdir -p $(@D)
touch $@
.PHONY: build
build: $(APP)
$(ENVIRONMENT): $(APP)
mkdir -p $(@D)
docker run -d --name test_dynamodb $(DYNAMODB_IMAGE) >> $@ && sleep 1
docker run -d --name dynamodb $(DYNAMODB_IMAGE) >> $@ && sleep 1
docker run -d --name server \
--link dynamodb:dynamodb \
-e DYNAMODB_ENDPOINT=http://dynamodb:8000 \
-e MARKET=efritin \
$(APP_IMAGE) server --bootstrap >> $@ && sleep 5
.PHONY: environment
environment: $(ENVIRONMENT)
# Quick tests in development
.PHONY: test
test: $(ENVIRONMENT)
ifdef FILE
$(DOCKER_RUN) -v $(CURDIR):/app \
--link test_dynamodb:dynamodb \
-e DYNAMODB_ENDPOINT=http://dynamodb:8000 \
$(APP_IMAGE) ruby $(FILE)
else
$(DOCKER_RUN) -v $(CURDIR):/app \
--link test_dynamodb:dynamodb \
-e DYNAMODB_ENDPOINT=http://dynamodb:8000 \
$(APP_IMAGE) bundle exec rake test
endif
# Run tests with build image intsead of mounting code inside
.PHONY: test-app
test-app: $(ENVIRONMENT)
$(info --- Running application tests)
$(DOCKER_RUN) \
--link test_dynamodb:dynamodb \
-e DYNAMODB_ENDPOINT=http://dynamodb:8000 \
$(APP_IMAGE) bundle exec rake test
# Run the monkey client against the server for a specific duration.
# The duration and thread values are not chosen for any specific resason.
# We may create specific configurations is we come across regressions.
# For now they are enough to execute a client and server in somewhat
# realistic conditions (multi threaded, ongoing traffic etc).
MONKEY_RUN:=$(DOCKER_RUN) --link server:server $(APP_IMAGE) monkey-client server 9090
.PHONY: test-monkey
test-monkey: $(ENVIRONMENT)
$(info --- Running monkey tests)
$(MONKEY_RUN) --calls 50 --threads 5
.PHONY: test-apollo
test-apollo: apollo.json
$(info --- Validating apollo.json)
apollo -2 validate
UTIL_RUN:=$(DOCKER_RUN) -v $(CURDIR):/app \
--link test_dynamodb:dynamodb \
-e DYNAMODB_ENDPOINT=http://dynamodb:8000 \
$(APP_IMAGE) util
.PHONY: test-util
# NOTE: reset is last because it puts everything back into an operating state.
# If the environment breaks, simply clean and bring it back up.
test-util: $(ENVIRONMENT)
$(info --- Running util tests)
$(UTIL_RUN) counters
$(UTIL_RUN) tables
$(UTIL_RUN) setup
$(UTIL_RUN) teardown -f
$(UTIL_RUN) reset -f
.PHONY: test-ci
test-ci: test-app test-monkey test-util test-apollo
.PHONY: push
push: $(APP) $(DOCKER_IMAGES)
ifndef TAG
$(error "TAG variable missing!")
endif
ifndef REGISTRY
$(error "REGISTRY missing!")
endif
docker tag -f $(APP_IMAGE) $(REGISTRY)/$(APP_IMAGE):$(TAG)
@echo "$(REGISTRY)/$(APP_IMAGE):$(TAG)" >> $(DOCKER_IMAGES)
docker push $(REGISTRY)/$(APP_IMAGE)
.PHONY: deploy
deploy: push
apollo deploy -e staging -t $(TAG)
.PHONY: clean
clean:
mkdir -p $(dir $(ENVIRONMENT))
touch $(ENVIRONMENT)
-cat $(ENVIRONMENT) | xargs --no-run-if-empty docker stop 2> /dev/null
-cat $(ENVIRONMENT) | xargs --no-run-if-empty docker rm -v 2> /dev/null
rm -f $(ENVIRONMENT) $(APP)
.PHONY: clean-docker
clean-docker: $(DOCKER_IMAGES)
-sort -u $< | xargs --no-run-if-empty docker rmi -f 2> /dev/null
rm -rf $<
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment