Docs
circleci.com
Start Building for Free

Deploy to Heroku

1 week ago1 min read
Cloud
Server v4.x
Server v3.x
Server v2.x
On This Page

Heroku is a popular platform for hosting applications in the cloud. To configure CircleCI to deploy your application to Heroku, follow the steps below.

Deploy with the Heroku orb

  1. Create a Heroku account and follow the Getting Started on Heroku documentation to set up a project in your chosen language.

  2. Add the name of your Heroku application and your Heroku API key as environment variables as HEROKU_APP_NAME and HEROKU_API_KEY, respectively. To take advantage of secrets masking, it is best practice to set environment variables at the project level or within a context.

  3. Use the Heroku orb to keep your config simple. The deploy-via-git installs the Heroku CLI in the primary container, runs any pre deployment steps you define, deploys your application, then runs any post-deployment steps you define. See the Heroku orb page in the orbs registry for full details of parameters and options:

    version: 2.1
    
    orbs:
        heroku: circleci/heroku@x.y # Use the Heroku orb in your config
    
    workflows:
        heroku_deploy:
        jobs:
            - build
            - heroku/deploy-via-git:
                requires:
                - build # only run deploy-via-git job if the build job has completed
                filters:
                branches:
                    only: main # only run deploy-via-git job on main branch

For more detailed information about these Heroku orbs, refer to the CircleCI Heroku Orb.

Heroku deployment with 2.0 configuration

  1. Create a Heroku account and follow the Getting Started on Heroku documentation to set up a project in your chosen language.

  2. Add the name of your Heroku application and your Heroku API key as environment variables as HEROKU_APP_NAME and HEROKU_API_KEY, respectively. To take advantage of secrets masking, it is best practice to set environment variables at the project level or within a context.

  3. In your .circleci/config.yml, create a deployment job and add an executor type.

  4. Add steps to your deployment job to checkout and deploy your code. You can specify which branch you would like to deploy, in this example we specify the main branch and deploy using a git push command.

    version: 2
    
    jobs:
        build:
        ...
        deploy:
        docker:
            - image: <docker-image-name-tag>
            auth:
                username: mydockerhub-user
                password: $DOCKERHUB_PASSWORD  # context / project UI env-var reference
        steps:
            - checkout
            - run:
                name: Deploy Main to Heroku
                command: |
                git push https://heroku:$HEROKU_API_KEY@git.heroku.com/$HEROKU_APP_NAME.git main
    
    workflows:
        version: 2
        build-deploy:
        jobs:
            - build
            - deploy:
                requires:
                - build # only run deploy-via-git job if the build job has completed
                filters:
                branches:
                    only: main # only run deploy-via-git job on main branch

Help make this document better

This guide, as well as the rest of our docs, are open source and available on GitHub. We welcome your contributions.

Need support?

Our support engineers are available to help with service issues, billing, or account related questions, and can help troubleshoot build configurations. Contact our support engineers by opening a ticket.

You can also visit our support site to find support articles, community forums, and training resources.