From c8b7cd7170a5572420c8137623a3b4dfd693c3ee Mon Sep 17 00:00:00 2001 From: Andrew Welch Date: Fri, 1 Dec 2023 20:01:49 -0500 Subject: [PATCH] refactor: Cleaner Makefile using variable targets --- Makefile | 85 ++++++++++++++++---------------------------------------- 1 file changed, 24 insertions(+), 61 deletions(-) diff --git a/Makefile b/Makefile index 7412495..377ccb3 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,18 @@ BUILDER_NAME?=craft-builder -BUILD_PLATFORMS?=linux/$(shell arch) -BUILD_COMMAND?=docker buildx build --load --platform ${BUILD_PLATFORMS} --builder ${BUILDER_NAME} +BUILD_PLATFORM?=linux/$(shell arch) +BUILD_COMMAND?=docker buildx build --load --platform ${BUILD_PLATFORM} --builder ${BUILDER_NAME} IMAGE_NAMESPACE?=nystudio107 -.PHONY: create-builder +PHP_PROD_NAMESPACE?=php-prod-base +PHP_DEV_NAMESPACE?=php-dev-base +NODE_NAMESPACE?=node-dev-base + +PHP_VERSIONS?=php-7.4-alpine php-8.0-alpine php-8.1-alpine php-8.2-alpine +NODE_VERSIONS?=node-14-alpine node-16-alpine node-18-alpine node-20-alpine + +.PHONY: all create-builder php $(PHP_VERSIONS) node $(NODE_VERSIONS) + +# Build all base images +all: php node # Ensure a builder exists for docker buildx create-builder: @@ -10,65 +20,18 @@ create-builder: echo "No builder exists, creating builder: ${BUILDER_NAME}" ; \ docker buildx create --name ${BUILDER_NAME} ; \ fi -# Build an image with docker buildx -build-image: create-builder - ${BUILD_COMMAND} -t ${IMAGE_NAMESPACE}/${TARGET_VERSION} ${TARGET_BASE}/${TARGET_VERSION} - -# Build & load all php base images -php: php8.0 php8.1 php8.2 - -# Build & load php prod & dev base 8.0 images -php8.0: php-prod-base8.0 php-dev-base8.0 -# Build & load php prod base 8.0 images -php-prod-base8.0: TARGET_BASE=php-prod-base -php-prod-base8.0: TARGET_VERSION=8.0-alpine -php-prod-base8.0: build-image -# Build & load php dev base 8.0 images -php-dev-base8.0: TARGET_BASE=php-dev-base -php-dev-base8.0: TARGET_VERSION=8.0-alpine -php-dev-base8.0: build-image - -# Build & load php prod & dev base 8.1 images -php8.1: php-prod-base8.1 php-dev-base8.1 -# Build & load php prod base 8.1 images -php-prod-base8.1: TARGET_BASE=php-prod-base -php-prod-base8.1: TARGET_VERSION=8.1-alpine -php-prod-base8.1: build-image -# Build & load php dev base 8.1 images -php-dev-base8.1: TARGET_BASE=php-dev-base -php-dev-base8.1: TARGET_VERSION=8.1-alpine -php-dev-base8.1: build-image - -# Build & load php prod & dev base 8.2 images -php8.2: php-prod-base8.2 php-dev-base8.2 -# Build & load php prod base 8.2 images -php-prod-base8.2: TARGET_BASE=php-prod-base -php-prod-base8.2: TARGET_VERSION=8.2-alpine -php-prod-base8.2: build-image -# Build & load php dev base 8.2 images -php-dev-base8.2: TARGET_BASE=php-dev-base -php-dev-base8.2: TARGET_VERSION=8.2-alpine -php-dev-base8.2: build-image - -# Build & load all node base images -node: node14 node16 node18 node20 -# Build & load node 14 images -node14: TARGET_BASE=node-dev-base -node14: TARGET_VERSION=14-alpine -node14: build-image +# Build all php base images +php:$(PHP_VERSIONS) -# Build & load node 16 images -node16: TARGET_BASE=node-dev-base -node16: TARGET_VERSION=16-alpine -node16: build-image +# Build specific php prod & dev base images +$(PHP_VERSIONS):create-builder + ${BUILD_COMMAND} -t ${IMAGE_NAMESPACE}/${PHP_PROD_NAMESPACE}:$(subst php-,,$@) ${PHP_PROD_NAMESPACE}/$(subst php-,,$@) + ${BUILD_COMMAND} -t ${IMAGE_NAMESPACE}/${PHP_DEV_NAMESPACE}:$(subst php-,,$@) ${PHP_DEV_NAMESPACE}/$(subst php-,,$@) -# Build & load node 18 images -node18: TARGET_BASE=node-dev-base -node18: TARGET_VERSION=18-alpine -node18: build-image +# Build all node base images +node:$(NODE_VERSIONS) -# Build & load node 20 images -node20: TARGET_BASE=node-dev-base -node20: TARGET_VERSION=20-alpine -node20: build-image +# Build specific node base images +$(NODE_VERSIONS):create-builder + ${BUILD_COMMAND} -t ${IMAGE_NAMESPACE}/${NODE_NAMESPACE}:$(subst node-,,$@) ${NODE_NAMESPACE}/$(subst node-,,$@)