Skip to content

Instantly share code, notes, and snippets.

@AlcidesRC

AlcidesRC/Makefile

Last active Mar 12, 2021
Embed
What would you like to do?
Makefile for dockerized PHP applications with available targets on the same file
.PHONY: no_targets__ info help build deploy doc
no_targets__:
.DEFAULT_GOAL := help
#-----------------------------------------------------------------
# CONSTANTS
#-----------------------------------------------------------------
ifneq (,$(findstring xterm,${TERM}))
BLACK := $(shell tput -Txterm setaf 0)
RED := $(shell tput -Txterm setaf 1)
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
BLUE := $(shell tput -Txterm setaf 4)
MAGENTA := $(shell tput -Txterm setaf 5)
CYAN := $(shell tput -Txterm setaf 6)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)
else
BLACK := ""
RED := ""
GREEN := ""
YELLOW := ""
BLUE := ""
MAGENTA := ""
CYAN := ""
WHITE := ""
RESET := ""
endif
TARGET_COLOR := $(YELLOW)
SERVICE_NAME = api
#-----------------------------------------------------------------
# HELP
#-----------------------------------------------------------------
help:
@echo "╔══════════════════════════════════════════════════════════════════════════════╗"
@echo "║ ║"
@echo "${CYAN}.:${RESET} AVAILABLE COMMANDS ${CYAN}:.${RESET}"
@echo "║ ║"
@echo "╚══════════════════════════════════════════════════════════════════════════════╝"
@echo ""
@grep -E '^[a-zA-Z_0-9%-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "· ${TARGET_COLOR}%-30s${RESET} %s\n", $$1, $$2}'
@echo ""
#-----------------------------------------------------------------
# MISCELANEOUS
#-----------------------------------------------------------------
check-required-param-%:
@if [ -z "$($(*))" ] ; then \
echo "" ; \
echo "${RED}Parameter [ ${WHITE}${*}${RED} ] is required!${RESET}" ; \
echo "" ; \
exit 1 ; \
fi;
#-----------------------------------------------------------------
# DOCKER-COMPOSE RELATED
#-----------------------------------------------------------------
build: ## Builds the container(s)
@echo "┌─────────────────────────────────────────────────────────────────────────────┐"
@echo "│ BUILDING THE SERVICE(s) │"
@echo "└─────────────────────────────────────────────────────────────────────────────┘"
@echo ""
@docker-compose build
@echo " 📦 ${GREEN}Build done!${RESET}"
@echo ""
build: ## Builds the container(s)
@echo "┌─────────────────────────────────────────────────────────────────────────────┐"
@echo "│ BUILDING THE SERVICE(s) │"
@echo "└─────────────────────────────────────────────────────────────────────────────┘"
@echo ""
@docker-compose build
@echo " 📦 ${GREEN}Service has been built!${RESET}"
@echo ""
rebuild: down build ## Rebuilds the container(s)
@echo "┌─────────────────────────────────────────────────────────────────────────────┐"
@echo "│ REBUILDING THE SERVICE(s) │"
@echo "└─────────────────────────────────────────────────────────────────────────────┘"
@echo ""
@docker-compose up --remove-orphans
@echo " 📦 ${CYAN}Service has been rebuilt!${RESET}"
@echo ""
up: check-folder ## Starts the container(s) in daemonized mode
@echo "┌─────────────────────────────────────────────────────────────────────────────┐"
@echo "│ BUILDING, (RE)CREATING, STARTING AND ATTACHING TO CONTAINERS FOR A SERVICE │"
@echo "└─────────────────────────────────────────────────────────────────────────────┘"
@echo ""
@docker-compose up --remove-orphans -d
@echo ""
@echo " 🟢 ${GREEN}Service is up & running!${RESET}"
@echo ""
down: ## Stops the container(s)
@echo "┌─────────────────────────────────────────────────────────────────────────────┐"
@echo "│ STOPPING AND REMOVING CONTAINERS, NETWORKS, VOLUMES AND IMAGES │"
@echo "└─────────────────────────────────────────────────────────────────────────────┘"
@echo ""
@docker-compose down
@echo ""
@echo " 🟠 ${YELLOW}Service has been stopped!${RESET}"
@echo ""
restart: ## Restarts the container(s)
@echo "┌─────────────────────────────────────────────────────────────────────────────┐"
@echo "│ RESTARTING ALL STOPPED AND RUNNING SERVICES │"
@echo "└─────────────────────────────────────────────────────────────────────────────┘"
@echo ""
@docker-compose restart
@echo ""
@echo " 🔵 ${BLUE}Service has been restarted!${RESET}"
@echo ""
logs: ## Prints out the container(s) log(s)
@echo "┌─────────────────────────────────────────────────────────────────────────────┐"
@echo "│ DISPLAYING LOG OUTPUT FROM SERVICES │"
@echo "└─────────────────────────────────────────────────────────────────────────────┘"
@echo ""
@docker-compose logs
@echo ""
@echo " 🔎 ${WHITE}Service log were displayed!${RESET}"
@echo ""
#-----------------------------------------------------------------
# SERVICE RELATED
#-----------------------------------------------------------------
bash: ## Opens a bash connection to main service
@echo "┌─────────────────────────────────────────────────────────────────────────────┐"
@echo "│ GETTING AN INTERACTIVE PROMPT FROM SERVICE CONTAINER │"
@echo "└─────────────────────────────────────────────────────────────────────────────┘"
@echo ""
@docker-compose exec ${SERVICE_NAME} bash
@echo ""
@echo "${GREEN}Task is done!${RESET}"
@echo ""
info: ## Displays information about main service
@echo "┌─────────────────────────────────────────────────────────────────────────────┐"
@echo "│ DISPLAYING GENERIC INFORMATION FROM SERVICE │"
@echo "└─────────────────────────────────────────────────────────────────────────────┘"
@echo ""
@docker-compose exec ${SERVICE_NAME} bash -c "php --version"
@echo ""
@echo "${GREEN}Task is done!${RESET}"
@echo ""
#-----------------------------------------------------------------
# COMPOSER RELATED
#-----------------------------------------------------------------
composer-install-no-dev: ## Performs <composer install --optimize-autoloader --no-dev> onto main service
@echo "┌─────────────────────────────────────────────────────────────────────────────┐"
@echo "│ INSTALLING DEPENDENCIES ONTO APPLICATION SERVICE IN PRODUCTION MODE │"
@echo "└─────────────────────────────────────────────────────────────────────────────┘"
@echo ""
@docker-compose exec ${SERVICE_NAME} bash -c "COMPOSER_MEMORY_LIMIT=-1 composer install --optimize-autoloader --no-dev"
@echo ""
composer-install: ## Performs <composer install --optimize-autoloader> onto main service
@echo "┌─────────────────────────────────────────────────────────────────────────────┐"
@echo "│ INSTALLING DEPENDENCIES ONTO APPLICATION SERVICE │"
@echo "└─────────────────────────────────────────────────────────────────────────────┘"
@echo ""
@docker-compose exec ${SERVICE_NAME} bash -c "COMPOSER_MEMORY_LIMIT=-1 composer install --optimize-autoloader"
@echo ""
@echo "${GREEN}Task is done!${RESET}"
@echo ""
composer-update: ## Performs <composer update> onto main service
@echo "┌─────────────────────────────────────────────────────────────────────────────┐"
@echo "│ UPDATING DEPENDENCIES ONTO APPLICATION SERVICE │"
@echo "└─────────────────────────────────────────────────────────────────────────────┘"
@echo ""
@docker-compose exec ${SERVICE_NAME} bash -c "COMPOSER_MEMORY_LIMIT=-1 composer update"
@echo ""
@echo "${GREEN}Task is done!${RESET}"
@echo ""
composer-dump-auto: ## Performs <composer dump-auto> onto main service
@echo "┌─────────────────────────────────────────────────────────────────────────────┐"
@echo "│ GENERATING OPTIMIZED AUTOLOAD CLASS MAP ONTO APPLICATION SERVICE │"
@echo "└─────────────────────────────────────────────────────────────────────────────┘"
@echo ""
@docker-compose exec ${SERVICE_NAME} bash -c "COMPOSER_MEMORY_LIMIT=-1 composer dump-auto"
@echo ""
@echo "${GREEN}Task is done!${RESET}"
@echo ""
#-----------------------------------------------------------------
# APPLICATION RELATED
#-----------------------------------------------------------------
tests: ## Executes the PHPUnit testsuite onto main service
@echo "┌─────────────────────────────────────────────────────────────────────────────┐"
@echo "│ RUNNING THE TESTSUITE ONTO APPLICATION SERVICE │"
@echo "└─────────────────────────────────────────────────────────────────────────────┘"
@echo ""
@docker-compose exec ${SERVICE_NAME} bash -c "cd /code/api && ./vendor/bin/phpunit"
@echo ""
@echo "${GREEN}Task is done!${RESET}"
@echo ""
tests-debug: ## Executes the PHPUnit testsuite with debug flag
@echo "┌─────────────────────────────────────────────────────────────────────────────┐"
@echo "│ RUNNING THE TESTSUITE ONTO APPLICATION SERVICE IN DEBUG MODE │"
@echo "└─────────────────────────────────────────────────────────────────────────────┘"
@echo ""
@docker-compose exec ${SERVICE_NAME} bash -c "cd /code/api && ./vendor/bin/phpunit --debug"
@echo ""
@echo "${GREEN}Task is done!${RESET}"
@echo ""
cache-clear: ## Performs <php artisan clear:cache> onto app container
@echo "┌─────────────────────────────────────────────────────────────────────────────┐"
@echo "│ CLEARING THE APPLICATION CACHE FROM APPLICATION SERVICE │"
@echo "└─────────────────────────────────────────────────────────────────────────────┘"
@echo ""
@docker-compose exec ${SERVICE_NAME} bash -c "cd /code/api && php artisan cache:clear"
@echo ""
@echo "${GREEN}Task is done!${RESET}"
@echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment