-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJenkinsfile
157 lines (141 loc) · 5.57 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
pipeline {
agent any
environment {
// Node version and paths
NODE_VERSION = '20'
WORKSPACE = "${WORKSPACE}"
// Docker configuration
DOCKER_IMAGE = "smsraj2001/shoe-shop"
DOCKER_CREDENTIALS = credentials('docker-creds')
// Application secrets
MONGODB_URI = credentials('mongodb-uri')
JWT_SECRET = credentials('jwt-secret')
BRAINTREE_MERCHANT_ID = credentials('braintree-merchant')
BRAINTREE_PUBLIC_KEY = credentials('braintree-public')
BRAINTREE_PRIVATE_KEY = credentials('braintree-private')
}
stages {
stage('Clean Workspace') {
steps {
// Clean workspace and node_modules
bat 'if exist "node_modules" rmdir /s /q node_modules'
bat 'if exist "client\\node_modules" rmdir /s /q client\\node_modules'
bat 'if exist "client\\build" rmdir /s /q client\\build'
}
}
stage('Checkout') {
steps {
checkout scm
}
}
stage('Setup Environment') {
steps {
script {
// Write server .env file
writeFile file: '.env', text: """
MONGODB_URI=${MONGODB_URI}
JWT_SECRET=${JWT_SECRET}
BRAINTREE_MERCHANT_ID=${BRAINTREE_MERCHANT_ID}
BRAINTREE_PUBLIC_KEY=${BRAINTREE_PUBLIC_KEY}
BRAINTREE_PRIVATE_KEY=${BRAINTREE_PRIVATE_KEY}
NODE_ENV=production
PORT=5000
"""
// Write client .env file with CI=false to prevent treating warnings as errors
writeFile file: 'client/.env', text: """
REACT_APP_API_URL=https://shoe-shop-ecommerce-web-app.onrender.com/api
DISABLE_ESLINT_PLUGIN=true
CI=false
"""
}
}
}
stage('Install Dependencies') {
steps {
// Install server dependencies
bat 'npm install'
// Install client dependencies and required ESLint plugins
dir('client') {
bat 'npm install'
bat 'npm install --save-dev @babel/plugin-proposal-private-property-in-object'
}
}
}
stage('Lint Check') {
steps {
dir('client') {
// Run ESLint with --max-warnings flag to allow warnings but catch errors
bat 'npm run lint --if-present || exit 0'
}
}
}
stage('Build Client') {
steps {
dir('client') {
bat 'set "CI=false" && set "GENERATE_SOURCEMAP=false" && npm run build'
}
}
}
stage('Docker Build & Push') {
steps {
script {
withCredentials([usernamePassword(credentialsId: 'docker-creds', passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USERNAME')]) {
// Login to Docker Hub
bat "docker login -u %DOCKER_USERNAME% -p %DOCKER_PASSWORD%"
// Build and push server image
bat """
docker build -f Dockerfile.server ^
--build-arg MONGODB_URI=%MONGODB_URI% ^
--build-arg JWT_SECRET=%JWT_SECRET% ^
--build-arg BRAINTREE_MERCHANT_ID=%BRAINTREE_MERCHANT_ID% ^
--build-arg BRAINTREE_PUBLIC_KEY=%BRAINTREE_PUBLIC_KEY% ^
--build-arg BRAINTREE_PRIVATE_KEY=%BRAINTREE_PRIVATE_KEY% ^
-t %DOCKER_IMAGE%:server-%BUILD_NUMBER% ^
-t %DOCKER_IMAGE%:server-latest .
"""
// Build and push client image
bat """
docker build -f client/Dockerfile.client ^
-t %DOCKER_IMAGE%:client-%BUILD_NUMBER% ^
-t %DOCKER_IMAGE%:client-latest .
"""
// Push images
bat "docker push %DOCKER_IMAGE%:server-%BUILD_NUMBER%"
bat "docker push %DOCKER_IMAGE%:server-latest"
bat "docker push %DOCKER_IMAGE%:client-%BUILD_NUMBER%"
bat "docker push %DOCKER_IMAGE%:client-latest"
}
}
}
}
stage('Deploy') {
steps {
script {
try {
bat 'docker-compose down'
} catch (Exception e) {
echo 'No existing containers to stop'
}
bat 'docker-compose up -d'
}
}
}
}
post {
always {
bat 'docker system prune -f'
cleanWs(
cleanWhenNotBuilt: false,
deleteDirs: true,
disableDeferredWipeout: true,
notFailBuild: true
)
}
success {
echo 'Pipeline succeeded! Application deployed successfully...'
}
failure {
echo 'Pipeline failed! Check the logs for details...'
}
}
}