How to do Deployment with GitLab | CI/CD with GitLab Part 2

Hello, In the second part of our GitLab CI/CD tutorials, I am going to show you an example of how to do deployment with GitLab. We will deploy a NodeJS app on a cloud provider. For this example, I am using Heroku, a cloud platform as a service supporting several programming languages.

For the sample application, we’ll continue using https://gitlab.com/canberkakduygu/demoproject

In our previous tutorial, we only executed API tests against our application which runs on the GitLab system. Now we are going to deploy this app on the cloud. For this purpose, we need to change some part of the source code.

Modifying the Code

Hostname and port were hardcoded in my previous example. As Heroku will host our application on a different host and will bind another port we’ll make some small change so host and port are dynamically bound. So we change our server.js file.

Previous code

const port = 3000; 
var server_host = 127.0.0.1;

Final code

const port = process.env.PORT || 3000; 
var server_host = process.env.YOUR_HOST || '0.0.0.0';

That way it’ll still listen to port 3000 when you test locally, but it will also work on Heroku.

Changing Package.json

You need to add your node and npm versions in the package.json file so Heroku knows which one to use. This kind of configuration is specific to the hosting platform. For AWS or some other provider might ask you to modify it differently.

 "engines": {
    "node": "8.1.1",
    "npm": "5.0.3"
  },

We also need to provide a start script so Heroku knows how to start your application. We add below line in the package.json file

"start": "node server.js"

Modify YML File for Deployment with GitLab

gitlab-ci.yml file keeps your pipeline information. In tutorial part 1 we added our testing stage, now we are going to add the deployment stage. In order to achieve it, we add a new stage under stages tag. We define our stages as below:

stages:
  - build-test
  - production

We specify the usage of the master branch on the only tag. Then provide necessary deployment scripts with Heroku APP and API_KEY variable. Those variables are specific to your app. You should never provide that information in the source code. You can define them in GitLab’s CI/CD Environment Variable page.

production:
  type: deploy
  stage: production
  image: ruby:latest
  script:
    - apt-get update -qy
    - apt-get install -y ruby-dev
    - gem install dpl
    - dpl --provider=heroku --app=$APP --api-key=$API_KEY
  only:
    - master

Deployment with GitLab

Now push everything to git. Your pipeline will trigger and in a few minutes, your application will be tested first and then deployed to Heroku Cloud. Now trigger an API call via browser URL and see the result.

Gitlab deployment

Remember that every cloud provider has its own way of deployment process so you need to get the basic of it and change your git-lab.yml file according to it.

Happy Deployments!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.