- Make directory structure
- Initialize git
$ git init
$ git remote add origin git@github.com:sheoranjs24/YOUR_APP_NAME.git
$ git -b checkout develop
- Touch initial files
$ touch app.py .gitignore .gitconfig README.md requirements.txt .editorconfig CHANGELOG.md LICENSE MANIFEST.in requirements.txt package.json gulpfile.js fabfile.py
- Make Python Virtual Environment
$ which python3
/usr/local/bin/python3
$ mkvirtualenv --python=/usr/local/bin/python3 YOUR_APP_NAME
- Automatically change working directory to the project directory
$ workon YOUR_APP_NAME
$ echo "cd ~/path/to/your/project" >> $VIRTUAL_ENV/bin/postactivate
- Install Python Libraries
$ pip install Flask==0.10.1
$ pip install gunicorn
- Add installed libaries to requirements.txt
$ pip freeze > requirements.txt
- Add content to app.py
- Run the app
$ python app.py \# http://localhost:5000/
- Install Heroku toolkit and plugins
$ heroku plugins:install heroku-pipelines
- Add content to procfile
$ echo "web: gunicorn --pythonpath src backend.app:app" >> Procfile
- Heroku settings for python
echo "python-3.4.2" >> runtime.txt
- Commit your changes in git and PUSH to Github
$ git checkout -b develop
$ git add .
$ git commit -m "hello world app"
$ git push -u origin develop
- Touch Heroku specific files
$ touch Procfile app.json .env .slugignore runtime.txt
- Create 2 heroku apps
$ heroku create YOUR_APP_NAME-stage
$ heroku create YOUR_APP_NAME-prod
- Add Heroku git remotes
$ git remote add stage git@heroku.com:YOUR_APP_NAME-stage.git
$ git remote add prod git@heroku.com:YOUR_APP_NAME-prod.git
- Create Heroku Pipelines
$ heroku pipelines:create -a YOUR_APP_NAME-stage
$ heroku pipelines:add -a YOUR_APP_NAME-prod YOUR_APP_NAME
- Connect Heroku with GitHub so that commits are automatically deployed
- Run app locally
$ gunicorn --pythonpath src backend.app:app # http://127.0.0.1:8000
- Test heroku local deploy
- Commit to git
$ git add .
$ git commit -m "add heroku config"
$ git push -u origin develop
- Bump up version number & push to staging branch on GitHub (auto-deploy to Heroku)
$ git checkout -b stage
\# todo: make some changes to reflect new version
$ git commit -a "bump version number to XX"
$ git push origin stage
- Check logs
$ heroku logs --tail --app YOUR_APP_NAME-stage
- Push changes to master & tag it
$ git checkout master
$ git merge --no-ff stage
$ git tag -a XX
$ git push origin master
- Create config.py file
- Local & Remote Settings
$ echo "export APP_SETTINGS=\"config.DevelopmentConfig\"" > $VIRTUAL_ENV/bin/Postactivate
$ workon YOUR_APP_NAME
$ heroku config:set APP_SETTINGS=config.StagingConfig --remote stage
$ heroku config:set APP_SETTINGS=config.ProductionConfig --remote prod
\# test
$ heroku run python src/backend/app.py --app YOUR_APP_NAME-stage
$ heroku run python src/backend/app.py --app YOUR_APP_NAME-prod
- Send logs to Papertrail
- Add Automated tests using Travis/Jenkins
- Install Dependencies
$ brew install postgres
$ pip install psycopg2 Flask-SQLAlchemy Flask-Migrate
$ pip freeze > requirements.txt
- Create a database called YOUR_APP_NAME-dev to use as our local development database.
$ createdb YOUR_APP_NAME-dev
$ psql YOUR_APP_NAME-dev
SQL> \q
$
- Add database to configuration file: Add SQLALCHEMY_DATABASE_URI field to the Config() class
- Add database variable to postactivate file
$ echo "export DATABASE_URL=\"postgresql://localhost/YOUR_APP_NAME-dev\"" >> $VIRTUAL_ENV/bin/postactivate
$ workon YOUR_APP_NAME #restart virtual env
- In your app.py file import SQLAlchemy and connect to the database.
- Use Alembic and Flask-Migrate to migrate our local database to the latest version
$ touch src/backend/manage.py
$ python src/backend/manage.py db init
- Migrate db
$ python src/backend/manage.py db migrate
$ python src/backend/manage.py db upgrade
- Remote Migration Configuration
\# Check if the app already has a database
$ heroku config --app YOUR_APP_NAME-stage
\# If no database, then add the Postgres addon
$ heroku addons:create heroku-postgresql:hobby-dev --app YOUR_APP_NAME-stage
- Commit the changes and merge to staging
$ git add .
$ git commit -m "add postgres configuration"
$ git push origin feature/config-database
\# todo: Create pull-request and merge branch to develop
$ git checkout develop
$ git pull origin develop
$ git checkout stage
$ git merge --no-ff develop
$ git push origin stage
- Run the migrations that we created to migrate our staging database.
$ heroku run python src/backend/manage.py db upgrade --app YOUR_APP_NAME-stage
- Setup Production database
$ heroku addons:create heroku-postgresql:hobby-dev --app YOUR_APP_NAME-prod
$ git checkout master
$ git merge --no-ff stage
$ git tag -a XX
$ git push origin master
\# todo: Use heroku pipeline to push stage to prod
$ heroku run python src/backend/manage.py db upgrade --app YOUR_APP_NAME-prod