Skip to content

Commit

Permalink
[#320] Optimized Docker image build and push process for frontend and…
Browse files Browse the repository at this point in the history
… backend

This commit optimizes the Docker image build and push process for the
frontend and backend components of the application. It introduces a
conditional check to ensure that Docker images are only built and pushed
to the Amazon Elastic Container Registry (ECR) if there have been
changes in their respective source directories. This is achieved by
adding a helper function, check_image_on_ecr, which checks for the
existence of the Docker image on ECR with the specified tag. If the
image already exists, the build and push processes are skipped, thereby
saving time and resources.

Key Changes:
* Conditional Build and Push: The build and push commands for both the
  frontend and backend Docker images now include a preceding check to
  determine if the image with the current tag already exists on ECR.
* Helper Function: Introduced a new helper function, check_image_on_ecr,
  which utilizes the docker manifest inspect command to check for the
  image's presence on ECR. This function is used to conditionally execute
  the build and push commands based on the image's existence.
* Efficiency Improvements: This approach minimizes unnecessary builds
  and uploads, especially useful in continuous integration and
  deployment (CI/CD) pipelines where build minutes and network bandwidth
  are valuable resources.

This optimization ensures that our deployment process is both efficient
and cost-effective, only utilizing resources when necessary to update
the Docker images on ECR.
  • Loading branch information
placek committed Mar 4, 2024
1 parent d547648 commit 0f05cf3
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions scripts/govtool/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ __check_defined = \
$(if $(value $1),, \
$(error Undefined $1$(if $2, ($2))))

# helper function for checking if image exists on ECR
check_image_on_ecr = \
$(docker) manifest inspect "$(repo_url)/$1:$2" > /dev/null 2>&1

.PHONY: all
all: docker-login prepare-config upload-config build-backend push-backend build-frontend push-frontend deploy-stack info notify

Expand Down Expand Up @@ -75,39 +79,42 @@ upload-config: prepare-config
rsync -av -e 'ssh -o StrictHostKeyChecking=no' config/target/. $(ssh_url):config

.PHONY: build-backend
build-backend: build-backend-base
build-backend: docker-login build-backend-base
@:$(call check_defined, cardano_network)
@:$(call check_defined, env)
$(call check_image_on_ecr,backend,$(backend_image_tag)) || \
$(docker) build --build-arg BASE_IMAGE_TAG=$(base_backend_image_tag) --tag "$(repo_url)/backend:$(backend_image_tag)" ../../govtool/backend

.PHONY: build-backend-base
build-backend-base:
build-backend-base: docker-login
@:$(call check_defined, cardano_network)
@:$(call check_defined, env)
docker manifest inspect "$(repo_url)/backend-base:$(base_backend_image_tag)" > /dev/null 2>&1 || \
$(call check_image_on_ecr,backend-base,$(base_backend_image_tag)) || \
$(docker) build --file ../../govtool/backend/Dockerfile.base --tag "$(repo_url)/backend-base:$(base_backend_image_tag)" ../../govtool/backend
@echo "Using backend-base image: $(repo_url)/backend-base:$(base_backend_image_tag)"

.PHONY: push-backend
push-backend: docker-login push-backend-base
push-backend: docker-login push-backend-base build-backend
@:$(call check_defined, cardano_network)
@:$(call check_defined, env)
$(call check_image_on_ecr,backend,$(backend_image_tag)) || \
$(docker) push $(repo_url)/backend:$(backend_image_tag)

.PHONY: push-backend-base
push-backend-base: docker-login
push-backend-base: docker-login build-backend-base
@:$(call check_defined, cardano_network)
@:$(call check_defined, env)
docker manifest inspect "$(repo_url)/backend-base:$(base_backend_image_tag)" > /dev/null 2>&1 || \
$(call check_image_on_ecr,backend-base,$(base_backend_image_tag)) || \
$(docker) push $(repo_url)/backend-base:$(base_backend_image_tag)

.PHONY: build-frontend
build-frontend:
build-frontend: docker-login
@:$(call check_defined, cardano_network)
@:$(call check_defined, env)
@:$(call check_defined, gtm_id)
@:$(call check_defined, sentry_dsn)
if [[ "$(cardano_network)" = "mainnet" ]]; then NETWORK_FLAG=1; else NETWORK_FLAG=0; fi; \
$(call check_image_on_ecr,frontend,$(frontend_image_tag)) || \
$(docker) build --tag "$(repo_url)/frontend:$(frontend_image_tag)" \
--build-arg VITE_BASE_URL="https://$(domain)/api" \
--build-arg VITE_GTM_ID="$(gtm_id)" \
Expand All @@ -116,9 +123,10 @@ build-frontend:
../../govtool/frontend

.PHONY: push-frontend
push-frontend: docker-login
push-frontend: docker-login build-frontend
@:$(call check_defined, cardano_network)
@:$(call check_defined, env)
$(call check_image_on_ecr,frontend,$(frontend_image_tag)) || \
$(docker) push $(repo_url)/frontend:$(frontend_image_tag)

.PHONY: deploy-stack
Expand Down

0 comments on commit 0f05cf3

Please sign in to comment.