Skip to content

Commit

Permalink
Change the build process
Browse files Browse the repository at this point in the history
- remove setup.cfg and use a pyproject.toml
- refactor the docker files structure and process
  • Loading branch information
tudoramariei committed Oct 26, 2023
1 parent 5b7cffa commit baf3828
Show file tree
Hide file tree
Showing 29 changed files with 279 additions and 324 deletions.
5 changes: 3 additions & 2 deletions .env.example.dev
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ NO_REPLY_EMAIL=
DEFAULT_FROM_EMAIL=

# admin
DJANGO_ADMIN_EMAIL=a@a.co
DJANGO_ADMIN_EMAIL=a@example.co
DJANGO_ADMIN_PASSWORD=a

# database
POSTGRES_USER=postgres
POSTGRES_PASSWORD=secret
POSTGRES_DB=seismic_site
DATABASE_URL=postgres://postgres:secret@db/postgres
POSTGRES_HOST=db
POSTGRES_PORT=

# client
REACT_APP_DJANGO_SITE_URL=http://localhost
Expand Down
5 changes: 3 additions & 2 deletions .env.example.prod
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ CORS_ALLOWED_ORIGINS=http://localhost
CORS_ALLOWED_ORIGIN_REGEXES=http://localhost*

# admin
DJANGO_ADMIN_EMAIL=a@a.co
DJANGO_ADMIN_EMAIL=a@example.co
DJANGO_ADMIN_PASSWORD=a

# database
POSTGRES_USER=postgres
POSTGRES_PASSWORD=secret
POSTGRES_DB=seismic_site
DATABASE_URL=postgres://postgres:secret@db/postgres
POSTGRES_HOST=db
POSTGRES_PORT=

BACKGROUND_WORKERS=2
GUNICORN_WORKERS=2
Expand Down
16 changes: 0 additions & 16 deletions .github/dependabot.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/api_pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
paths:
- 'api/**.py'
- 'api/requirements*.*'
- 'api/Dockerfile'
- 'docker/dockerfiles/Dockerfile.backend*'
- 'docker-compose.yml'
- '.github/workflows/api_pipeline.yml'

Expand Down
35 changes: 35 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
on:
push:
branches:
- 'main'
tags:
- 'v*'

name: Build Docker image

jobs:

build-backend-amd64:
name: backend/amd64
uses: code4romania/.github/.github/workflows/build-push-image.yml@main
with:
images: ghcr.io/code4romania/seismic-risc-backend
context: ./
dockerfile: ./docker/dockerfiles/Dockerfile.backend
registry: ghcr.io
secrets:
username: ${{ github.actor }}
token: ${{ secrets.GITHUB_TOKEN }}

# disable frontend build for now since we're using Vercel
# build-frontend-amd64:
# name: frontend/amd64
# uses: code4romania/.github/.github/workflows/build-push-image.yml@main
# with:
# images: ghcr.io/code4romania/seismic-risc-frontend
# context: ./client
# dockerfile: ./docker/dockerfiles/Dockerfile.frontend
# registry: ghcr.io
# secrets:
# username: ${{ github.actor }}
# token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/client_pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
paths:
- 'client/**.js'
- 'client/package*.json'
- 'client/Dockerfile'
- 'docker/dockerfiles/Dockerfile.frontend*'
- 'docker-compose.yml'
- '.github/workflows/client_pipeline.yml'

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis-js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
paths:
- 'client/**.js'
- 'client/package*.json'
- 'client/Dockerfile'
- 'docker/dockerfiles/Dockerfile.frontend*'
- 'docker-compose.yml'
- '.github/workflows/client_pipeline.yml'
- '.github/workflows/code-analysis-js.yml'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis-py.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
paths:
- 'api/**.py'
- 'api/requirements*.*'
- 'api/Dockerfile'
- 'docker/dockerfiles/Dockerfile.backend*'
- 'docker-compose.yml'
- '.github/workflows/api_pipeline.yml'
- '.github/workflows/code-analysis-py.yml'
Expand Down
27 changes: 0 additions & 27 deletions .github/workflows/deploy_staging.yml

This file was deleted.

42 changes: 0 additions & 42 deletions .github/workflows/local_deployment_pipeline.yml

