How to deploy to Heroku directly from my Gitlab repository

50,575

Solution 1

If you are not prepared to use Ruby/dpl you can deploy to Heroku as follows:

Look up your Heroku API key (Account settings -> API Key on the Heroku web console) and make it available as a Gitlab secret variable e.g. HEROKU_API_KEY (Please note the values is not the same as what heroku auth:token returns...)

Then add two script lines in your .gitlab-ci.yml config file at the relevant job:

git remote add heroku https://heroku:[email protected]/<name of your heroku app>.git

git push -f heroku HEAD:master

You can see detailed explanation at http://blog.thecodewhisperer.com/permalink/deploying-jekyll-to-heroku-using-gitlab-ci

Solution 2

Here is the solution I found , restating in case the link is broken:

Configure project

This is what the .gitlab-ci.yml file looks like for this project:

test:
  script:
  # this configures Django application to use attached postgres database that is run on `postgres` host
  - export DATABASE_URL=postgres://postgres:@postgres:5432/python-test-app
  - apt-get update -qy
  - apt-get install -y python-dev python-pip
  - pip install -r requirements.txt
  - python manage.py test

staging:
  type: deploy
  script:
  - apt-get update -qy
  - apt-get install -y ruby-dev
  - gem install dpl
  - dpl --provider=heroku --app=gitlab-ci-python-test-staging --api-key=$HEROKU_STAGING_API_KEY
  only:
  - master

production:
  type: deploy
  script:
  - apt-get update -qy
  - apt-get install -y ruby-dev
  - gem install dpl
  - dpl --provider=heroku --app=gitlab-ci-python-test-prod --api-key=$HEROKU_PRODUCTION_API_KEY
  only:
  - tags

This project has three jobs:

test - used to test Django application,

staging - used to automatically deploy staging environment every push to master branch

production - used to automatically deploy production environmnet for every created tag

Store API keys

You'll need to create two variables in Project > Variables:

HEROKU_STAGING_API_KEY - Heroku API key used to deploy staging app,
HEROKU_PRODUCTION_API_KEY - Heroku API key used to deploy production app.
Share:
50,575
Toanalien
Author by

Toanalien

Hi. I'm Vietnamese, Linux user and web developer especial in PHP, NodeJS, JavaScript, AngularJS, Mongodb, JQuery. I’m Sysadmin, UI/UX Designer, Interaction Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better. If you like my article, Please like it or share it to appreciate my effort. More Information, contact me [email protected]

Updated on July 02, 2020

Comments

  • Toanalien
    Toanalien almost 4 years

    In my team, we use Gitlab as a remote repository, so we are looking for a solution to auto deploy our apps to Heroku. We found Codeship for auto deploying apps to Heroku from Github.

    Any tips? Tricks?

  • Cynthia Sanchez
    Cynthia Sanchez almost 7 years
    Hi dnit13, where exactly is that path Project > Variables ? Im not finding it in Gitlab
  • dnit13
    dnit13 almost 7 years
    @CynthiaSanchez As described by Zsolt below, you will find the api key in your heroku account
  • Fery W
    Fery W about 6 years
    Make sure to add -q flag while pushing to heroku otherwise you'll reveal your $HEROKU_API_KEY at the end of your output.
  • Théo Champion
    Théo Champion almost 6 years
    @Zsolt When I do that, the push is empty (the heroku build therefore fail), what I am missing here ? Do I need to make a dummy commit to init heroku:master ?
  • dancypants
    dancypants about 5 years
    running git remote add heroku ... in .gitlab-ci.yml sometimes causes fatal: remote heroku already exists changed it to git remote add heroku ... || true to force return code to 0
  • MrKioZ
    MrKioZ over 4 years
    It's at Settings > CI/CD > Environment variables
  • Asriel
    Asriel over 2 years
    This is not really a good idea. Some variables should be kept secret. A better answer would have been: "Make sure to only use protected variables on protected branches."