GitLab Continuous Integration Docker Deployment to Heroku

0
4448
views

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.

stages: 
    - package
    - push
    - deploy

.extendjob:  
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker build -t registry.gitlab.com/<your-gitlab-username>/<your-docker-image-name> .
    - docker push registry.gitlab.com/<your-gitlab-username>/<your-docker-image-name>:latest

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


push_to_heroku:
  image: docker:latest
  stage: push
  services:
  - docker:dind
  extends: .extendjob
  script:
    # This is for gitlab
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker pull  registry.gitlab.com/<your-gitlab-username>/<your-docker-image-name>:latest
    # This is for heroku
    - docker login --username=_  --password=<heroku-auth-token> registry.heroku.com
    - docker tag registry.gitlab.com/<your-gitlab-username>/<your-docker-image-name>:latest registry.heroku.com/aqueous-falls-31318/web:latest
    - docker push registry.heroku.com/<your-heroku-app-name>/web:latest


deploy_to_heroku:
  image: node:latest
  stage: deploy
  services:
  - docker:dind
  extends: .extendjob
  script:
    - 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..!