DEV Community

Kyle Schwartz
Kyle Schwartz

Posted on

4

Integrating GitLab CI/CD, Flask, and Heroku

This tutorial assumes you already have a working flask application within a virtual environment and have your code hosted on GitLab. Also ensure you have Gunicorn installed. You can install it from pip install gunicorn.

Getting Ready

Run pip freeze > requirements.txt to grab all the requirements for running your application.

Create a Procfile file. Inside, place the following code web: gunicorn <name of the app.py file>:<app-name> where app-name is usually “app”.

Push everything to GitLab.

Connecting Everything

Create a new Heroku application. Using a short name will make later steps easier.

Create a new file in your app root called .gitlab-ci.yaml.

Here is a cookie-cutter example of a GitLab CI/CD config:

[app.py file name]:
    script:
        — apt-get update -qy
        — apt-get install -y python-dev python-pip
        — pip install -r requirements.txt

production:
    type: deploy
    script:
        — apt-get update -qy
        — apt-get install -y ruby-dev
        — gem install dpl
        — dpl — provider=heroku — app=[Heroku App Name] — api-key=$HEROKU_SECRET_KEY
    only:
        — master
Enter fullscreen mode Exit fullscreen mode

Now swap out [app.py file name] with your file name and [Heroku App Name] with the name of your Heroku app.

To set the HEROKU_SECRET_KEY,

  • Get your Heroku API key from https://dashboard.heroku.com/account.

  • Within GitLab, go to Settings → CI / CD → Secret variables.

  • Create a new variable and set the Variable key to HEROKU_SECRET_KEY and the Variable Value to your Heroku API Key.

  • Set the protected switch to ON.

Push the changes to GitLab.

Conclusion

Now that everything is all setup, head over to your Heroku app URL and enjoy the fruits of your labour.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (1)

Collapse
 
hhsm95 profile image
Hugo Sandoval

Thanks bro, this is really helpful. I only changed:

apt-get install -y python-dev python-pip
pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

with python3:

apt-get install -y python3-dev python3-pip
pip3 install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay