Continuous integration with GitLab CI
In this article, we introduce the basic concepts behind continuous integration and continuous deployment and show how to put them into practice with GitLab CI.

Editorial translation by Impulso.
Introduction
As the IT market has continued to grow, agility has become essential in software development. But agility alone isn’t enough. We also need to work efficiently. DevOps (Developer Operations) emerged as a way to bring business, development, and infrastructure teams together to improve efficiency and shorten the intervals between versions, tests, and releases.
The term emerged in the early 1990s with the rise of agile methodologies, which proposed a new approach to software development. DevOps aims to reduce the bottleneck between the development team (Developers) and the infrastructure team (Operations).
In this context, a vital part of the DevOps process is “continuous delivery (Continuous Integration) (HUMBLE; FARLEY, 2014).” This methodology seeks to break down barriers between developers, testers, business analysts, and infrastructure teams, fostering a culture of cooperation.
Concepts
There are 3 variations on this idea: Continuous Integration (CI), Continuous Deployment (CD), and Continuous Delivery (CDE).
Continuous Integration is the process of automating part of the developers’ work. It takes the commits they make throughout the day, merges them automatically, and runs automated tests and quality checks.
Continuous Delivery automates builds just like Continuous Integration, but a person makes the release decision. In other words, someone decides when to release the application.
Continuous Deployment, in turn, automates the release of these artifacts or services once the version has been properly tested and reviewed and is ready to go out.
The diagram below shows how far each stage goes.

Fig. 1 – The integration, delivery, and deployment layers. Source: Atlasian – 2019
It’s worth pointing out that you shouldn’t confuse CI/CD with DevOps (Developer Operations). CI/CD is certainly part of DevOps culture, but it’s only one part of a culture that involves the entire project team, from support staff to the IT manager.
GitLab and GitLab CI
GitLab is a Git-based repository manager. It includes a wiki, a task manager, and a CI/CD pipeline, which is the focus of this article. GitLab is similar to GitHub. Its open-source code lets you host it on your own infrastructure. A cloud version is also available, and GitLab supports both public and private repositories.
Starting a project
For this article, I created a repository at https://gitlab.com/orlandoburli/hello-gitlab-ci, which we’ll use as our project to test builds and deployments.
The project is simple: a Spring Boot application with a URL that takes a name as a parameter and returns the string "Hello, name!"

Fig. 2 – Application source code
With the project ready, let’s talk about the GitLab CI configuration file,.gitlab-ci.yml.
Important: This file uses the YAML format, which has specific formatting rules, so pay close attention. For indentation, never use TAB, only spaces.
To trigger the GitLab CI build, this file needs to be at the root of the project, as shown below:

Fig. 4 – GitLab CI configuration file
Configuring the pipeline
Now we’ll write the build configuration. The first thing we need to define is the build image. In our case, we’ll use Java 1.8, as shown below:
image: java:8
The second step is to define the stages, or steps in our pipeline. We’ll start by defining the test and build steps (remember to watch your indentation and use 3 spaces to structure the command hierarchy):
stages:
- test
- build
We’ll also add a step before these stages using the before_script command:
before_script:
- chmod +x mvnw
The purpose of this step is to set permissions on the mvnw file, a Maven shell script that builds your project. If you start your project with the Spring starter, for example, it will create these files for you.
Now let’s get to the pipeline commands themselves. First, we’ll define the test command:
test:
state: test
script: ./mvnw test
The commands here are very simple. First, I name the command test. Then I set its stage to test and its script to./mvnw test to run the Maven test command. If the test fails, the build will fail at this step too.
Now let’s define the build command:
build:
stage: build
script: ./mvnw package
artifacts:
paths:
- target/hello-0.0.1-SNAPSHOT.jar
First, we name the command build. We set the stage to build (this will make more sense later in the article), and in its script we add the./mvnw package command to generate the JAR with Maven.
Finally, we define the artifacts, which are the artifacts this build generates and that need to be stored. This GitLab feature lets you store the artifacts or versions generated by your pipeline alongside your repository.
Here’s our complete file:
image: java:8
stages:
- test
- build
before_script:
- chmod +x mvnw
test:
state: test
script: ./mvnw test
Running the pipeline
With that done, we’re ready to test our pipeline. To test it, I’ll push my commit to GitLab, which will automatically trigger the project build.
To see the builds in progress, go to your repository’s home page and click CI / CD > Pipelines in the sidebar. You’ll see your pipelines running, as shown below:

Fig. 4 – Pipeline running at the test stage.
Here we can see why the stages mentioned earlier in the article matter. They give you a clear visual indication of which step of the pipeline is running and let you see which stage failed.
In Fig. 4, we can see the test stage running. Notice that the previous build failed at that very stage.
That makes it easy to see where you need to fix your pipeline.
In Fig. 5, we can see the completed stages.

Fig. 5 – Build complete and artifacts available for download.
We can also see a button to download the artifacts from this build, since they’re now stored in your repository.
Once this pipeline is complete, the next step would be to deploy this artifact to production so it can run. But that’s a topic for a future article.
Conclusion
In this article, we covered the theory behind continuous integration and how to use it with GitLab CI. There are certainly several tools on the market designed for this purpose, and GitLab is a great open-source option. I hope this was helpful. See you next time!
References
https://docs.gitlab.com/ee/ci/examples/deploy_spring_boot_to_cloud_foundry/index.html
You can also read the English version of this post.


