How to deploy to Heroku directly from my Gitlab re

2020-02-07 17:19发布

问题:

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?

回答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:$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



回答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.