Skip to content

Instantly share code, notes, and snippets.

@moracabanas
Last active December 24, 2022 05:11
Show Gist options
  • Save moracabanas/93301a4020c362a7ed59b62ea6136cb5 to your computer and use it in GitHub Desktop.
Save moracabanas/93301a4020c362a7ed59b62ea6136cb5 to your computer and use it in GitHub Desktop.
Docker build, tag and push helper script
# Docker build helper
-include .make
REPO := $(shell grep REPO .make 2>/dev/null | cut -d '=' -f2)
IMAGE := $(shell grep IMAGE .make 2>/dev/null | cut -d '=' -f2)
VERSION := $(shell grep VERSION .make 2>/dev/null | cut -d '=' -f2)
TIMESTAMP := $(shell date +%Y.%m.%d-%H.%M.%S)
TAG := $(REPO)/$(IMAGE):$(VERSION)-$(TIMESTAMP)
LATEST := $(REPO)/$(IMAGE):latest
.PHONY: help
help:
@echo "Please use 'make <target>' where <target> is one of the following:"
@echo " help to print this help (the default)"
@echo " build to build a docker image with semantic version and timestamp"
@echo " push to push the latest image and semantic versioned image timestamp"
@echo " prune to prune all tagged $(IMAGE) images"
@echo " test to print an example of genetared image names"
@echo " init to generate a .make file with placeholders for the REPO, IMAGE, and VERSION variables"
@echo $(REPO) $(IMAGE) $(VERSION)
.PHONY: build
build: check_makefile
@echo "Building your images..."
docker build --build-arg PB_VERSION=$(VERSION) . -t $(TAG)
docker tag $(TAG) $(LATEST)
.PHONY: push
push: check_makefile
@echo "Pushing your images..."
docker login
docker push $(TAG)
docker push $(LATEST)
.PHONY: test
test: check_makefile
@echo "Example of names generated:"
@echo " $(TAG)"
@echo " $(LATEST)"
.PHONY: prune
prune: check_makefile
@echo "Pruning images prefixed with $(REPO)/$(IMAGE)..."
@docker images --filter "reference=$(REPO)/$(IMAGE)*" -q | xargs -r docker rmi -f
.PHONY: init
init:
@echo "Generating .make file..."
@echo "REPO=\nIMAGE=\nVERSION=" > .make
# checkings TODO check .make exist and variables are not empty
check_makefile:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment