Fast-track guide to deploying a Flask app on Upsun
Here are all the steps required to successfully generate a Flask app with Cookiecutter, and deploy it on Upsun.
T: Run the line in a terminal/command prompt
-
T:
pip3 install cookiecutter -
T:
cookiecutter https://github.com/cookiecutter-flask/cookiecutter-flask.git -
Follow the prompts to submit the following information:
- Name
- GitHub username
- Project name
- App name
- Description
- Pipenv
- Python version
- Node version
- Heroku
-
cdinto project directory (app_namedefined at step 3) -
T:
git init . -
T:
git branch -m main -
T:
upsun project:init -
Follow the prompts:
- Select Python
- Select your services
-
T:
git add . -
T:
git commit -m "Init commit" -
T:
upsun p:create -
Follow the prompts:
- Create or select your organization
- Name your project
- Choose a region
- Enter the branch name defined at step 6
- Set the project as the remote (for now)
- Select
Yto continue
-
Open
.upsun/config.yaml -
Find the section describing
variables -
Uncomment
# variablesand theenv:line -
Add the following configuration:
.upsun/config.yamlvariables: env: FLASK_APP: autoapp.py -
Find the section for
mounts -
Add the following configuration:
.upsun/config.yamlmounts: "app_name/static": source: "storage" source_path: "static_assets" -
Find the section for
hooks:build -
Add the following configuration:
.upsun/config.yamlhooks: build: | set -eux pip install --upgrade pip pip install -r requirements.txt npm install -
Find the section for
hooks:deploy -
Add the following configuration:
.upsun/config.yamldeploy: set -eux npm run build -
Find the section for
web:commands:start -
Add the following configuration:
.upsun/config.yamlweb: commands: start: "flask run -p $PORT" upstream: socket_family: tcp -
T:
git add .upsun/config.yaml -
T:
git commit -m "Adds FLASK_APP env var, adds mount for static builds, build commands, npm run build on deploy, web start command" -
Open your
.environmentfile -
Change
DATATABASE_URLto:your_service_type://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE} -
Add the following lines to
.environment:.environmentexport SECRET_KEY="${PLATFORM_PROJECT_ENTROPY}" export FLASK_DEBUG=$( [ "${PLATFORM_ENVIRONMENT_TYPE}" = "production" ] && echo 0 || echo 1) export FLASK_ENV="${PLATFORM_ENVIRONMENT_TYPE}" export GUNICORN_WORKERS=1 export LOG_LEVEL=$( [ "${PLATFORM_ENVIRONMENT_TYPE}" = "production" ] && echo "info" || echo "debug") # In production, set to a higher number, like 31556926 export SEND_FILE_MAX_AGE_DEFAULT=$( [ "${PLATFORM_ENVIRONMENT_TYPE}" = "production" ] && echo 31556926 || echo 0) -
T:
git add .environment -
T:
git commit -m "Adds needed flask env vars" -
T:
upsun environment:push -
Enter
Y. -
If error, T:
upsun resources:setand follow the prompts. -
T:
python3 -m venv env -
T:
source venv/bin/activate -
T:
pip install --upgrade pip -
T:
pip install -r requirements.txt -
T:
upsun tunnel:open -y -
Open
.upsun/config.yaml -
Add the following variables:
.upsun/config.yamlexport PLATFORM_RELATIONSHIPS="$(upsun tunnel:info --encode)" export PORT=8888 export PLATFORM_PROJECT_ENTROPY=$(openssl rand -base64 32) export PLATFORM_ENVIRONMENT_TYPE=production -
T:
source ./.environment -
T:
flask db init -
T:
flask db migrate -
Open
.upsun/config.yaml -
Find the section for
hooks:deploy -
Add the following configuration:
.upsun/config.yamldeploy: | set -eux npm run build flask db upgrade -
T:
git add migrations/* -
T:
git commit -m "Adds migrations" -
T:
git add .upsun/config.yaml -
T:
git commit -m "Adds flask db upgrade to deploy hook" -
T:
upsun environment:push
The above steps do not include setting up a local development environment, or switching to Gunicorn as a web server.