Skip to content

Commit

Permalink
Makefile improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
fqjony committed Dec 23, 2024
1 parent 5608fe9 commit 9c79e8e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dist/
*_creds.json

# Environment variables file
.env
*env*

# Ignore Prettier configuration overrides for development
.prettierignore
33 changes: 19 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ include Makefile.help

# Automatically detect JSON credentials file, stringify its content, and set it as an environment variable
stringify-creds:
@echo "#!/bin/sh" > creds_env.sh
@for file in *.json; do \
if [ -f "$$file" ]; then \
CREDS_VAR_NAME=$$(echo "$$file" | sed -e 's/\.json//g' -e 's/\./_/g' | tr '[:lower:]' '[:upper:]'); \
CREDS_VAR_VALUE=$$(cat "$$file" | jq -c .); \
echo "export $$CREDS_VAR_NAME='$$CREDS_VAR_VALUE'" >> creds_env.sh; \
echo "Setting $$CREDS_VAR_NAME environment variable..."; \
export $$CREDS_VAR_NAME="$$CREDS_VAR_VALUE"; \
else \
echo "No JSON credential files found. Skipping..."; \
fi \
fi; \
done
@chmod +x creds_env.sh

# Build the Docker image
MULTIPLATFORM ?= false
Expand All @@ -37,9 +39,10 @@ build:
# Run Docker container (supports interactive mode)
run: clean stringify-creds
@echo "Running Docker container..."
@docker run $(if $(INTERACTIVE),-it,-d) --rm --name $(CONTAINER_NAME) \
$(foreach file,$(wildcard *.json),-e $(shell echo $(file) | sed -e 's/\.json//g' -e 's/\./_/g' | tr '[:lower:]' '[:upper:]')="$$(cat $(file) | jq -c .)") \
$(DOCKER_IMAGE) $(if $(INTERACTIVE),sh)
@. ./creds_env.sh && docker run $(if $(INTERACTIVE),-it,-d) --rm --name $(CONTAINER_NAME) \
$(foreach var,$(shell . ./creds_env.sh && env | grep -E '^[A-Z_]+_CREDS=' | cut -d= -f1),-e $(var)=$$$(var)) \
$(foreach vol,$(VOLUMES),-v $(vol)) \
$(DOCKER_IMAGE) $(COMMAND)
$(if $(filter false,$(INTERACTIVE)),docker logs -f $(CONTAINER_NAME);)

# Run Docker container in interactive mode
Expand All @@ -54,21 +57,23 @@ exec:
# View the container logs
log:
@echo "Viewing Docker container logs..."
@docker logs $(CONTAINER_NAME)
@if [ "$(FOLLOW_LOGS)" = "true" ]; then \
docker logs -f $(CONTAINER_NAME); \
else \
docker logs $(CONTAINER_NAME); \
fi

# Delete the running container
clean:
@echo "Deleting Docker container if exists..."
@docker stop $(CONTAINER_NAME) 2>/dev/null || true
@docker rm -f $(CONTAINER_NAME) 2>/dev/null || true

# Run the validation tests
test: clean stringify-creds
@echo "Running Docker container to execute tests..."
@docker run --rm --name $(CONTAINER_NAME) \
-v $(USER_WORKER_CONFIG):/home/udx/.cd/configs/worker.yml:ro \
$(foreach file,$(wildcard *.json),-e $(shell echo $(file) | sed -e 's/\.json//g' -e 's/\./_/g' | tr '[:lower:]' '[:upper:]')="$$(cat $(file) | jq -c .)") \
$(DOCKER_IMAGE) /usr/local/tests/main.sh
@echo "Validation tests completed."
# Test Docker container
test: VOLUMES=$(TEST_WORKER_CONFIG):/home/udx/.cd/configs/worker.yml:ro
test: COMMAND=/usr/local/tests/main.sh
test: run
@$(MAKE) log FOLLOW_LOGS=true
@$(MAKE) clean

# Development pipeline
Expand Down
9 changes: 6 additions & 3 deletions Makefile.help
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ help:
@echo " IMAGE_NAME (default: $(IMAGE_NAME))"
@echo " TAG (default: $(TAG))"
@echo " CONTAINER_NAME (default: $(CONTAINER_NAME))"
@echo " ENV_FILE (default: .udx)"
@echo " DOCKER_IMAGE (default: $(DOCKER_IMAGE))"
@echo " USER_WORKER_CONFIG (default: $(USER_WORKER_CONFIG))"
@echo " TEST_WORKER_CONFIG (default: $(TEST_WORKER_CONFIG))"
@echo " VOLUMES (default: $(VOLUMES))"
@echo " DEBUG (default: $(DEBUG))"
@echo " COMMAND (default: $(COMMAND))"
@echo " MULTIPLATFORM (default: $(MULTIPLATFORM))"
@echo " DOCKER_IMAGE (default: $(DOCKER_IMAGE))"
@echo " FOLLOW_LOGS (default: $(FOLLOW_LOGS))"
6 changes: 5 additions & 1 deletion Makefile.variables
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ IMAGE_NAME ?= usabilitydynamics/udx-worker
TAG ?= latest
DOCKER_IMAGE := $(IMAGE_NAME):$(TAG)
CONTAINER_NAME ?= udx-worker-container
USER_WORKER_CONFIG ?= ./tests/configs/worker.yml
TEST_WORKER_CONFIG ?= ./tests/configs/worker.yml
VOLUMES ?=
DEBUG ?= false
COMMAND ?=sh
MULTIPLATFORM ?= false
FOLLOW_LOGS ?= false

0 comments on commit 9c79e8e

Please sign in to comment.