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

Final code

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.

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

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:

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.

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!