This file was deleted.

152 changes: 100 additions & 52 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,96 +1,144 @@
help: ## Display a help message detailing commands and their purpose
@echo "Commands:"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@grep -E '^([a-zA-Z_-]+:.*?## .*|#+ (.*))$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@echo ""

build-dev: ## builds the container with the development flag
docker compose build
docker compose up -d

build-prod: ## builds the container with the production flag
## [Managing the project]
### Stopping the containers and dropping the databases
stop-dev: ## stops the dev project
docker compose down -t 60

drop-dev: ## stops the dev project
docker compose down -v -t 60

stop-prod: ## stops the dev project
docker compose -f docker-compose.prod.yml down -t 60

drop-prod: ## stops the dev project
docker compose -f docker-compose.prod.yml down -v -t 60

### Building & starting the containers
up-dev: ## run the project
docker compose up --build

upd-dev: ## run the project in detached mode
docker compose up -d --build

build-prod:
docker compose -f docker-compose.prod.yml build \
--build-arg $$(cat .env.prod | grep ENVIRONMENT) \
--build-arg $$(cat .env.prod | grep REACT_APP_CAPTCHA_API_KEY) \
--build-arg $$(cat .env.prod | grep REACT_APP_HERE_MAPS_API_KEY) \
--build-arg $$(cat .env.prod | grep REACT_APP_DJANGO_SITE_URL) \
--build-arg $$(cat .env.prod | grep REACT_APP_DJANGO_PORT) \
--build-arg $$(cat .env.prod | grep REACT_APP_DJANGO_API_ENDPOINT)


up-prod: ## run the project
docker compose -f docker-compose.prod.yml up

upd-prod: ## builds the container with the production flag
docker compose -f docker-compose.prod.yml up -d

### Using the dev setup
run-dev: up-dev ## run the project
rund-dev: upd-dev ## run the project in detached mode
redo-dev: drop-dev up-dev ## delete the db and rerun the project
redod-dev: drop-dev upd-dev ## delete the db and rerun the project in detached mode

build: build-dev ## shorthand for the build-dev command
### With an image built for production
run-prod: build-prod up-prod ## run the project with production settings
rund-prod: build-prod upd-prod ## run the project with production settings in detached mode
redo-prod: drop-prod build-prod up-prod ## delete the db and rerun the project
redod-prod: drop-prod build-prod upd-prod ## delete the db and rerun the project in detached mode

superuser: ## creates a superuser for the API
docker compose exec api ./manage.py createsuperuser
### Other run options
run: run-dev ## set the default run command to dev
redo: redo-dev ## set the default redo command to dev
rund: rund-dev ## set the default run command to dev
redod: redod-dev ## set the default redo command to dev

init-db: superuser ## sets up the database and fixtures
docker compose exec api ./manage.py loaddata statistics
docker compose exec api ./manage.py loaddata proximal_utilities
docker compose exec api ./manage.py loaddata work_performed
docker compose exec api ./manage.py loaddata buildings
stop: stop-dev stop-prod ## stop all running projects

drop-db-dev: ## drops the containers and removes the database for the development environment
docker compose down -v -t 60
drop: drop-dev drop-prod ## drop all databases

drop-db-prod: ## drops the containers and removes the database for the production environment
docker compose -f docker-compose.prod.yml down -v -t 60

drop-db: drop-db-dev drop-db-prod ## drops the containers and removes the database for both environments
## [Monitoring the containers]
logs-dev: ## show the logs of the containers
docker compose logs -f api

redo-db: drop-db init-db ## drops the database, then sets up the database and fixtures
logs: logs-dev ## set the default logs command to dev

## [UTILS]
requirements-build: ## run pip compile and add requirements from the *.in files
docker compose run --rm --no-deps --entrypoint "bash -c" api "cd /code && pip-compile --resolver=backtracking -o requirements.txt requirements.in && pip-compile --resolver=backtracking -o requirements-dev.txt requirements-dev.in"
logs-prod: ## show the logs of the containers
docker compose -f docker-compose.prod.yml logs -f api

