Store application code on Gitlab

Emanuele Pecorari
3 min readJan 5, 2024

In the preceding article titled “Containerization of an API based on Nest.js using Podman”, we developed the code for an application that exposes a simple API endpoint.

Our current objective is to archive this code in a Source Code Management (SCM) system and subsequently facilitate automated deployment to a Cloud service. This involves constructing continuous deployment (CD) pipelines that will oversee both the code deployment and the configuration of the infrastructure.

The initial phase entails selecting an appropriate SCM tool, and our choice is GitLab.

What is Gitlab

GitLab isn’t just another Git repository manager — it’s a comprehensive platform reshaping how teams handle source code and collaboration. Beyond its role as a version control system, GitLab integrates a suite of DevOps tools, encompassing continuous integration, deployment, and monitoring — all consolidated within a single application. Its user-friendly interface and diverse features make it a practical solution for project management, issue tracking, and implementing CI/CD pipelines. From startups to large enterprises, GitLab simplifies workflows, fostering efficiency and collaboration throughout the software development lifecycle.

Start with Gitlab

Let’s open an account on Gitlab. After the verification of our email and our phone number we can choose to create a new project:

The screen to select to create a new project

and define a group (that can host several projects) and our first project:

Creation of the project

At the end of the project createion we access the dashboard:

Gitlab dashboard

Upload the code to Gitlab

Add SSH key to the account

We can generate a key with the instruction at https://docs.gitlab.com/ee/user/ssh.html. In our case the commands below will generate the RSA keys pair:

From the Gitlab web console we can click on the avatar icon of our account and select “Edit profile”:

then, we choose “SSH keys” on the left menu and “Add new key” in the screen that open.

In our terminal (Mac OS in our case) we run the command:

tr -d '\n' < ~/.ssh/gitlab_key.pub | pbcopy

which copy the content of the public key ready to be pasted in the form open in the Gitlab console in the “Key” field (in our example we call the key “gitlab_key” as shown in the “Title” field below):

We click on the “Add key” button to complete the step.

We can now test the connectivity from our terminal with the following command:

ssh -i ~/.ssh/gitlab_key -T git@gitlab.com

and if everything is correct, a welcome message will appear.

Let’s add the files and perform our first commit (gitlab username and password will be asked to push):

git add .
git commit -m "My message"
git push -uf origin main

If you get an error message saying you’re trying to force a push to a protected branch, you can disable (just for demo sake: avoid this in real projects) the protection going inside the “General” settings of the repository, expanding the “Protected branches” section and clicking the button “Unprotect” on the right.

The code is now stored on Gitlab

The files uploaded on the repository

In the next article we’ll start to build the pipelines to deploy the application on Google Cloud Run service.

--

--

Emanuele Pecorari

Cloud Architect and Tech Product Owner. Soccer player and coach in the free time.