Squashed '.ci/' changes from cb5faca..5554972

5554972 feat: multi-arch container images
da15d68 feat: handle nothing to cleanup gracefully
01df38b feat: move ecr-login into its own task
ea9c914 chore: test mermaid
19a782e chore: test mermaid
a47929d feat: switch to latest trivy cli syntax

git-subtree-dir: .ci
git-subtree-split: 55549729264a318c4c942957a36e8659553fece0
This commit is contained in:
Stefan Reimer 2022-10-11 14:42:07 +02:00
parent 51fd4d494e
commit f06971948d

View File

@ -1,36 +1,45 @@
# Parse version from latest git semver tag # 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) 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/-$$//') TAG ?= $(shell echo $(GTAG) | awk -F '-' '{ print $$1 "-" $$2 }' | sed -e 's/-$$//')
ARCH := amd64
# EXTRA_TAGS supposed to be set at the caller, eg. $(shell echo $(TAG) | awk -F '.' '{ print $$1 "." $$2 }') # EXTRA_TAGS supposed to be set at the caller, eg. $(shell echo $(TAG) | awk -F '.' '{ print $$1 "." $$2 }')
ifeq ($(TRIVY_REMOTE),) ifneq ($(TRIVY_REMOTE),)
TRIVY_OPTS := image TRIVY_OPTS := --server $(TRIVY_REMOTE)
else
TRIVY_OPTS := client --remote ${TRIVY_REMOTE}
endif endif
.PHONY: build test scan push clean .PHONY: build test scan push clean ecr-login
all: test all: test
build: build:
@docker image exists $(REGISTRY)/$(IMAGE):$(TAG) || \ @docker image exists $(REGISTRY)/$(IMAGE):$(TAG)-$(ARCH) || \
docker build --rm -t $(REGISTRY)/$(IMAGE):$(TAG) --build-arg TAG=$(TAG) . docker build --rm -t $(REGISTRY)/$(IMAGE):$(TAG)-$(ARCH) --build-arg TAG=$(TAG) --build-arg ARCH=$(ARCH) --platform linux/$(ARCH) .
test: build rm-test-image test: build rm-test-image
@test -f Dockerfile.test && \ @test -f Dockerfile.test && \
{ docker build --rm -t $(REGISTRY)/$(IMAGE):$(TAG)-test --from=$(REGISTRY)/$(IMAGE):$(TAG) -f Dockerfile.test . && \ { 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)-test; } || \ docker run --rm --env-host -t $(REGISTRY)/$(IMAGE):$(TAG)-$(ARCH)-test; } || \
echo "No Dockerfile.test found, skipping test" echo "No Dockerfile.test found, skipping test"
scan: build scan: build
@echo "Scanning $(REGISTRY)/$(IMAGE):$(TAG) using Trivy" @echo "Scanning $(REGISTRY)/$(IMAGE):$(TAG)-$(ARCH) using Trivy $(TRIVY_REMOTE)"
@trivy $(TRIVY_OPTS) $(REGISTRY)/$(IMAGE):$(TAG) @trivy image $(TRIVY_OPTS) $(REGISTRY)/$(IMAGE):$(TAG)-$(ARCH)
push: build # 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:
@aws ecr-public get-login-password --region $(REGION) | docker login --username AWS --password-stdin $(REGISTRY) @aws ecr-public get-login-password --region $(REGION) | docker login --username AWS --password-stdin $(REGISTRY)
@for t in $(TAG) latest $(EXTRA_TAGS); do echo "tag and push: $$t"; docker tag $(IMAGE):$(TAG) $(REGISTRY)/$(IMAGE):$$t && docker push $(REGISTRY)/$(IMAGE):$$t; done
clean: rm-test-image rm-image clean: rm-test-image rm-image
@ -38,18 +47,19 @@ clean: rm-test-image rm-image
.PHONY: rm-remote-untagged .PHONY: rm-remote-untagged
rm-remote-untagged: rm-remote-untagged:
@echo "Removing all untagged images from $(IMAGE) in $(REGION)" @echo "Removing all untagged images from $(IMAGE) in $(REGION)"
@aws ecr-public batch-delete-image --repository-name $(IMAGE) --region $(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) @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"
.PHONY: rm-image .PHONY: rm-image
rm-image: rm-image:
@test -z "$$(docker image ls -q $(IMAGE):$(TAG))" || docker image rm -f $(IMAGE):$(TAG) > /dev/null @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))" || echo "Error: Removing image failed" @test -z "$$(docker image ls -q $(IMAGE):$(TAG)-$(ARCH))" || echo "Error: Removing image failed"
# Ensure we run the tests by removing any previous runs # Ensure we run the tests by removing any previous runs
.PHONY: rm-test-image .PHONY: rm-test-image
rm-test-image: rm-test-image:
@test -z "$$(docker image ls -q $(IMAGE):$(TAG)-test)" || docker image rm -f $(IMAGE):$(TAG)-test > /dev/null @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)-test)" || echo "Error: Removing test image failed" @test -z "$$(docker image ls -q $(IMAGE):$(TAG)-$(ARCH)-test)" || echo "Error: Removing test image failed"
# Convience task during dev of downstream projects # Convience task during dev of downstream projects
.PHONY: ci-pull-upstream .PHONY: ci-pull-upstream