CI/CD Made Easy: Using GitLab’s Templates to Build Your First Pipeline

Akash Patel
5 min readOct 15, 2024

--

“DEVOPS” is a term we hear often, and the first thing which comes to our mind is CI/CD. As DevOps Engineers we create CI/CD pipeline to automated several key aspects of software development, particularly the integration, testing, and deployment of code changes.

If you are interested in how CI/CD pipeline and want to try yourself but don’t know where to start, this article will help you get started.

In this tutorial, we will not create CI/CD pipeline file from scratch. Instead, we will use templates available in the official GitLab Repository, which can be accessed through the link below.

GitLab Repository Templates Link

The Concept

Lets us understand what “.gitlab-ci.yml” file is and how it works.

The “.gitlab-ci.yml” file is a configuration file used in GitLab's Continuous Integration and Continuous Delivery (CI/CD) environment. It defines the structure and behaviour of CI/CD pipelines for projects hosted on GitLab.

The primary purpose of the “.gitlab-ci.yml” file is to specify the jobs that should be executed during the CI/CD process, as well as their order and conditions for execution. This file must be located at the root of your GitLab repository, and it is written in YAML syntax, which allows for clear organization and readability.

Below is an example of a basic .gitlab-ci.yml file.

# Define the stages of the pipeline
stages:
- build
- test
- deploy

# Job to build the application
build-job:
stage: build
image: node:14 # Use Node.js version 14
script:
- echo "Installing dependencies..."
- npm install
- echo "Building the application..."
- npm run build
artifacts:
paths:
- dist/ # Store the build artifacts for later stages

# Job to run tests
test-job:
stage: test
image: node:14 # Use Node.js version 14
script:
- echo "Running tests..."
- npm test

# Job to deploy the application
deploy-job:
stage: deploy
image: node:14 # Use Node.js version 14
script:
- echo "Deploying the application..."
- npm run deploy
only:
- main # Only deploy from the main branch

The Start

You can either create your own project or use the project link I’m using for this tutorial. After downloading, unzip the project.

Project Download Link

Pre Requisites

  1. Gitlab Account (If you don’t have one, you can create a GitLab.com account using this link).
  2. Docker (Optional, but I’m using it for this tutorial).

Step 1. Go to project by running the following commands.

cd dummy-spring-boot

Step 2. Next, you need to create a Dockerfile. Don’t worry if you don’t know how to create a Dockerfile — run the following command, and it will create one for you. (The docker init command creates a Dockerfile for more information, refer to Docker CLI).

sudo docker init

Step 3. Since its a Spring project, select Java and hit enter.

Step 4. Enter your src path if its in another directory. Since it’s in current folder, just hit enter.

Step 5. Enter your java version for us it’s 21.

Step 6. For server port, enter 9090. (For this project only, by default Spring Boot uses port 8080.)

And its done, you have created a Dockerfile!

Step 7. Next create a file name “.gitlab-ci.yml” and paste the following contents.

include:
- template: Docker.gitlab-ci.yml

Note:- Above configuration uses contents from this link. (You can change file name as per your project and will still work.)

And all the variables defined in that file are predefined which you can find using this link.

Step 8. Create a blank project and push it to your GitLab repository.

Step 9. Go to the project, click on Build and then Pipelines. (You should see an output similar to the one shown below.)

Step 10. Once you see “Passed” go to the Container Registry by clicking on Deploy, and then Container Registry on the left side of the screen.

Now, if you click on the Docker image name (for us, it’s ci-cd-demo), you will see an output similar to the image below.

In the above image, you can see two docker images tags one is “latest” and the other is “main” (which is project branch name).

Conclusion

Now, we need to run and check if docker image is working or not.

Step 1. Copy docker image name by clicking on copy icon as shown in below image. (You can choose anyone as they both are the same.)

Step 2. Open terminal or cmd and run this below command.

#For us
docker run -it -p 9090:9090 <docker-image-name:tag>
#For others
docker run -it -p 9090:9090 <docker-image-name:tag>

First, it will download image as shown in below image.

Then your service will start as shown below.

Step.3 Open browser and enter below URL.

localhost:9090

And you will see similar output.

If you are getting this output, then congratulations you have successfully configured GitLab CI/CD pipeline.

Feel free to comment below if you are stuck anywhere in this tutorial. I will surely try to help.

--

--

No responses yet