Building Our Pipeline

To quickly release new features, we need to concentrate on our deployment pipeline. When we push our code to our remote repository, we want GitLab to autodeploy our project to Heroku. In order to do this, we must enhance our Django project to a proper Heroku app and add a .gitlab-ci.yml file.

Adding GitLab CI instructions

Now that our Heroku requirements are fulfilled, we need to tell GitLab to deploy our code to Heroku on push. We do this by adding a CI/CD pipeline to our remote repository. GitLab creates this pipeline automatically when a .gitlab-ci.yml file is present in the root of the repository.

Our .gitlab-ci.yml contain instructions about connecting with Heroku. The authorization will be done by an env variable named HEROKU_PRODUCTION_API_KEY.

Get your Heroku API key

You can find your Heroku API key (aka. auth token) in your Heroku account settings. You can also retrieve it with the commandline.

Copy the API key to your clipboard:

Terminal
heroku auth:token | pbcopy

Add the Heroku API key to GitLab

To add custom env variables to your GitLab repo, we must go to the settings of our repository on gitlab.com.

Browser (any window)
  1. Go to your project’s Settings > CI/CD and expand the Variables section.

  2. Click the Add Variable button.

  3. Add a variable named HEROKU_API_KEY with your Heroku API key (see above) as value.

You may need to delete the newline the auth token, when you used pbcopy to add it to your clipboard.

Create the .gitlab-ci.yml file

Back in the terminal, create the gitlab-ci.yml file:

Terminal
touch .gitlab-ci.yml
Don’t forget the . at the beginning of the file name.

Add the Heroku deploy stage to the .gitlab-ci.yml file:

.gitlab-ci.yml
---
// Important: Change {appname} to your Heroku app name
heroku:
  type: deploy
  script:
  - apt-get update -qy
  - apt-get install -y ruby-dev
  - gem install dpl
  - dpl --provider=heroku --app={appname} --api-key=$HEROKU_API_KEY
---
This is a very basic example of a .gitlab-ci.yml file. Feel free to let it run tests or only deploy if you push to a specific branch.

See the GitLab CI/CD documentation for more information.

Commit the code

If you run git status you will see, that we updated requirements.txt, and added Procfile and .gitlab-ci.yml.

Now we can stage and commit our changes:

Terminal
git add .
git commit -m "Add deployment pipeline ☁️"

We did not push our code yet. There will be a special chapter for this.

Checklist

✔︎ .gitlab-ci.yml is present

results matching ""

    No results matching ""