Skip to content

Commit

Permalink
♻️ Add a Makefile goal to build and push docker image at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
Borie, Niels committed Mar 5, 2024
1 parent f56447f commit f2abd1c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build_and_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
name: Build and push Docker images
runs-on: ubuntu-latest
steps:
- name: Checkout code
- name: ↙️ Checkout code
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
Expand All @@ -24,7 +24,7 @@ jobs:
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: ✨Clean up unnecessary directories
- name: Clean up unnecessary directories
run: |
sudo rm -rf /usr/share/dotnet
sudo rm -rf /opt/ghc
Expand Down
44 changes: 36 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ SUPPORTED_PYTHON_VERSIONS := 3.9 3.10 3.11 3.12
ALL_LAYERS := base advanced
ALL_BUILDERS := conda mamba

ARCHITECTURE := $(shell uname -m)

ifeq ($(ARCHITECTURE),arm64)
DOCKER_BUILD_COMMAND = docker build --progress=plain --no-cache --force-rm
else
DOCKER_BUILD_COMMAND = docker buildx build --platform linux/amd64,linux/arm64 --progress=plain --no-cache --force-rm
endif


.DEFAULT_GOAL:=help

help: ## Display this help
Expand All @@ -38,29 +47,48 @@ help: ## Display this help
### Docker ###
.PHONY: build-all-images build-image

build-and-push-all-images: ## Build all machine-learning-environments docker images and push them to the registry
@for version in $(SUPPORTED_PYTHON_VERSIONS); do \
for builder in $(ALL_BUILDERS); do \
for layer in $(ALL_LAYERS); do \
$(MAKE) build-image PYTHON_VERSION=$$version LAYER=$$layer BUILDER=$$builder IMAGE_VERSION=$(IMAGE_VERSION); \
if [ "$(BRANCH_NAME)" = "develop" ] || [ "$(BRANCH_NAME)" = "main" ]; then \
$(MAKE) push-image PYTHON_VERSION=$$version LAYER=$$layer BUILDER=$$builder IMAGE_VERSION=$(IMAGE_VERSION); \
fi \
done; \
$(MAKE) clean-images; \
done \
done

clean-images: ## Clean all built images and associated layers
@echo "Cleaning built images and associated layers..."
@docker image prune -af

build-all-images: ## Build all machine-learning-environments docker images
@for version in $(SUPPORTED_PYTHON_VERSIONS); do \
for layer in $(ALL_LAYERS); do \
for builder in $(ALL_BUILDERS); do \
for builder in $(ALL_BUILDERS); do \
for layer in $(ALL_LAYERS); do \
$(MAKE) build-image PYTHON_VERSION=$$version LAYER=$$layer BUILDER=$$builder IMAGE_VERSION=$(IMAGE_VERSION); \
done \
done \
done

push-all-images: ## push all machine-learning-environments docker images
push-all-images: ## Push all machine-learning-environments docker images to the registry
@for version in $(SUPPORTED_PYTHON_VERSIONS); do \
for layer in $(ALL_LAYERS); do \
$(MAKE) push-image PYTHON_VERSION=$$version LAYER=$$layer IMAGE_VERSION=$(IMAGE_VERSION); \
for builder in $(ALL_BUILDERS); do \
for layer in $(ALL_LAYERS); do \
$(MAKE) push-image PYTHON_VERSION=$$version LAYER=$$layer BUILDER=$$builder IMAGE_VERSION=$(IMAGE_VERSION); \
done \
done \
done

build-image: ## Build a single machine-learning-environments docker image (args : PYTHON_VERSION, LAYER, BUILDER, IMAGE_VERSION)
@echo "PYTHON_VERSION=$(PYTHON_VERSION) PYTHON_RELEASE_VERSION=$$(jq -r '.python."$(PYTHON_VERSION)".release' package.json)"
@echo "Building image using PYTHON_VERSION=$(PYTHON_VERSION) LAYER=$(LAYER) BUILDER=$(BUILDER) IMAGE_VERSION=$(IMAGE_VERSION)"
@real_python_version=$$(jq -r '.python."$(PYTHON_VERSION)".release' package.json); \
docker build --progress=plain --no-cache --force-rm -t $(REGISTRY_URL)/$(LAYER)-$(BUILDER)-py$(PYTHON_VERSION):$(IMAGE_VERSION) --build-arg PYTHON_RELEASE_VERSION=$$real_python_version --build-arg PYTHON_VERSION=$(PYTHON_VERSION) --build-arg IMAGE_VERSION=$(IMAGE_VERSION) --build-arg BUILDER=$(BUILDER) -f layers/$(LAYER)/$(BUILDER).Dockerfile layers/$(LAYER)/

$(DOCKER_BUILD_COMMAND) -t $(REGISTRY_URL)/$(LAYER)-$(BUILDER)-py$(PYTHON_VERSION):$(IMAGE_VERSION) --build-arg PYTHON_RELEASE_VERSION=$$real_python_version --build-arg PYTHON_VERSION=$(PYTHON_VERSION) --build-arg IMAGE_VERSION=$(IMAGE_VERSION) --build-arg BUILDER=$(BUILDER) -f layers/$(LAYER)/$(BUILDER).Dockerfile layers/$(LAYER)/

push-image: ## Push machine-learning-environments image to registry (args : PYTHON_VERSION, LAYER, BUILDER, IMAGE_VERSION)
@echo "Pushing image $(REGISTRY_URL)/$(LAYER)-$(BUILDER)-py$(PYTHON_VERSION):$(IMAGE_VERSION)"
docker push $(REGISTRY_URL)/$(LAYER)-$(BUILDER)-py$(PYTHON_VERSION):$(IMAGE_VERSION)
if [ "${BRANCH_NAME}" = "main" ]; then \
docker push $(REGISTRY_URL)/$(LAYER)-$(BUILDER)-py$(PYTHON_VERSION):latest; \
Expand Down

0 comments on commit f2abd1c

Please sign in to comment.