Deploy Docker image to Heroku with CicleCI

Heroku is a functional PaaS that can deploy the Docker image, and CircleCI is also useful SaaS for CI/CD.

In this post, I show an example of CircleCI config which deploys the Docker image to Heroku.

Actually, I had trouble when setting it up. 🤣

Prerequisite

  • Code repository and CircleCI had already connected
  • Created Heroku app for deploying the Docker image
  • Generated Heroku API Key in Manage Account view on Heroku

Set up

Environment Variable

Set environment variables on your project settings in CircleCI.

Name Value
HEROKU_API_KEY Your Heroku API Key
HEROKU_APP_NAME Heroku app name for deploying the Docker image

config.yml

Modify config.yml below.

jobs:
  # Build the docker image and run tests.
  build_and_test:
    docker:
      - image: cimg/base:2021.07
    steps:
      # For using docker commands.
      - setup_remote_docker:
          version: 19.03.13

      # Build the docker image
      - checkout
      - run: docker build -t webapp .

      # Run tests.
      - run:
          command: docker run webapp python3 tests.py

  # Build the docker image and deploy it to Heroku.
  build_and_deploy:
    docker:
      - image: cimg/base:2021.07
    steps:
      # For using docker commands.
      - setup_remote_docker:
          version: 19.03.13

      # Build the docker image
      - checkout
      - run: docker build .

      # Deploy to Heroku
      - heroku/install
      - run: heroku container:login
      - heroku/push-docker-image:
          process-types: web
      - heroku/release-docker-image:
          process-types: web

orbs:
  # For using Heroku CLI commands.
  heroku: circleci/[email protected]

version: 2.1
workflows:
  heroku_deploy:
    jobs:
      - build_and_test
      - build_and_deploy:
          requires:
            # Deploy after testing.
            - build_and_test

Test

Example codes are in this repository.

Refs.

Thanks!😆

26