Django Heroku Tutorial Series:
- Heroku vs AWS Which is Best for Your Django project
- How to deploy Django project to Heroku using Docker
- How to deploy Python project to Heroku in Gitlab CI
- How to use Heroku Pipeline
- Heroku Logs Tutorial
- How to monitor Heroku Postgres using heroku-pg-extras
Django Dokku Tutorial Series:
Introduction
In this Heroku tutorial, I will talk about how to deploy python project to Heroku in Gitlab CI
After reading, you will get:
- How to create Heroku token to access the Heroku platform API.
- How to deploy the project to Heroku using Heroku CLI
- How to deploy the project to Heroku using
dpl
package.
Register Heroku API token
First, we should create a Heroku token
, and then we can use that token to call some Heroku API for us to deploy Python project.
There are mainly two ways for us to create Heroku API token.
- Please go to Heorku account setting
- At the bottom, you can see a section
API Key
and you can generate API token there.
Another way is to create API in Heroku CLI.
USAGE
$ heroku authorizations:create
OPTIONS
-S, --short only output token
-d, --description=description set a custom authorization description
-e, --expires-in=expires-in set expiration in seconds (default no
expiration)
-j, --json output in json format
-s, --scope=scope set custom OAuth scopes
DESCRIPTION
This creates an authorization with access to your Heroku account.
$ heroku authorizations:create -d "token for Django project"
# after you create, you can check existing tokens with this command
$ heroku authorizations
Notes:
- Use
heroku authorizations:create
for production apps, useheroku auth:token
for development. Herou doc
Use Heroku API token in Gitlab CI
There are also two ways to do this, you should create .gitlab-ci.yml
at root of your project and Gitlab would scan it automatically.
One way is to install Heroku CLI and config the git repo.
deploy:
image: debian:stretch
stage: deploy
variables:
HEROKU_APP: django
HEROKU_DEPLOYMENT_BRANCH: main
only:
- main
before_script:
- apt-get update -y
- apt-get install -y curl git gnupg
- curl https://cli-assets.heroku.com/install-ubuntu.sh | sh
- |
cat >~/.netrc <<EOF
machine api.heroku.com
login $HEROKU_EMAIL
password $HEROKU_TOKEN
machine git.heroku.com
login $HEROKU_EMAIL
password $HEROKU_TOKEN
EOF
- chmod 600 ~/.netrc
- heroku git:remote --app $HEROKU_APP
script:
- git checkout $CI_COMMIT_SHA
- git push heroku $CI_COMMIT_SHA:$HEROKU_DEPLOYMENT_BRANCH
- You need to set
$HEROKU_EMAIL
and$HEROKU_TOKEN
in Gitlab CI env variable to make it work. - You need to change
$HEROKU_APP
to your Heroku app. - We installed Heroku CLI and then
$HEROKU_TOKEN
in ~/.netrc can make the Heroku CLI can access Heroku API.
Another way is to use a package dpl
to do this.
deploy:
stage: deploy
variables:
HEROKU_APP: django
only:
- main
script:
- gem install dpl
- dpl --provider=heroku --app=$HEROKU_APP --api-key=$HEROKU_TOKEN
After you push the .gitlab-ci.yml
to main branch of Gitlab repo, new commits to main branch would be deployed to Heroku automatically. So you do not need to deploy in local env.
Django Heroku Tutorial Series:
- Heroku vs AWS Which is Best for Your Django project
- How to deploy Django project to Heroku using Docker
- How to deploy Python project to Heroku in Gitlab CI
- How to use Heroku Pipeline
- Heroku Logs Tutorial
- How to monitor Heroku Postgres using heroku-pg-extras
Django Dokku Tutorial Series: