GitLab Continuous Integration Docker Deployment to Heroku


Continuous Integration is something like building the pipeline to do continuous delivery and deployment. You have built and deployed your application in some hosting service. What if any minor or major changes will come to your application. Now you will have to again develop,test, build and deploy your application. It’s gonna be a tedious and time consuming job to do the same process again and again.

If we build a pipeline we don’t have to do the same hassle again. All we need to do is push our code to GitLab and rest of the things are taken care of by GitLab CI. GitLab CI will deploy your code to staging or production branch if everything is green with that branch.

In my previous blog I explained about Docker Deployment Angular 5 Application to Heroku

We will do the same process via GitLab CI. As I said earlier all you need to do is push your code to GitLab that’s it! rather than going through so many commands while waiting.

GitLab Continuous Integration Docker Deployment to Heroku

Create .gitlab-ci.yml file

This file is something like building our pipeline for continuous deployment.

create a .gitlab-ci.yml file in the root of your project structure.

    - package
    - push
    - deploy

    - docker build -t<your-gitlab-username>/<your-docker-image-name> .
    - docker push<your-gitlab-username>/<your-docker-image-name>:latest

  image: docker:latest
  - docker:dind
  stage: package
  extends: .extendjob  

  image: docker:latest
  stage: push
  - docker:dind
  extends: .extendjob
    # This is for gitlab
    - docker pull<your-gitlab-username>/<your-docker-image-name>:latest
    # This is for heroku
    - docker login --username=_  --password=<heroku-auth-token>
    - docker tag<your-gitlab-username>/<your-docker-image-name>:latest
    - docker push<your-heroku-app-name>/web:latest

  image: node:latest
  stage: deploy
  - docker:dind
  extends: .extendjob
    - npm install -g heroku
    - heroku container:release web --app <your-heroku-app-name>

As you can see in the above code we are dividing deployment process into 3 stages. Package, Push and Deploy.

.extendjob: it works something like inheritance we can run same script line commands again wherever we need.

Once you push your code to gitlab automatically detects .gitlab-ci.yml file and starts running the batch processes.

GitLab Continuous Integration Docker Deployment to Heroku

Package stage:

we are building docker image and pushing it to the gitlab container registry.

Push stage:

We are logging in to the gitlab and pulling our image. After that we are logging into the Heroku and pushing our docker image to heroku container registry.

Here we need a heroku auth token you can generate it by running the command.

heroku auth:token

copy the generated token and put it in the required line of gitlab-ci.yml file.

Deploy stage:

We are releasing the docker image from heroku container registry.

If all the three stages went well we were able to see our app up and running live.

You can check it by visiting to the your app url in the browser.

You can check this 3 stage deployment process in gitlab CI section.

I hope this helped.

GitLab Continuous Integration Docker Deployment to Heroku

Happy Coding..!