Saturday, June 6, 2026

How to Build and Deploy a Java Web App Using GitLab CI/CD | GitLab Pipeline Tutorial

Introduction - Gitlab CICD Pipeline

In this lab, we will create a GitLab CI/CD pipeline that automatically builds a Java web application using Maven and deploys the generated WAR file to Tomcat.

Pipeline flow:

Developer → GitLab Repository → GitLab CI/CD Pipeline → GitLab Runner → Maven Build → WAR Artifact → Deploy → Tomcat → Web Application

What is GitLab CICD?

GitLab CI/CD automates the software delivery process whenever developers push code to a GitLab repository.

A GitLab CI/CD pipeline can automate:

  • Building the application
  • Running tests
  • Performing security and code-quality scans
  • Creating artifacts
  • Deploying the application

The .gitlab-ci.yml file defines the GitLab CI/CD pipeline and is stored in the root of your GitLab repository.

It defines:

  • Pipeline stages
  • Jobs
  • Container images
  • Commands/scripts
  • Variables
  • Artifacts
  • Deployment instructions

When code is pushed to GitLab, GitLab reads the .gitlab-ci.yml file and schedules the jobs for execution by a GitLab Runner.

GitLab automatically reads this file whenever code changes are pushed into the repository. In this lab, GitLab Runner uses a Docker container image to run the job,

What is a GitLab Runner?

A GitLab Runner is the execution engine that actually runs the CI/CD jobs defined in .gitlab-ci.yml.

In this lab, the runner uses different Docker images for different jobs:

Build job: maven:3.8.6-eclipse-temurin-11
Deploy job: curlimages/curl:latest

Pre-requisites:

Configure Tomcat credentials in GitLab

Add Tomcat user, Tomcat Password and Tomcat URL as Variables in GitLab
Go to your GitLab Repo --> Click on Settings --> CICD --> Variables

Create .gitlab-ci.yml for implementing CICD using GitLab

stages:

  - build

  - deploy

build_war:

  stage: build

  image: maven:3.8.6-eclipse-temurin-11

  script:

    - echo "Building WAR file using Maven"

    - mvn clean install -f MyWebApp/pom.xml

    - echo "Listing target directory"

    - ls -la MyWebApp/target

  artifacts:

    paths:

      - MyWebApp/target/*.war

    expire_in: 1 hour

deploy_to_tomcat:

  stage: deploy

  image: curlimages/curl:latest

  dependencies:

    - build_war

  script:

    - echo "Deploying WAR file to Tomcat running on AWS EC2"

    - |
      curl -v -u ${TOMCAT_USER}:${TOMCAT_PASSWORD} \
      -T MyWebApp/target/MyWebApp.war \
      "http://${TOMCAT_HOST}/manager/text/deploy?path=/MyWebApp&update=true"

You can access Pipeline:


What happens when the pipeline runs?

1. Developer pushes code → GitLab

GitLab detects .gitlab-ci.yml and creates a pipeline.

2. GitLab Runner picks up build_war

The Runner starts the Maven container:

maven:3.8.6-eclipse-temurin-11

Maven compiles, tests, packages the application and generates:

MyWebApp.war

3. WAR is stored as a GitLab artifact

MyWebApp/target/MyWebApp.war

4. Runner executes deploy_to_tomcat

A lightweight curl container is started:

curlimages/curl:latest

5. WAR is deployed to Tomcat

The pipeline calls the Tomcat Manager API and uploads the WAR file.

Verify the deployment

After the pipeline completes successfully, open:

http://<TOMCAT-PUBLIC-DNS>:8080/MyWebApp

You should see the deployed Java web application.

You can access lab video:

1 comment:

How to Configure GitLab Runner with Docker Executor on Ubuntu 24.04 EC2

How to Configure GitLab Runner with Docker Executor on Ubuntu 24.04 EC2 In this tutorial, we will configure a self-hosted GitLab R...