Skip to content

Student project #10 - Scale a Django application with CircleCI, Docker, Heroku and Sentry

License

Notifications You must be signed in to change notification settings

KDerec/OC-Lettings

 
 

Repository files navigation


Logo

Scale a Django application with CircleCI, Docker, Heroku and Sentry

circleci-badge

This student project is the #10 of my training.
You can follow the previous one here.

Table of Contents
  1. About The Project
  2. Built With
  3. Getting Started
  4. License
  5. Contact

About The Project

🌱 Developped skills

  • Apply a modular architecture in a Python application
  • Manage code production using the CI/CD methodology with CircleCI
  • Refactor an application to reduce technical debt
  • Deploy an application using Heroku
  • Implement a code control system using Sentry

📖 Scenario

I’m a junior developer at Orange County Lettings, a startup in the real estate rental industry and the company has decided to take action to improve them site and its deployment.

For that, Dominique, my technical director, asked me :
“Here are the areas of the site and deployment that we would like you to improve or add:

  1. Reduction of various technical liabilities on the project
  2. Redesign of the modular architecture
  3. Added a CI/CD pipeline using CircleCI and Heroku
  4. Application monitoring and error tracking via Sentry.

🚧 🚀 Project goal & deliverable

Briefly show the site running on localhost, and explain the problems encountered when setting up the local development environment.
After presenting the link to the Heroku deployment deliverable, delete the Heroku application, create a new one, and redeploy the site by running the CI/CD pipeline in CircleCI.
Once the CI/CD pipeline is complete, resubmit the site via the link to the new Heroku deployment.
Run the Docker command that pulls the image from Docker Hub and manages the site locally.

(back to top)

Built With

(back to top)

Getting Started

There are two sections, one to get a local copy and another to deploy the application on a Heroku server.

Local development

To get a local copy up and running follow these simple example steps.

L-Prerequisites

In the rest of the local development documentation, it is assumed that the python command in your shell OS runs the above Python interpreter (unless a virtual environment is enabled).

(back to top)

Installation

  1. Clone the project in desired directory ;
    git clone https://github.com/KDerec/OC-Lettings.git
  2. Change directory to project folder ;
    cd path/to/OC-Lettings
  3. Create a virtual environnement (More detail to Creating a virtual environment) ;
    • For Windows :
      python -m venv venv
    • For macOS / Linux :
      python3 -m venv venv
  4. Activate the virtual environment ;
    • For Windows :
      .\venv\Scripts\activate
    • For macOS / Linux :
      source venv/bin/activate
  5. Install package of requirements.txt ;
    pip install -r requirements.txt
  6. Create a .env file in the root project directory with a SECRET_KEY for Django, for example :
    # in .env
    SECRET_KEY = "Yf7S^Z}FShCCm+ZdSb51vA)rr<4eFi"
  7. Run the server ;
    python manage.py runserver
  8. Now the application is accessible in your browser at http://localhost:8000 🎉.

(back to top)

Usage

Linting

Adapt the virtual environment activation to your system.

cd path/to/OC-Lettings
source venv/bin/activate
flake8

Unit tests

Adapt the virtual environment activation to your system.

cd path/to/OC-Lettings
source venv/bin/activate
pytest

Database

  • Open sqlite3 command-line program (doc here) in your path/to/OC-Lettings ;
  • Connect to the database .open oc-lettings-site.sqlite3 ;
  • Display the tables in the database .tables ;
  • Display the columns in the profiles table, pragma table_info(profiles_profile);
  • Run a query on the profiles table, select user_id, favorite_city from profiles_profile where favorite_city like 'B%';
  • .quit to exit.

Administration panel

  • Go to http://localhost:8000/admin ;
  • Log in with user admin, password Abc1234!.

(back to top)

Deployment

The application is deployed on Heroku using CircleCi pipeline, Docker container and Sentry for monitoring and error tracking.

How it's works ?

When a developer executes a git push command to the master branch of the project, it's run a pipeline on CircleCi and this pipeline run a set of processess consisting of building and testing stage before deployment.

If testing (pytest + flake8) and building (docker image) are successful, docker image is pushed to DockerHub and deploy to Heroku.

The details of the pipeline configuration are available in the config.yml file of the .circleci folder and the the instructions to build docker images are in Dockerfile.

deployment-schema

Condition

Only modification to master branch execute the push of the Docker image to DockerHub and the Heroku deployment.
Changes to other branches will only trigger testing and linting.

(back to top)

D-Prerequisites

(back to top)

Deploy from zero and yourself repository

  1. Fork this repo (How to fork a repo ? Very clear answer) ;
  2. Follow the installation guide to setup the project ;
  3. Logged with your Docker account to hub.docker.com, create a new public repository with oc-lettings name (it's important);
  4. Connected to Heroku dashboard, create a new app ;
  5. Open a new terminal, enter the command below and follow the instruction to login (more detail about Heroku CLI authentification):
    heroku login
  6. Retrieve your API token by entering the command below and keep this token (for example : c4cd94da15ea0544802c2cfd5ec4ead324327430):
    heroku auth:token
  7. Connected to Sentry, create a new projet and retrieve your DSN key ;
  8. Login to CircleCI with your GitHub account ;
  9. Set up your project by selecting your OC-Lettings repository and the config.yml file from master branch ;
    set-up-project-from-circleci-webpage
  10. You will see your project and it's status will be Failed, but it's normal, because we haven't configured the project yet ;
  11. To do that, go to Project Settings ;
    project-settings-from-circleci-webpage
  12. Add environment variables below ;
Name Value
DOCKER_PASSWORD Your docker password account
DOCKER_USER Your docker username account
HEROKU_API_KEY The token you generated before with the heroku auth:token command
HEROKU_APP_NAME The name of the app you created in Heroku
SECRET_KEY Your Django secret key
SENTRY_DSN Your DSN key from Sentry

environement-variable-from-circleci-webpage
13. Go back to the pipelines of the project, rerun the workflow and wait until Success ;
14. Go to heroku dashboard, open your app and congratulation, it's deploy ! 🎉

(back to top)

License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

Contact

Kévin Dérécusson 👇🏻
Email : kevin.derecusson@outlook.fr
LinkedIn : https://www.linkedin.com/in/kevin-derecusson/

(back to top)

This student project is the 10# of my training.
You can follow the previous one here.

About

Student project #10 - Scale a Django application with CircleCI, Docker, Heroku and Sentry

Topics

Resources

License

Stars

Watchers

Forks

Languages

  • JavaScript 51.6%
  • CSS 40.9%
  • Python 6.8%
  • Other 0.7%