Last active
August 8, 2019 19:07
-
-
Save renoirb/36850e3a23abd7877a25060131b2ecbe to your computer and use it in GitHub Desktop.
Use GNU Make to run execute commands inside containers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Project Makefile | |
# | |
# @author Renoir Boulanger <hello@renoirboulanger.com> | |
# | |
# Ensure you have the following environment variables set in you | |
# shell of choice | |
# - USER_ID | |
# - DB_ENV_MYSQL_ROOT_PASSWORD | |
# | |
# Fish: | |
# ``` | |
# set -g -x USER_ID "(id -u)" | |
# set -g -x DB_ENV_MYSQL_ROOT_PASSWORD "password" | |
# ``` | |
# | |
# Bash: | |
# ``` | |
# export USER_ID="$(id -u)" | |
# export DB_ENV_MYSQL_ROOT_PASSWORD="password" | |
# ``` | |
SHELL := bash | |
PATH := bin:${PATH} | |
PWD := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) | |
# This is a hack | |
# - Get all current user shell env | |
# - Take only ones that starts by T or U (e.g. TERM, USER, DB_ENV_MYSQL_PASSWORD, USER_ID, etc.) | |
# - Replace assignment in GNU Makefile format (e.g. VARNAME := 'value') | |
# - Write to .makeenv | |
# - Make Make include the values. | |
MAKEENVREHASH := $(shell bash -c "env | sed -n '/^[TUD]/p' | sed 's/=/:=/' | sed 's/^/export /' > .makeenv") | |
include .makeenv | |
.DEFAULT_GOAL: default | |
.PHONY: default | |
default: | |
$(info Use one of the following make targets:) | |
@awk -F: '/^[A-z\-_]+\:/ {print " - " $$1}' Makefile | |
# Connect to PostgreSQL CLI | |
# To select a different database than postgres, use | |
# make psql db=otherdb | |
.PHONY: psql | |
psql: | |
USER_ID="postgres" docker-compose exec pg psql -U postgres $(db) | |
.PHONY: mysql | |
mysql: | |
@echo MySQL password is $(DB_ENV_MYSQL_ROOT_PASSWORD) | |
docker-compose exec db /usr/bin/mysql -u root -p"$(DB_ENV_MYSQL_ROOT_PASSWORD)" $(db) | |
# Connect to app and run Symfony console commands | |
# make app-console run="cache:clear" | |
# make app-console run="debug:container" | |
.PHONY: cli-app | |
cli-app: | |
docker-compose exec --user $(USER_ID) app bash | |
.PHONY: cli-assets | |
cli-assets: | |
docker-compose exec --user $(USER_ID) assets bash | |
.PHONY: repl-js | |
repl-js: | |
docker-compose exec assets /usr/local/bin/nodejs | |
.PHONY: repl-php | |
repl-php: | |
docker-compose exec --user $(USER_ID) app php -a | |
.PHONY: repl-redis | |
repl-redis: | |
docker-compose exec redis /usr/local/bin/redis-cli | |
.PHONY: repl-python | |
repl-python: | |
docker-compose exec apiclinical python | |
# Connect to app and run Symfony console commands | |
# make app-console run="cache:clear" | |
# make app-console run="debug:container" | |
.PHONY: app-console | |
app-console: | |
docker-compose exec --user $(USER_ID) app ./bin/console $(run) | |
# make addpkg-bower name="jquery" | |
.PHONY: addpkg-bower | |
addpkg-bower: | |
docker-compose exec --user node assets node_modules/.bin/bower install --save $(name) | |
.PHONY: update-composer | |
update-composer: | |
docker-compose exec --user $(USER_ID) app ./composer.phar install | |
.PHONY: tests-php | |
tests-php: | |
docker-compose exec --user $(USER_ID) app bin/phpunit.xdebug -c protected/tests/phpunit.xml | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment