build: Tag Docker image with version on main, SHA otherwise

Derive the Docker tag from the crate version when building on the
main branch and fall back to the short commit SHA on any other
branch. Update the run, clean, shell and push targets to use the
same tag instead of the implicit latest tag.
This commit is contained in:
2026-08-28 15:23:26 +02:00
parent 64f30a8494
commit 94df2c8d4f
+18 -8
View File
@@ -3,6 +3,16 @@
# Variables # Variables
IMAGE_NAME = pop-imap-importer IMAGE_NAME = pop-imap-importer
DOCKER_PLATFORM = linux/amd64 DOCKER_PLATFORM = linux/amd64
VERSION = $(shell grep -m1 '^version' Cargo.toml | cut -d'"' -f2)
GIT_BRANCH = $(shell git rev-parse --abbrev-ref HEAD)
GIT_SHA = $(shell git rev-parse --short HEAD)
# Docker tag: version number on main branch, short commit SHA otherwise
ifeq ($(GIT_BRANCH),main)
DOCKER_TAG = $(VERSION)
else
DOCKER_TAG = $(GIT_SHA)
endif
# Default target # Default target
.PHONY: help .PHONY: help
@@ -36,29 +46,29 @@ test: ## Run all tests
# Docker targets # Docker targets
.PHONY: build .PHONY: build
build: ## Build the Docker image for linux/amd64 platform build: ## Build the Docker image for linux/amd64 platform (tagged with version on main, commit SHA otherwise)
docker build --platform=$(DOCKER_PLATFORM) -t $(IMAGE_NAME) . docker build --platform=$(DOCKER_PLATFORM) -t $(IMAGE_NAME):$(DOCKER_TAG) .
.PHONY: run .PHONY: run
run: ## Run the Docker container run: ## Run the Docker container
docker run --platform=$(DOCKER_PLATFORM) --rm -it $(IMAGE_NAME) docker run --platform=$(DOCKER_PLATFORM) --rm -it $(IMAGE_NAME):$(DOCKER_TAG)
.PHONY: run-with-env .PHONY: run-with-env
run-with-env: ## Run the Docker container with a custom .env file run-with-env: ## Run the Docker container with a custom .env file
docker run --platform=$(DOCKER_PLATFORM) --rm -it -v $(PWD)/.env:/app/.env $(IMAGE_NAME) docker run --platform=$(DOCKER_PLATFORM) --rm -it -v $(PWD)/.env:/app/.env $(IMAGE_NAME):$(DOCKER_TAG)
.PHONY: clean .PHONY: clean
clean: ## Remove the Docker image clean: ## Remove the Docker image
docker rmi $(IMAGE_NAME) docker rmi $(IMAGE_NAME):$(DOCKER_TAG)
.PHONY: push .PHONY: push
push: ## Push the Docker image to a registry (requires REPOSITORY variable) push: ## Push the Docker image to a registry (requires REPOSITORY variable)
ifndef REPOSITORY ifndef REPOSITORY
$(error REPOSITORY is not set. Please set it with: make push REPOSITORY=your-repo/image-name) $(error REPOSITORY is not set. Please set it with: make push REPOSITORY=your-repo/image-name)
endif endif
docker tag $(IMAGE_NAME) $(REPOSITORY):latest docker tag $(IMAGE_NAME):$(DOCKER_TAG) $(REPOSITORY):$(DOCKER_TAG)
docker push $(REPOSITORY):latest docker push $(REPOSITORY):$(DOCKER_TAG)
.PHONY: shell .PHONY: shell
shell: ## Run a shell in the Docker container shell: ## Run a shell in the Docker container
docker run --platform=$(DOCKER_PLATFORM) --rm -it $(IMAGE_NAME) sh docker run --platform=$(DOCKER_PLATFORM) --rm -it $(IMAGE_NAME):$(DOCKER_TAG) sh