Here below is the code for creating GitLab CICD yaml file for Java Web App project to automate build and deployment.
What is GitLab CICD?
GitLab CI/CD is a continuous integration and continuous deployment solution built into GitLab.
GitLab CI/CD Pipeline Flow
Developer Commit → GitLab Repository → GitLab Runner → Build Stage → Test Stage → Security Scan → Docker Build → Push to Registry → Deploy to Kubernetes/Tomcat/Cloud
What is .gitlab-ci.yml?
The .gitlab-ci.yml file is the heart of GitLab CI/CD pipelines.
It contains:
- Pipeline stages
- Jobs
- Scripts
- Variables
- Artifacts
- Deployment instructions
GitLab automatically reads this file whenever code changes are pushed into the repository.
.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"



