39

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?

Ben Murden
  • 590
  • 2
  • 6
  • 23
Toanalien
  • 659
  • 1
  • 7
  • 15

4 Answers4

54

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:$HEROKU_API_KEY@git.heroku.com/<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

Gus Costa
  • 609
  • 5
  • 13
Zsolt
  • 541
  • 4
  • 4
  • 16
    Make sure to add `-q` flag while pushing to heroku otherwise you'll reveal your `$HEROKU_API_KEY` at the end of your output. – Fery W Mar 22 '18 at 23:51
  • @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` ? – Théo Champion Jun 12 '18 at 10:45
  • 6
    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 – dancypants Apr 23 '19 at 09:54
33

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.
Community
  • 1
  • 1
dnit13
  • 2,478
  • 18
  • 35
0

To complete dnit13's answer:

Make sure your environment variables are unprotected.

Go to Settings > CI/CD > Environment variables and untick Protected Variable.

More information on this thread.

JayDew
  • 11
  • 1
  • 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." – Asriel Jan 12 '22 at 13:29
0

base on the documentaion of gitlab-ci has deployment tool that use to directly communicate with these providers, Heroku, Cloud Foundry, AWS/S3 etc.

here is the basic usage of dpl for your job script

staging:
  stage: deploy
  script:
    - apt-get update -yq
    - apt-get install -y ruby-dev
    - gem install dpl
    - dpl --provider=heroku --app=my-app-staging --api_key=$HEROKU_STAGING_API_KEY
  only:
    - main

for those seeker here just read this gitlab dpl documentation

Marvin
  • 647
  • 7
  • 15