-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjenkinsfile
More file actions
146 lines (132 loc) · 4.63 KB
/
jenkinsfile
File metadata and controls
146 lines (132 loc) · 4.63 KB
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
pipeline {
agent any
stages {
stage('Checkout Code') {
steps {
checkout scm
}
}
stage('Set Environment Variables') {
steps {
script {
echo "Debug: Current BRANCH_NAME = '${env.BRANCH_NAME}'"
sh '''
if [ -d .env ]; then
echo ".env is a directory. Removing it..."
rm -rf .env
fi
if [ -f .env ]; then
echo ".env is a file. Removing it..."
rm -f .env
fi
'''
if (env.BRANCH_NAME == 'main') {
echo "Setting environment for main (production) branch..."
withCredentials([file(credentialsId: 'backend_env_file_prod', variable: 'ENV_FILE_PATH')]) {
sh 'cp $ENV_FILE_PATH .env'
}
} else {
echo "Setting environment for non-main branch (dev/test)..."
writeFile file: '.env', text: """\
MYSQL_DATABASE=test_db
MYSQL_USER=test_user
MYSQL_PASSWORD=test_pw
MYSQL_ROOT_PASSWORD=test_root_pw
MYSQL_HOST=localhost
MYSQL_IN_PORT=3306
MYSQL_EX_PORT=3307
REDIS_HOST=localhost
REDIS_PORT=6379
SPRING_PROFILE_ACTIVE=dev
SPRING_APP_PORT=4001
FILE_STORAGE_PATH=/tmp/test-storage
JWT_SECRET_KEY=dummysecret
EMAIL_ADDRESS=test@example.com
EMAIL_PASSWORD=1234
BASE_PROFILE_IMAGE_NAME=default.png
ADMIN_STUDENT_NUMBER=12345678
ADMIN_PASSWORD=1234
"""
}
}
}
}
stage('Grant Permissions') {
steps {
sh 'chmod +x ./gradlew'
}
}
// stage('Run Tests') {
// steps {
// echo "Running tests on all branches"
// sh "./gradlew test"
// }
// }
stage('Build JAR') {
steps {
sh "./gradlew build -x test --no-daemon"
}
}
stage('Prepare Docker Context') {
when {
branch 'main'
}
steps {
sh 'cp .env build/libs/.env'
}
}
stage('Build Docker Image') {
when {
branch 'main'
}
steps {
script {
def imageTag = "latest"
docker.build("myapp:${imageTag}")
}
}
}
stage('Deploy to Production (Only for main branch)') {
when {
branch 'main'
}
steps {
script {
echo "Stopping old container if running..."
sh '''
docker ps -q --filter "name=backend-spring-app-1" | grep -q . && docker stop backend-spring-app-1 && docker rm backend-spring-app-1 || true
'''
echo "Running new container on correct network with consistent project name..."
sh '''
docker compose -p backend --env-file .env build spring-app
docker compose -p backend --env-file .env up -d --no-deps spring-app
'''
}
}
}
stage('Cleanup') {
steps {
script {
def isPruneRunning = sh(script: "ps aux | grep 'docker image prune' | grep -v grep || true", returnStdout: true).trim()
if (isPruneRunning) {
echo "Docker prune is already running. Skipping this step."
} else {
sh "docker image prune -f"
}
}
}
}
}
post {
always {
echo "Cleaning up resources"
}
success {
echo "Pipeline completed successfully."
}
failure {
echo "Pipeline failed. Check the logs."
error("Pipeline failed! Fix the issue before merging to main.")
}
}
}