2022-02-11 16:47:10 +00:00
|
|
|
# Parse version from latest git semver tag
|
2023-04-25 10:49:36 +00:00
|
|
|
GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
|
|
|
|
GIT_TAG := $(shell git describe --tags --match v*.*.* 2>/dev/null || git rev-parse --short HEAD 2>/dev/null)
|
|
|
|
|
|
|
|
# append branch name to tag if NOT main nor master
|
|
|
|
TAG := $(GIT_TAG)
|
|
|
|
ifeq (,$(filter main master, $(GIT_BRANCH)))
|
|
|
|
ifneq ($(GIT_TAG), $(GIT_BRANCH))
|
|
|
|
TAG = $(GIT_TAG)-$(GIT_BRANCH)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2022-10-27 12:16:35 +00:00
|
|
|
ARCH := amd64
|
2022-11-14 13:12:04 +00:00
|
|
|
ALL_ARCHS := amd64 arm64
|
2023-04-25 10:49:36 +00:00
|
|
|
_ARCH = $(or $(filter $(ARCH),$(ALL_ARCHS)),$(error $$ARCH [$(ARCH)] must be exactly one of "$(ALL_ARCHS)"))
|
2022-07-12 13:13:20 +00:00
|
|
|
|
|
|
|
ifneq ($(TRIVY_REMOTE),)
|
2022-11-14 13:12:04 +00:00
|
|
|
TRIVY_OPTS := --server $(TRIVY_REMOTE)
|
2022-02-11 16:47:10 +00:00
|
|
|
endif
|
|
|
|
|
2022-10-27 12:16:35 +00:00
|
|
|
.SILENT: ; # no need for @
|
|
|
|
.ONESHELL: ; # recipes execute in same shell
|
|
|
|
.NOTPARALLEL: ; # wait for this target to finish
|
|
|
|
.EXPORT_ALL_VARIABLES: ; # send all vars to shell
|
|
|
|
.PHONY: all # All targets are accessible for user
|
|
|
|
.DEFAULT: help # Running Make will run the help target
|
2022-02-11 16:47:10 +00:00
|
|
|
|
2022-10-27 12:16:35 +00:00
|
|
|
help: ## Show Help
|
|
|
|
grep -E '^[a-zA-Z_-]+:.*?## .*$$' .ci/podman.mk | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
|
2022-02-11 16:47:10 +00:00
|
|
|
|
2022-10-27 12:16:35 +00:00
|
|
|
build: ## Build the app
|
2023-04-25 10:49:36 +00:00
|
|
|
buildah build --rm --layers -t $(IMAGE):$(TAG)-$(_ARCH) --build-arg TAG=$(TAG) --build-arg ARCH=$(_ARCH) --platform linux/$(_ARCH) .
|
2022-02-11 16:47:10 +00:00
|
|
|
|
2022-10-27 12:16:35 +00:00
|
|
|
test: rm-test-image ## Execute Dockerfile.test
|
|
|
|
test -f Dockerfile.test && \
|
2023-04-25 10:49:36 +00:00
|
|
|
{ buildah build --rm --layers -t $(REGISTRY)/$(IMAGE):$(TAG)-test --from=$(REGISTRY)/$(IMAGE):$(TAG) -f Dockerfile.test --platform linux/$(_ARCH) . && \
|
|
|
|
podman run --rm --env-host -t $(REGISTRY)/$(IMAGE):$(TAG)-$(_ARCH)-test; } || \
|
2022-02-11 22:21:47 +00:00
|
|
|
echo "No Dockerfile.test found, skipping test"
|
2022-02-11 16:47:10 +00:00
|
|
|
|
2022-10-27 12:16:35 +00:00
|
|
|
scan: ## Scan image using trivy
|
2023-04-25 10:49:36 +00:00
|
|
|
echo "Scanning $(IMAGE):$(TAG)-$(_ARCH) using Trivy $(TRIVY_REMOTE)"
|
|
|
|
trivy image $(TRIVY_OPTS) localhost/$(IMAGE):$(TAG)-$(_ARCH)
|
2022-02-11 16:47:10 +00:00
|
|
|
|
2022-11-14 13:12:04 +00:00
|
|
|
# first tag and push all actual images
|
|
|
|
# create new manifest for each tag and add all available TAG-ARCH before pushing
|
|
|
|
push: ecr-login ## push images to registry
|
|
|
|
for t in $(TAG) latest $(EXTRA_TAGS); do \
|
2023-04-25 10:49:36 +00:00
|
|
|
echo "Tagging image with $(REGISTRY)/$(IMAGE):$${t}-$(ARCH)"
|
|
|
|
buildah tag $(IMAGE):$(TAG)-$(_ARCH) $(REGISTRY)/$(IMAGE):$${t}-$(_ARCH); \
|
2022-11-15 12:03:20 +00:00
|
|
|
buildah manifest rm $(IMAGE):$$t || true; \
|
|
|
|
buildah manifest create $(IMAGE):$$t; \
|
2022-11-14 13:12:04 +00:00
|
|
|
for a in $(ALL_ARCHS); do \
|
2022-11-15 12:03:20 +00:00
|
|
|
buildah manifest add $(IMAGE):$$t $(REGISTRY)/$(IMAGE):$(TAG)-$$a; \
|
2022-11-14 13:12:04 +00:00
|
|
|
done; \
|
2023-04-25 10:49:36 +00:00
|
|
|
echo "Pushing manifest $(IMAGE):$$t"
|
2022-11-15 12:03:20 +00:00
|
|
|
buildah manifest push --all $(IMAGE):$$t docker://$(REGISTRY)/$(IMAGE):$$t; \
|
2022-10-27 12:16:35 +00:00
|
|
|
done
|
2022-02-11 16:47:10 +00:00
|
|
|
|
2022-10-27 12:16:35 +00:00
|
|
|
ecr-login: ## log into AWS ECR public
|
2022-11-15 12:03:20 +00:00
|
|
|
aws ecr-public get-login-password --region $(REGION) | podman login --username AWS --password-stdin $(REGISTRY)
|
2022-09-15 09:34:27 +00:00
|
|
|
|
2022-10-27 12:16:35 +00:00
|
|
|
clean: rm-test-image rm-image ## delete local built container and test images
|
2022-02-11 16:47:10 +00:00
|
|
|
|
2022-10-27 12:16:35 +00:00
|
|
|
rm-remote-untagged: ## delete all remote untagged images
|
|
|
|
echo "Removing all untagged images from $(IMAGE) in $(REGION)"
|
|
|
|
IMAGE_IDS=$$(for image in $$(aws ecr-public describe-images --repository-name $(IMAGE) --region $(REGION) --output json | jq -r '.imageDetails[] | select(.imageTags | not ).imageDigest'); do echo -n "imageDigest=$$image "; done) ; \
|
2022-11-14 13:12:04 +00:00
|
|
|
[ -n "$$IMAGE_IDS" ] && aws ecr-public batch-delete-image --repository-name $(IMAGE) --region $(REGION) --image-ids $$IMAGE_IDS || echo "No image to remove"
|
2022-02-11 16:47:10 +00:00
|
|
|
|
|
|
|
rm-image:
|
2023-04-25 10:49:36 +00:00
|
|
|
test -z "$$(podman image ls -q $(IMAGE):$(TAG)-$(_ARCH))" || podman image rm -f $(IMAGE):$(TAG)-$(_ARCH) > /dev/null
|
|
|
|
test -z "$$(podman image ls -q $(IMAGE):$(TAG)-$(_ARCH))" || echo "Error: Removing image failed"
|
2022-02-11 16:47:10 +00:00
|
|
|
|
|
|
|
# Ensure we run the tests by removing any previous runs
|
|
|
|
rm-test-image:
|
2023-04-25 10:49:36 +00:00
|
|
|
test -z "$$(podman image ls -q $(IMAGE):$(TAG)-$(_ARCH)-test)" || podman image rm -f $(IMAGE):$(TAG)-$(_ARCH)-test > /dev/null
|
|
|
|
test -z "$$(podman image ls -q $(IMAGE):$(TAG)-$(_ARCH)-test)" || echo "Error: Removing test image failed"
|
2022-02-11 16:47:10 +00:00
|
|
|
|
2022-10-27 12:16:35 +00:00
|
|
|
ci-pull-upstream: ## pull latest shared .ci subtree
|
2022-03-23 09:54:49 +00:00
|
|
|
git stash && git subtree pull --prefix .ci ssh://git@git.zero-downtime.net/ZeroDownTime/ci-tools-lib.git master --squash && git stash pop
|
|
|
|
|
2022-10-27 12:16:35 +00:00
|
|
|
create-repo: ## create new AWS ECR public repository
|
2022-07-12 13:13:20 +00:00
|
|
|
aws ecr-public create-repository --repository-name $(IMAGE) --region $(REGION)
|