2022-02-11 16:47:10 +00:00
|
|
|
# Parse version from latest git semver tag
|
|
|
|
GTAG=$(shell git describe --tags --match v*.*.* 2>/dev/null || git rev-parse --short HEAD 2>/dev/null)
|
|
|
|
TAG ?= $(shell echo $(GTAG) | awk -F '-' '{ print $$1 "-" $$2 }' | sed -e 's/-$$//')
|
2022-10-11 12:42:07 +00:00
|
|
|
ARCH := amd64
|
2022-02-11 16:47:10 +00:00
|
|
|
|
2022-05-24 14:53:55 +00:00
|
|
|
# EXTRA_TAGS supposed to be set at the caller, eg. $(shell echo $(TAG) | awk -F '.' '{ print $$1 "." $$2 }')
|
|
|
|
|
2022-10-11 12:42:07 +00:00
|
|
|
ifneq ($(TRIVY_REMOTE),)
|
|
|
|
TRIVY_OPTS := --server $(TRIVY_REMOTE)
|
2022-02-11 16:47:10 +00:00
|
|
|
endif
|
|
|
|
|
2022-10-11 12:42:07 +00:00
|
|
|
.PHONY: build test scan push clean ecr-login
|
2022-02-11 16:47:10 +00:00
|
|
|
|
|
|
|
all: test
|
|
|
|
|
|
|
|
build:
|
2022-10-11 12:42:07 +00:00
|
|
|
@docker image exists $(REGISTRY)/$(IMAGE):$(TAG)-$(ARCH) || \
|
|
|
|
docker build --rm -t $(REGISTRY)/$(IMAGE):$(TAG)-$(ARCH) --build-arg TAG=$(TAG) --build-arg ARCH=$(ARCH) --platform linux/$(ARCH) .
|
2022-02-11 16:47:10 +00:00
|
|
|
|
|
|
|
test: build rm-test-image
|
2022-02-11 22:21:47 +00:00
|
|
|
@test -f Dockerfile.test && \
|
2022-10-11 12:42:07 +00:00
|
|
|
{ docker build --rm -t $(REGISTRY)/$(IMAGE):$(TAG)-test --from=$(REGISTRY)/$(IMAGE):$(TAG) -f Dockerfile.test --platform linux/$(ARCH) . && \
|
|
|
|
docker 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
|
|
|
|
|
|
|
scan: build
|
2022-10-11 12:42:07 +00:00
|
|
|
@echo "Scanning $(REGISTRY)/$(IMAGE):$(TAG)-$(ARCH) using Trivy $(TRIVY_REMOTE)"
|
|
|
|
@trivy image $(TRIVY_OPTS) $(REGISTRY)/$(IMAGE):$(TAG)-$(ARCH)
|
2022-02-11 16:47:10 +00:00
|
|
|
|
2022-10-11 12:42:07 +00:00
|
|
|
# We create new manifest and add TAG-ARCH image
|
|
|
|
# if manigest exists already, get it and add TAG-ARCH to eg. add arm64 to existing amd64
|
|
|
|
push: ecr-login
|
|
|
|
@for t in $(TAG) latest $(EXTRA_TAGS); \
|
|
|
|
do echo "creating and pushing: $$t"; \
|
|
|
|
docker tag $(REGISTRY)/$(IMAGE):$(TAG)-$(ARCH) $(REGISTRY)/$(IMAGE):$${t}-$(ARCH) && \
|
|
|
|
docker push $(REGISTRY)/$(IMAGE):$${t}-$(ARCH); \
|
|
|
|
podman manifest exists $(IMAGE):$$t || podman manifest create $(IMAGE):$$t; \
|
|
|
|
buildah manifest add $(IMAGE):$$t $(REGISTRY)/$(IMAGE):$(TAG)-$(ARCH) && docker manifest push $(IMAGE):$$t $(REGISTRY)/$(IMAGE):$$t; \
|
|
|
|
done
|
|
|
|
|
|
|
|
ecr-login:
|
2022-02-11 16:47:10 +00:00
|
|
|
@aws ecr-public get-login-password --region $(REGION) | docker login --username AWS --password-stdin $(REGISTRY)
|
|
|
|
|
|
|
|
clean: rm-test-image rm-image
|
|
|
|
|
|
|
|
# Delete all untagged images
|
|
|
|
.PHONY: rm-remote-untagged
|
|
|
|
rm-remote-untagged:
|
2022-02-11 22:21:47 +00:00
|
|
|
@echo "Removing all untagged images from $(IMAGE) in $(REGION)"
|
2022-10-11 12:42:07 +00:00
|
|
|
@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) ; \
|
|
|
|
[ -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
|
|
|
|
|
|
|
.PHONY: rm-image
|
|
|
|
rm-image:
|
2022-10-11 12:42:07 +00:00
|
|
|
@test -z "$$(docker image ls -q $(IMAGE):$(TAG)-$(ARCH))" || docker image rm -f $(IMAGE):$(TAG)-$(ARCH) > /dev/null
|
|
|
|
@test -z "$$(docker 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
|
|
|
|
.PHONY: rm-test-image
|
|
|
|
rm-test-image:
|
2022-10-11 12:42:07 +00:00
|
|
|
@test -z "$$(docker image ls -q $(IMAGE):$(TAG)-$(ARCH)-test)" || docker image rm -f $(IMAGE):$(TAG)-$(ARCH)-test > /dev/null
|
|
|
|
@test -z "$$(docker image ls -q $(IMAGE):$(TAG)-$(ARCH)-test)" || echo "Error: Removing test image failed"
|
2022-02-11 16:47:10 +00:00
|
|
|
|
2022-03-23 09:54:49 +00:00
|
|
|
# Convience task during dev of downstream projects
|
|
|
|
.PHONY: ci-pull-upstream
|
|
|
|
ci-pull-upstream:
|
|
|
|
git stash && git subtree pull --prefix .ci ssh://git@git.zero-downtime.net/ZeroDownTime/ci-tools-lib.git master --squash && git stash pop
|
|
|
|
|
2022-05-24 14:53:55 +00:00
|
|
|
.PHONY: create-repo
|
|
|
|
create-repo:
|
|
|
|
aws ecr-public create-repository --repository-name $(IMAGE) --region $(REGION)
|
|
|
|
|
2022-02-11 16:47:10 +00:00
|
|
|
.DEFAULT:
|
|
|
|
@echo "$@ not implemented. NOOP"
|