DEV Community

Michael Yin
Michael Yin

Posted on

How to deploy Python project to Heroku in Gitlab CI

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

There are some little difference for you to create token in the account settings page and Heroku CLI

  1. If you have changed your Heroku account password, the token created in account settings page would expire.

  2. You can create multiple tokens (one token for one project) and add description using Heroku CLI.

So I would recommend to use Heroku CLI to create token for your Heorku API.

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: master
  only:
    - master
  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 master
    - git push heroku master:$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:
    - master
  script:
    - gem install dpl
    - dpl --provider=heroku --app=$HEROKU_APP --api-key=$HEROKU_TOKEN

After you push the .gitlab-ci.yml to master branch of Gitlab repo, new commits to master branch would be deployed to Heroku automatically. So you do not need to deploy in local env.

Conclusion

In this Heroku tutorial, I talked about how to deploy project to Heroku in Gitlab CI, you can change the config code to make it work on other CI.

If you have any question, please feel free to contact us.

Reference

This article was originally posted on How to deploy Python project to Heroku in Gitlab CI

Top comments (0)