From 6264b223116a033acea1bdac3d7d4d49f78f0968 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tobiasz=20K=C4=99dzierski?= <tobiasz.kedzierski@polidea.com>
Date: Sat, 21 Sep 2019 16:16:40 +0200
Subject: [PATCH] Add heroku deployment feature

---
 cookiecutter.json                             |  3 +-
 {{cookiecutter.app_name}}/Pipfile             |  3 ++
 {{cookiecutter.app_name}}/Procfile            |  1 +
 {{cookiecutter.app_name}}/README.rst          | 53 +++++++++++++++++++
 {{cookiecutter.app_name}}/app.json            | 30 +++++++++++
 {{cookiecutter.app_name}}/package.json        |  3 +-
 .../requirements/prod.txt                     |  3 ++
 7 files changed, 94 insertions(+), 2 deletions(-)
 create mode 100644 {{cookiecutter.app_name}}/app.json

diff --git a/cookiecutter.json b/cookiecutter.json
index f43940ce..03cd028c 100644
--- a/cookiecutter.json
+++ b/cookiecutter.json
@@ -7,5 +7,6 @@
 	"project_short_description": "A flasky app.",
 	"use_pipenv": ["no", "yes"],
 	"python_version": ["3.7", "3.6"],
-	"node_version": ["12", "10", "8"]
+	"node_version": ["12", "10", "8"],
+	"deployment_on_heroku": ["no", "yes"]
 }
diff --git a/{{cookiecutter.app_name}}/Pipfile b/{{cookiecutter.app_name}}/Pipfile
index 427b8f29..033e2146 100644
--- a/{{cookiecutter.app_name}}/Pipfile
+++ b/{{cookiecutter.app_name}}/Pipfile
@@ -12,6 +12,9 @@ click = ">=5.0"
 # Database
 Flask-SQLAlchemy = "==2.4.1"
 SQLAlchemy = "==1.3.9"
+{%- if cookiecutter.deployment_on_heroku == "yes" %}
+psycopg2 = "==2.8.3"
+{%- endif %}
 
 # Migrations
 Flask-Migrate = "==2.5.2"
diff --git a/{{cookiecutter.app_name}}/Procfile b/{{cookiecutter.app_name}}/Procfile
index aaf74edf..56dc5814 100644
--- a/{{cookiecutter.app_name}}/Procfile
+++ b/{{cookiecutter.app_name}}/Procfile
@@ -1 +1,2 @@
+release: flask db upgrade
 web: gunicorn {{cookiecutter.app_name}}.app:create_app\(\) -b 0.0.0.0:$PORT -w 3
diff --git a/{{cookiecutter.app_name}}/README.rst b/{{cookiecutter.app_name}}/README.rst
index dc15fac9..08a65149 100644
--- a/{{cookiecutter.app_name}}/README.rst
+++ b/{{cookiecutter.app_name}}/README.rst
@@ -135,3 +135,56 @@ should cache all your assets forever by including the following line
 in your ``settings.py``::
 
     SEND_FILE_MAX_AGE_DEFAULT = 31556926  # one year
+
+
+{%- if cookiecutter.deployment_on_heroku == "yes" %}
+Deployment on Heroku
+--------------------
+
+Before using automatic deployment on Heroku you have to add migrations to your repository.
+You can do it by using following commands ::
+
+    flask db init
+    flask db migrate
+    git add migrations/
+    git commit -m "Add migrations"
+    git commit push
+
+Make sure folder `migrations/versions` is not empty.
+
+Deploy to Heroku button
+^^^^^^^^^^^^^^^^^^^^^^^
+
+.. raw:: html
+
+    <a href="https://heroku.com/deploy"><img src="https://www.herokucdn.com/deploy/button.svg" title="Deploy" alt="Deploy"></a>
+
+
+Heroku CLI
+^^^^^^^^^^
+
+If you want deploy by using Heroku CLI:
+
+* create Heroku App. You can leave your app name, change it or leave it blank (random name will be generated)::
+
+    heroku create {{cookiecutter.app_name}}
+
+* add buildpacks::
+
+    heroku buildpacks:add --index=1 heroku/nodejs
+    heroku buildpacks:add --index=1 heroku/python
+
+* add Postgres database addon (it also sets `DATABASE_URL` environmental variable to created database)::
+
+    heroku addons:create heroku-postgresql:hobby-dev
+
+* set environmental variables (change secret key)::
+
+    heroku config:set SECRET_KEY=<secret-key>
+    heroku config:set FLASK_APP=autoapp.py
+
+* deploy on Heroku::
+
+    git push heroku master
+
+{%- endif %}
\ No newline at end of file
diff --git a/{{cookiecutter.app_name}}/app.json b/{{cookiecutter.app_name}}/app.json
new file mode 100644
index 00000000..b13ea76c
--- /dev/null
+++ b/{{cookiecutter.app_name}}/app.json
@@ -0,0 +1,30 @@
+{
+   "name": "{{cookiecutter.app_name}}",
+   "repository": "https://github.com/{{cookiecutter.github_username}}/{{cookiecutter.app_name}}",
+   "env": {
+      "SECRET_KEY": {
+         "description": "SECRET_KEY.",
+         "generator": "secret"
+      },
+      "FLASK_APP": {
+         "description": "FLASK_APP.",
+         "value": "autoapp.py"
+      }
+   },
+   "buildpacks": [
+      {
+         "url": "heroku/nodejs"
+      },
+      {
+         "url": "heroku/python"
+      }
+   ],
+   "addons": [
+      {
+         "plan": "heroku-postgresql",
+         "options": {
+            "version": "11"
+         }
+      }
+   ]
+}
diff --git a/{{cookiecutter.app_name}}/package.json b/{{cookiecutter.app_name}}/package.json
index 45fb1e21..d6a1f856 100644
--- a/{{cookiecutter.app_name}}/package.json
+++ b/{{cookiecutter.app_name}}/package.json
@@ -7,7 +7,8 @@
     "start": "concurrently -n \"WEBPACK,FLASK\" -c \"bgBlue.bold,bgMagenta.bold\" \"npm run webpack-dev-server\" \"npm run flask-server\"",
     "webpack-dev-server": "NODE_ENV=debug webpack-dev-server --host=0.0.0.0 --port 2992 --hot --inline",
     "flask-server": "{% if cookiecutter.use_pipenv == 'yes' %}pipenv run {% endif %}flask run --host=0.0.0.0",
-    "lint": "eslint \"assets/js/*.js\""
+    "lint": "eslint \"assets/js/*.js\"",
+    "postinstall": "npm run build"
   },
   "repository": {
     "type": "git",
diff --git a/{{cookiecutter.app_name}}/requirements/prod.txt b/{{cookiecutter.app_name}}/requirements/prod.txt
index 86f1ffe6..4d6b1438 100644
--- a/{{cookiecutter.app_name}}/requirements/prod.txt
+++ b/{{cookiecutter.app_name}}/requirements/prod.txt
@@ -8,6 +8,9 @@ click>=7.0
 # Database
 Flask-SQLAlchemy==2.4.1
 SQLAlchemy==1.3.9
+{%- if cookiecutter.deployment_on_heroku == "yes" %}
+psycopg2==2.8.3
+{%- endif %}
 
 # Migrations
 Flask-Migrate==2.5.2
-- 
GitLab