-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
87 lines (75 loc) · 2.73 KB
/
Jenkinsfile
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
76
77
78
79
80
81
82
83
84
85
86
87
pipeline {
agent any
environment {
DOCKER_IMAGE = 'my-reddit-clone'
DOCKER_REGISTRY = 'yashs3324'
REMOTE_HOST = '13.53.196.171'
REMOTE_USER = 'ec2-user'
GITHUB_BRANCH = 'master'
}
stages {
stage('Code Checkout') {
steps {
git branch: '${GITHUB_BRANCH}',
url: 'https://github.com/yashs33244/iiitu-today.git'
}
}
stage('Test') {
steps {
sh 'echo "Running tests..."'
}
}
stage('Build Docker Image') {
steps {
script {
// Build on worker node
sh """
docker build -t ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest .
"""
}
}
}
stage('Push to Registry') {
steps {
script {
withCredentials([usernamePassword(credentialsId: 'docker-hub-credentials',
passwordVariable: 'DOCKER_PASSWORD',
usernameVariable: 'DOCKER_USERNAME')]) {
sh """
echo ${DOCKER_PASSWORD} | docker login -u ${DOCKER_USERNAME} --password-stdin
docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest
"""
}
}
}
}
stage('Deploy') {
steps {
script {
sshagent(['jenkins-ssh-key']) {
sh """
ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} '
cd /path/to/your/app
# Stop the running containers
docker-compose down
# Remove old images (keeping volumes intact)
docker image prune -f
# Pull the latest image
docker-compose pull
# Start containers with new image
docker-compose up -d
'
"""
}
}
}
}
}
post {
always {
sh 'docker logout'
// Clean up old images on worker node
sh 'docker image prune -f'
}
}
}