Skip to content

Instantly share code, notes, and snippets.

@wdhowe
Last active April 11, 2021 22:10
Show Gist options
  • Save wdhowe/b9d51de2f4d515edb549d426bd6f8beb to your computer and use it in GitHub Desktop.
Save wdhowe/b9d51de2f4d515edb549d426bd6f8beb to your computer and use it in GitHub Desktop.
An example Makefile for Docker and Leiningen.
.PHONY: build test deploy
##-- Environment Variables --#
# Image and container registry
IMAGE_NAME := my-image-name
IMAGE_PATH := mygroup/myproject
REGISTRY := registry-url:port
IMAGE := $(REGISTRY)/$(IMAGE_PATH)/$(IMAGE_NAME)
# Get current branch and transform '/' to '-'
BRANCH := $(or $(CI_COMMIT_REF_NAME), `git rev-parse --abbrev-ref HEAD`)
BRANCH := $(shell echo $(BRANCH) | tr / -)
# Retrieve first 7 characters of current commit hash
SHORT_HASH := `git rev-parse --short HEAD`
# Docker image tag for a local build
BUILD_TAG := $(IMAGE):build
# Docker image tag that will be pushed to the registry
TAG := $(IMAGE):$(BRANCH)-$(SHORT_HASH)
##-- Main Makefile Targets --##
# build - build a local docker image and tag the image
build: docker-build docker-tag
# test - run unit tests
test:
lein test
# deploy - push image into registry
deploy: docker-login docker-push
##-- Extra Makefile Targets --##
# clean - cleanup project space
clean:
lein clean
# docs - build documentation
docs:
lein codox
# shell - open a shell on the build container
shell:
docker run --interactive --tty --rm --name testing $(BUILD_TAG) /bin/bash
##-- Lein Targets --##
# build-lein - build the project uberjar
lein-build:
lein deps
lein uberjar
# deploy-lein - deploy the project to clojars
deploy-lein: lein-build test
lein deploy clojars
##-- Docker Helper Targets --##
# docker-build - build an image with a local build tag
docker-build:
docker build --tag $(BUILD_TAG) --rm --compress $(PWD)
# docker-tag - prep local built image with tag for pushing to registry
docker-tag:
docker tag $(BUILD_TAG) $(TAG)
# docker-login - login to registry using vars if available,
# otherwise interactive login.
docker-login:
@$(if $(and $(CI_REGISTRY_USER), $(CI_REGISTRY_PASSWORD)), \
docker login -u $(CI_REGISTRY_USER) \
-p $(CI_REGISTRY_PASSWORD) \
$(CI_REGISTRY), \
docker login $(REGISTRY))
# docker-push - push image to registry
docker-push:
docker push $(TAG)
##-- Debug --##
# debug - display all environment variables
debug:
@echo "IMAGE: $(IMAGE)"
@echo "BUILD_TAG: $(BUILD_TAG)"
@echo "TAG: $(TAG)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment