Skip to content

Commit

Permalink
improve Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
fqjony committed Dec 23, 2024
1 parent 9c79e8e commit afaaf5e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
30 changes: 11 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,7 @@ include Makefile.help
# Default target
.DEFAULT_GOAL := help

.PHONY: run run-it clean build stringify-creds exec log test dev-pipeline

# 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..."; \
else \
echo "No JSON credential files found. Skipping..."; \
fi; \
done
@chmod +x creds_env.sh
.PHONY: run run-it clean build exec log test dev-pipeline

# Build the Docker image
MULTIPLATFORM ?= false
Expand All @@ -37,10 +22,17 @@ build:
@echo "Docker image build completed."

# Run Docker container (supports interactive mode)
run: clean stringify-creds
run: clean
@echo "Running Docker container..."
@. ./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)) \
@docker run $(if $(INTERACTIVE),-it,-d) --rm --name $(CONTAINER_NAME) \
$(foreach file,$(wildcard *.json),\
$(eval CREDS_VAR_NAME=$(shell echo "$(file)" | sed -e 's/\.json//g' -e 's/\./_/g' | tr '[:lower:]' '[:upper:]')) \
$(eval CREDS_VAR_VALUE=$(shell cat "$(file)" | jq -c .)) \
-e $(CREDS_VAR_NAME)='$(CREDS_VAR_VALUE)' \
) \
$(foreach env_var,$(filter %_CREDS,$(.VARIABLES)),\
-e $(env_var)=$($(env_var)) \
) \
$(foreach vol,$(VOLUMES),-v $(vol)) \
$(DOCKER_IMAGE) $(COMMAND)
$(if $(filter false,$(INTERACTIVE)),docker logs -f $(CONTAINER_NAME);)
Expand Down
4 changes: 2 additions & 2 deletions Makefile.help
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Help content for Makefile targets

HELP_BUILD = "Build the Docker image:"
HELP_RUN = "Run the Docker container:"
HELP_RUN = "Run the Docker container (supports credential JSON files and environment variables):"
HELP_RUN_IT = "Run the Docker container interactively:"
HELP_EXEC = "Exec into the running container:"
HELP_LOG = "View the container logs:"
Expand Down Expand Up @@ -52,4 +52,4 @@ help:
@echo " COMMAND (default: $(COMMAND))"
@echo " MULTIPLATFORM (default: $(MULTIPLATFORM))"
@echo " DOCKER_IMAGE (default: $(DOCKER_IMAGE))"
@echo " FOLLOW_LOGS (default: $(FOLLOW_LOGS))"
@echo " FOLLOW_LOGS (default: $(FOLLOW_LOGS))"

0 comments on commit afaaf5e

Please sign in to comment.