Cloud Native 12 min read

CI/CD Pipeline Templates for Java Projects Using GitLab and Kubernetes

This guide explains how to organize GitLab CI/CD job templates, define global variables, configure build, test, code analysis, artifact, and deployment stages, and integrate Kubernetes deployment and Docker image creation for Java applications, providing complete YAML examples and usage instructions.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
CI/CD Pipeline Templates for Java Projects Using GitLab and Kubernetes

The repository follows a simple directory layout where the jobs folder stores individual job templates and the templates folder contains the base pipeline template ( default-pipeline.yml) used by all jobs.

Job Templates

Four main job groups are defined:

Build jobs ( jobs/build.yml) – includes standard Maven/NPM/Gradle builds and a Docker image build stage.

Test jobs ( jobs/test.yml) – runs unit tests and publishes JUnit reports.

Code analysis jobs ( jobs/codeanalysis.yml) – executes SonarQube scans with configurable parameters.

Artifact jobs ( jobs/artifactory.yml) – uploads and downloads artifacts from an Artifactory repository.

Deploy jobs ( jobs/deploy.yml) – applies Kubernetes manifests and performs rollbacks.

Each job template uses the rules keyword to enable or disable execution based on pipeline variables such as RUN_PIPELINE_BUILD, RUN_CODE_ANALYSIS, and RUN_DEPLOY_K8S.

Default Pipeline Template

The templates/default-pipeline.yml file assembles the pipeline with the following sections:

include:
  - project: 'cidevops/cidevops-newci-service'
    ref: master
    file: 'jobs/build.yml'
  - project: 'cidevops/cidevops-newci-service'
    ref: master
    file: 'jobs/test.yml'
  - project: 'cidevops/cidevops-newci-service'
    ref: master
    file: 'jobs/codeanalysis.yml'
  - project: 'cidevops/cidevops-newci-service'
    ref: master
    file: 'jobs/deploy.yml'
  - project: 'cidevops/cidevops-newci-service'
    ref: master
    file: 'jobs/artifactory.yml'

Global variables are defined under variables: to control repository paths, Docker image names, SonarQube credentials, and Kubernetes deployment parameters.

The workflow: section restricts pipeline execution to merge‑request events, web triggers, and excludes version‑branch commits, while the stages: list orders the execution order (build, test, code_analysis, deploy‑artifact, deploy‑feature, rollout‑feature, etc.).

Example Build Job

build:
  image: ${BUILD_IMAGE}
  extends: .build

Kubernetes Deployment

.deploy_k8s:
  stage: deploy
  script:
    - kubectl config set-cluster my-cluster --server=${KUBE_URL} --certificate-authority="${KUBE_CA_PEM_FILE}"
    - kubectl config set-credentials admin --token=${KUBE_TOKEN}
    - sed -i "s#__namespace__#${NAMESPACE}#g" ${DEPLOY_FILE}
    - kubectl apply -f ${DEPLOY_FILE}
  environment:
    name: "${ENV_NAME}"
    url: "http://${ENV_NAME}.${CI_PROJECT_NAMESPACE}.${CI_PROJECT_NAME}.devops.com"
  rules:
    - if: "$RUN_DEPLOY_K8S == 'no'"
      when: never
    - when: manual

The guide also shows how to configure feature, UAT, staging, and production environments with corresponding rollout jobs, and provides sample variable settings for a Java project (Maven build, SonarQube analysis, Docker image creation, and Kubernetes deployment).

Overall, the document serves as a comprehensive reference for setting up a cloud‑native CI/CD pipeline that automates building, testing, scanning, artifact management, and Kubernetes‑based deployment of Java applications.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javaci/cdDevOpsGitLab
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.