requirements-update: ## run pip compile and rebuild the requirements files
docker compose run --rm --no-deps --entrypoint "bash -c" api "cd /code && pip-compile --resolver=backtracking -r -U -o requirements.txt requirements.in && pip-compile --resolver=backtracking -r -U -o requirements-dev.txt requirements-dev.in && chmod a+r requirements.txt && chmod a+r requirements-dev.txt"

migrations: ## generate migrations in a clean container
docker compose exec api ./manage.py makemigrations
## [Django operations]
makemigrations: ## generate migrations in a clean container
docker compose exec api sh -c "python3 -Wd ./manage.py makemigrations $(apps)"

migrate: ## apply migrations in a clean container
docker compose exec api ./manage.py migrate
docker compose exec api sh -c "python3 -Wd ./manage.py migrate $(apps)"

migrations: makemigrations migrate ## generate and apply migrations


makemessages: ## generate the strings marked for translation
docker compose exec api ./manage.py makemessages -a
docker compose exec api sh -c "python3 -Wd ./manage.py makemessages -a"

compilemessages: ## compile the translations
docker compose exec api ./manage.py compilemessages
docker compose exec api sh -c "python3 -Wd ./manage.py compilemessages"

messages: makemessages compilemessages ## generate and compile the translations

messages: makemessages compilemessages

collectstatic:
docker compose exec api ./manage.py collectstatic --no-input
collectstatic: ## collect the static files
docker compose exec api sh -c "python3 -Wd ./manage.py collectstatic --no-input"

format: ## format the code with black & ruff
docker compose exec api sh -c "black ./api && ruff check --fix ./api"

pyshell: ## start a django shell
docker compose exec api ./manage.py shell
docker compose exec -it api sh -c "python3 -Wd ./manage.py shell"

black: ## run the Black formatter on the Python code
black --line-length 120 --target-version py311 --exclude migrations ./api
sh: ## start a sh shell
docker compose exec -it api sh -c "sh"

## [TEST]
test: ## run all tests
docker compose run --rm api "pytest"
bash: ## start a bash shell
docker compose exec -it api sh -c "bash"

test-pdb: ## run tests and enter debugger on failed assert or error
docker compose run --rm api "pytest --pdb"

test-lf: ## rerun tests that failed last time
docker compose run --rm api "pytest --lf"
## [Requirements management]
requirements-build: ## run pip compile and add requirements from the *.in files
docker compose exec api sh -c " \
pip-compile --strip-extras --resolver=backtracking -o requirements.txt requirements.in && \
pip-compile --strip-extras --resolver=backtracking -o requirements-dev.txt requirements-dev.in \
"

requirements-update: ## run pip compile and rebuild the requirements files
docker compose exec api sh -c " \
pip-compile --strip-extras --resolver=backtracking -r -U -o requirements.txt requirements.in && \
pip-compile --strip-extras --resolver=backtracking -r -U -o requirements-dev.txt requirements-dev.in && \
chmod a+r requirements.txt && \
chmod a+r requirements-dev.txt \
"

## [CLEAN]
clean: clean-docker clean-py ## remove all build, test, coverage and Python artifacts

## [Clean-up]
clean-docker: ## stop docker containers and remove orphaned images and volumes
docker compose down -t 60
docker compose down -v -t 60
docker compose -f docker-compose.prod.yml down -v -t 60
docker system prune -f

clean-py: ## remove Python test, coverage, file artifacts, and compiled message files
find ./api -name '.coverage' -delete
find ./api -name '.pytest_cache' -delete
find ./api -name '__pycache__' -delete
find ./api -name 'htmlcov' -delete
find ./api -name '*.pyc' -delete
find ./api -name '*.pyo' -delete
find ./api -name '*.mo' -delete
find ./backend -name '*.mo' -delete
find ./backend -name '*.pyc' -delete
find ./backend -name '*.pyo' -delete
find ./backend -name '.coverage' -delete
find ./backend -name '.pytest_cache' -delete
find ./backend -name '.ruff_cache' -delete
find ./backend -name '__pycache__' -delete
find ./backend -name 'htmlcov' -delete

clean: clean-docker clean-py ## remove all build, test, coverage and Python artifacts
Loading

0 comments on commit baf3828

Please sign in to comment.