To clone a Git repository and build it using that repository within a GitLab CI/CD pipeline, with the final build being deployed using a Tomcat Docker container, you're going to need to break down this process into several defined steps. This setup involves:
- Defining a pipeline in
.gitlab-ci.yml that builds the Java (Maven) project.
- Creating a Docker image for the deployment on Tomcat.
- Optionally pushing that Docker image to a Docker registry (if you need to deploy it elsewhere).
- Running the Docker image.
Let's go through these steps:
1. GitLab CI/CD Pipeline Configuration for a Maven Project
Your .gitlab-ci.yml file defines your pipeline. In this scenario, we don't need to explicitly clone the repository in your job script because GitLab CI/CD automatically checks out the code before running CI/CD jobs.
First, you need a Docker image with Maven and Java installed to build the project. The official Maven Docker images come with Java installed.
Here's a simple .gitlab-ci.yml example that builds a Maven project:
stages:
- build
- package
- deploy
variables:
MAVEN_CLI_OPTS: "-B -DskipTests=true"
build_app:
image: maven:3.8.1-jdk-11
stage: build
script:
- mvn $MAVEN_CLI_OPTS clean package
artifacts:
paths:
- target/*.war
expire_in: 1 day
2. Building a Docker Image for Tomcat Deployment
To package your application in a Docker image with Tomcat, you need a Dockerfile. Here is an example Dockerfile that copies the WAR file built in the previous step to the webapps directory of the Tomcat image:
# Use the official Tomcat image
FROM tomcat:9.0
# Remove default web applications
RUN rm -rf /usr/local/tomcat/webapps/*
# Copy WAR file to the Tomcat webapps directory
COPY --from=build_app /builds/your-group/your-project-name/target/your-app.war /usr/local/tomcat/webapps/ROOT.war
# Expose the port Tomcat is reachable on
EXPOSE 8080
# Start Tomcat
CMD ["catalina.sh", "run"]
Adjust /builds/your-group/your-project-name/target/your-app.war according to your GitLab group/project name and WAR file name.
3. Adding Docker Image Building and Pushing to the pipeline
Below is an expanded .gitlab-ci.yml including a Docker build and push. You'll need to have a Docker registry where you can push the image. GitLab has its own Container Registry you can use.
...
package:
image: docker:latest
stage: package
services:
- docker:dind
script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker build -t $CI_REGISTRY_IMAGE .
- docker push $CI_REGISTRY_IMAGE
only:
- main
4. Deployment
For deploying the Docker image, it depends on whether the Tomcat container will run on the same machine as the GitLab Runner, a different server, a Kubernetes cluster, etc.
An example script section to stop a running container and start a new one could look like this:
deploy:
image: docker:latest
stage: deploy
script:
- docker pull $CI_REGISTRY_IMAGE
- docker stop my-tomcat-app || true
- docker rm my-tomcat-app || true
- docker run --name my-tomcat-app -d -p 8080:8080 $CI_REGISTRY_IMAGE
only:
- main
Conclusion
This GitLab CI pipeline builds a Maven project, packages it into a Docker image with a Tomcat server, and can optionally push this image to a registry and deploy it. Adapt paths, names, and specific commands to fit your project's and environment's needs.
Remember that running Docker commands in CI/CD jobs requires Docker-in-Docker (docker:dind) service if you're not using a shell executor, which must be set up carefully to avoid security issues. Always follow best practices for security when configuring your CI/CD pipelines.