This project demonstrates how to deploy a simple Java web application using Jenkins and Apache Tomcat, hosted on two separate EC2 instances. The CI/CD pipeline automates code retrieval, build, and deployment using Maven and Jenkins.
| Component | Purpose |
|---|---|
| EC2 Instance #1 | Jenkins Server (CI/CD) |
| EC2 Instance #2 | Apache Tomcat Server |
Source code is hosted on GitHub:
π first-java-webapp
- Launch two Ubuntu EC2 instances.
- Assign Elastic IPs.
- Open ports:
- Jenkins:
8080 - Tomcat:
8080 - SSH:
22
- Jenkins:
-
Access Jenkins at
http://<JENKINS_EC2_IP>:8080 -
Install required plugins:
- Git
- Maven Integration
- Deploy to Container
-
Configure Maven under Manage Jenkins β Global Tool Configuration with name:
mymaven -
Add Jenkins credentials:
- ID:
tomcat - Username:
tomcat - Password:
********
- ID:
- Access Tomcat at
http://<TOMCAT_EC2_IP>:8080
pipeline {
agent any
tools {
maven "mymaven"
}
stages {
stage('Code from GitHub') {
steps {
git branch: 'main', url: 'https://github.com/nagarjuna281/first-java-webapp.git'
}
}
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Deploy to Tomcat') {
steps {
deploy adapters: [
tomcat9(
alternativeDeploymentContext: '',
credentialsId: 'tomcat',
path: '',
url: 'http://<TOMCAT_EC2_IP>:8080'
)
],
contextPath: 'myapp',
war: 'target/*.war'
}
}
}
}- Open Jenkins dashboard.
- Click New Item.
- Enter a name (e.g.,
first-java-webapp) and select Pipeline, then click OK. - Scroll down to the Pipeline section.
- Choose Pipeline script and paste the Jenkinsfile code above.
- Click Save.
- Click Build Now to trigger the pipeline.
- Jenkins pulls code from GitHub.
- Maven builds the
.warfile. - Jenkins deploys the
.warto Tomcat using the Deploy to Container plugin.
Once deployed, your app will be available at:
http://<TOMCAT_EC2_IP>:8080/myapp