-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile-ec2
75 lines (68 loc) · 2.06 KB
/
Jenkinsfile-ec2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
pipeline {
agent any
environment {
DOCKER_IMAGE = 'manjuntha1963/my-java-app' // Full Docker image name
EC2_HOST = '54.234.97.114' // Replace with your EC2 instance's public IP
EC2_USER = 'ubuntu' // EC2 username (e.g., 'ubuntu' for Ubuntu)
BRANCH = 'master' // Configurable branch
}
stages {
stage('Checkout') {
steps {
// Checkout code from GitHub
git branch: BRANCH, url: 'https://github.com/manjuntha1963/springboot.git'
}
}
stage('Build with Maven') {
steps {
script {
echo 'Running Maven build...'
sh 'mvn clean package'
}
}
}
stage('Build Docker Image') {
steps {
script {
echo 'Building Docker image...'
sh "docker build -t $DOCKER_IMAGE ."
}
}
}
stage('Push Docker Image to Docker Hub') {
steps {
script {
echo 'Pushing Docker image to Docker Hub...'
docker.withRegistry('', 'docker-hub-credentials') {
sh "docker push $DOCKER_IMAGE"
}
}
}
}
stage('Deploy to EC2') {
steps {
script {
echo 'Deploying to EC2 instance...'
}
sshagent(['ssh-key-id']) {
sh """
ssh -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST <<EOF
docker pull $DOCKER_IMAGE:latest
docker stop my-java-app || true
docker rm my-java-app || true
docker run -dp 8090:8080 --name my-java-app $DOCKER_IMAGE:latest
EOF
"""
}
}
}
}
post {
success {
echo 'Deployment successful!'
}
failure {
echo 'Build or Deployment failed!'
}
}
}