How to deploy Python project to Heroku in Gitlab CI

Table of Contents

Django Heroku Tutorial Series:

  1. Heroku vs AWS Which is Best for Your Django project
  2. How to deploy Django project to Heroku using Docker
  3. How to deploy Python project to Heroku in Gitlab CI
  4. How to use Heroku Pipeline
  5. Heroku Logs Tutorial
  6. How to monitor Heroku Postgres using heroku-pg-extras

Django Dokku Tutorial Series:

  1. How to deploy Django project to Dokku
  2. How to deploy Django project to Dokku with Docker

Introduction

In this Heroku tutorial, I will talk about how to deploy python project to Heroku in Gitlab CI

After reading, you will get:

  1. How to create Heroku token to access the Heroku platform API.
  2. How to deploy the project to Heroku using Heroku CLI
  3. 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.

  1. Please go to Heorku account setting
  2. 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:

  1. Use heroku authorizations:create for production apps, use heroku 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
  1. You need to set $HEROKU_EMAIL and $HEROKU_TOKEN in Gitlab CI env variable to make it work.
  2. You need to change $HEROKU_APP to your Heroku app.
  3. 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:

  1. Heroku vs AWS Which is Best for Your Django project
  2. How to deploy Django project to Heroku using Docker
  3. How to deploy Python project to Heroku in Gitlab CI
  4. How to use Heroku Pipeline
  5. Heroku Logs Tutorial
  6. How to monitor Heroku Postgres using heroku-pg-extras

Django Dokku Tutorial Series:

  1. How to deploy Django project to Dokku
  2. How to deploy Django project to Dokku with Docker
Launch Products Faster with Django

SaaS Hammer helps you launch products in faster way. It contains all the foundations you need so you can focus on your product.

Michael Yin

Michael is a Full Stack Developer from China who loves writing code, tutorials about Django, and modern frontend tech.

He has published some ebooks on leanpub and tech course on testdriven.io.

He is also the founder of the AccordBox which provides the web development services.

Django SaaS Template

It aims to save your time and money building your product

Learn More

Hotwire is the default frontend solution shipped in Rails, this book will teach you how to make it work with Django, you will learn building modern web applications without using much JavaScript.

Read More
© 2018 - 2024 AccordBox