forked from samarpita-bhaumik/Moviemate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkinsfile
172 lines (158 loc) · 6.62 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
pipeline {
agent any
environment {
DOCKER_IMAGE_NAME_FRONTEND = 'spefrontend1'
DOCKER_IMAGE_NAME_BACKEND = 'spebackend3'
GITHUB_REPO_URL = 'https://github.com/samarpita-bhaumik/Moviemate.git'
GIT_CREDENTIALS = 'mmProj'
DOCKER_HUB_USERNAME = 'samsun0196'
DOCKER_HUB_PASSWORD = 'Sr@16031996'
DOCKER_HUB_CREDENTIALS = 'Dockerhub_creds_samsun'
LANG = 'en_US.UTF-8'
LC_ALL = 'en_US.UTF-8'
}
stages {
stage('Stage 1: Checkout') {
steps {
// Checkout the repository using GitSCM plugin with specified credentials
checkout([$class: 'GitSCM', branches: [[name: '*/main']],
userRemoteConfigs: [[url: env.GITHUB_REPO_URL, credentialsId: env.GIT_CREDENTIALS]]
])
}
}
stage('Stage 2: Prepare Environment') {
steps {
// sh 'sudo apt-get update'
sh 'chmod -R +r MoviemateProject Backend'
sh 'ls -l MoviemateProject'
sh 'ls -l Backend'
}
}
stage('Stage 3: Install Python and Flask') {
steps {
// Update package index and install Python 3 and pip
sh 'sudo apt update'
sh 'sudo apt install -y python3 python3-pip'
// Install Flask using pip
sh 'pip3 install Flask pandas flask_cors requests python-logstash-async'
}
}
stage('Stage 4: Start Flask Server') {
steps {
script {
// Start the Flask server in the background
dir('Backend') {
sh 'nohup python3 app.py > /dev/null 2>&1 &'
sleep 40 // Give Flask some time to start (adjust as needed)
}
}
}
}
stage('Stage 5: Backend Testing') {
steps {
script {
try {
// Change directory to Backend
dir('Backend') {
sh 'python3 test_backend.py'
}
} finally {
// Ensure the Flask server is stopped after tests
dir('Backend') {
sh 'pkill -f "python3 app.py"'
}
}
}
}
}
stage('Stage 6: Setup ngrok') {
steps {
script {
// Ensure the directory exists before writing the ngrok configuration file
sh 'mkdir -p /var/lib/jenkins/.ngrok2'
sh 'echo "authtoken: 2cGkRwBjSdrcSHpoKCNgqCqTvY4_2k3ore6rZvxLpw3SDZmwv" > /var/lib/jenkins/.ngrok2/ngrok.yml'
sh 'ngrok config upgrade' // Upgrade ngrok configuration file
}
}
}
stage('Stage 7: Start ngrok') {
steps {
script {
// Install screen if not already installed
sh 'sudo apt-get install -y screen'
// Start ngrok in a detached screen session
sh 'screen -dmS ngrok_session ngrok http 5000'
sleep 10 // Give ngrok some time to start and provide the URL
}
}
}
stage('Stage 8: Install jq') {
steps {
script {
// Install jq
sh 'sudo apt-get update && sudo apt-get install -y jq'
}
}
}
stage('Stage 9: Get ngrok URL') {
steps {
script {
// Get the ngrok URL
def ngrokUrl = sh(script: "curl -s http://127.0.0.1:4040/api/tunnels | jq -r .tunnels[0].public_url", returnStdout: true).trim()
echo "ngrok URL: ${ngrokUrl}"
// Replace the backendUrl in the frontend code
sh """
sed -i 's|http://localhost:5000|${ngrokUrl}|g' MoviemateProject/Moviematebody.js
"""
}
}
}
stage('Stage 10: Build Frontend Image') {
steps {
// Build frontend Docker image
sh "sudo docker build -t ${DOCKER_IMAGE_NAME_FRONTEND} MoviemateProject"
sh "sudo docker tag ${DOCKER_IMAGE_NAME_FRONTEND}:latest ${DOCKER_HUB_USERNAME}/${DOCKER_IMAGE_NAME_FRONTEND}:latest"
}
}
stage('Stage 11: Build Backend Image') {
steps {
// Build backend Docker image
sh "sudo docker build -t ${DOCKER_IMAGE_NAME_BACKEND} Backend"
sh "sudo docker tag ${DOCKER_IMAGE_NAME_BACKEND}:latest ${DOCKER_HUB_USERNAME}/${DOCKER_IMAGE_NAME_BACKEND}:latest"
}
}
stage('Stage 12: Push Image to DockerHub') {
steps {
script {
withCredentials([usernamePassword(credentialsId: env.DOCKER_HUB_CREDENTIALS, usernameVariable: 'DOCKER_HUB_USERNAME', passwordVariable: 'DOCKER_HUB_PASSWORD')]) {
// Log in to DockerHub
sh "echo ${DOCKER_HUB_PASSWORD} | docker login -u ${DOCKER_HUB_USERNAME} --password-stdin"
// Push frontend and backend images to DockerHub
sh "docker push ${DOCKER_HUB_USERNAME}/${DOCKER_IMAGE_NAME_FRONTEND}:latest"
sh "docker push ${DOCKER_HUB_USERNAME}/${DOCKER_IMAGE_NAME_BACKEND}:latest"
}
}
}
}
stage('Stage 13: Clean Docker Images') {
steps {
script {
// Prune unused Docker containers and images
sh 'docker container prune -f'
sh 'docker image prune -f'
}
}
}
stage('Stage 14: Ansible Deployment') {
steps {
script {
// Execute Ansible playbook for deployment
// def ansiblePlaybook = 'moviemateDeploy.yaml' // Name of your playbook file
// def playbookOutput = sh(script: "ansible-playbook -i inventory.yaml ${ansiblePlaybook}", returnStdout: true)
// echo playbookOutput
sh 'ansible-playbook -i inventory.yaml moviemateDeploy.yaml'
}
}
}
}
}