-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
176 lines (165 loc) · 6.26 KB
/
Jenkinsfile
File metadata and controls
176 lines (165 loc) · 6.26 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
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
173
174
175
176
pipeline {
agent {
kubernetes {
yaml """
apiVersion: v1
kind: Pod
spec:
containers:
- name: gradle
image: gradle:8.14.2-jdk21-corretto
command:
- cat
tty: true
resources:
requests:
memory: "2Gi"
cpu: "1"
limits:
memory: "4Gi"
cpu: "2"
"""
}
}
parameters {
string(name: 'APP_PROFILE', defaultValue: 'prod', description: 'Application profile to use (e.g., dev, prod)')
booleanParam(name: 'PERFORM_DEPLOYMENT', defaultValue: false, description: 'Check to perform deployment')
}
environment {
VERSION = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
DOCKER_HUB_IMAGE = "webdev0594/moco-backend"
PORT = "8080"
}
options {
timeout(time: 1, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: '10'))
disableConcurrentBuilds()
}
stages {
stage('Initialize') {
steps {
script {
echo "Running with profile: ${params.APP_PROFILE}"
echo "Deployment will be performed: ${params.PERFORM_DEPLOYMENT}"
echo "Current branch: ${env.BRANCH_NAME}"
echo "PERFORM_DEPLOYMENT type: ${params.PERFORM_DEPLOYMENT.getClass()}"
echo "Branch condition check: ${env.BRANCH_NAME == 'main' || env.BRANCH_NAME == 'develop'}"
}
}
}
stage('Checkout') {
steps {
checkout([
$class: 'GitSCM',
branches: scm.branches,
extensions: [
[$class: 'SubmoduleOption',
disableSubmodules: false,
parentCredentials: true,
recursiveSubmodules: true,
reference: '',
trackingSubmodules: false
]
],
submoduleCfg: [],
userRemoteConfigs: scm.userRemoteConfigs
])
}
}
stage('Test') {
options {
timeout(time: 15, unit: 'MINUTES')
}
steps {
container('gradle') {
sh "./gradlew :application:test --parallel"
}
}
}
stage('Build & Push Docker Image') {
options {
timeout(time: 20, unit: 'MINUTES')
}
steps {
container('gradle') {
script {
withCredentials([usernamePassword(
credentialsId: 'registry-credential',
usernameVariable: 'DOCKER_HUB_USERNAME',
passwordVariable: 'DOCKER_HUB_PASSWORD'
)]) {
sh """
./gradlew clean :application:jib \
-Djib.to.image=${DOCKER_HUB_IMAGE} \
-Djib.to.tags=latest,${VERSION} \
-Djib.to.auth.username=${DOCKER_HUB_USERNAME} \
-Djib.to.auth.password=${DOCKER_HUB_PASSWORD} \
-Djib.container.environment=SPRING_PROFILES_ACTIVE=${params.APP_PROFILE}
"""
}
}
}
}
}
stage('Check Deploy Conditions') {
steps {
script {
echo "=== Deploy Condition Check ==="
echo "PERFORM_DEPLOYMENT: ${params.PERFORM_DEPLOYMENT}"
echo "Current Branch: ${env.BRANCH_NAME}"
echo "Branch matches main: ${env.BRANCH_NAME == 'main'}"
echo "Branch matches develop: ${env.BRANCH_NAME == 'develop'}"
def shouldDeploy = params.PERFORM_DEPLOYMENT == true &&
(env.BRANCH_NAME == 'main' || env.BRANCH_NAME == 'develop')
echo "Should deploy: ${shouldDeploy}"
}
}
}
stage('Deploy to Kubernetes') {
when {
expression { params.PERFORM_DEPLOYMENT == true }
}
stages {
stage('Approve Production Deploy') {
when {
expression { params.APP_PROFILE == 'prod' }
}
steps {
input message: "Deploy to Production environment?", ok: "Deploy"
}
}
stage('Update Helm Values') {
steps {
container('gradle') {
withCredentials([usernamePassword(credentialsId: 'git-credential', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
sh """
git clone https://\${GIT_USERNAME}:\${GIT_PASSWORD}@github.com/moco-labs/moco-chart.git deploy-repo
cd deploy-repo
sed -i 's/tag: ".*"/tag: "${VERSION}"/g' infrastructure/helm-charts/applications/backend/values-prod.yaml
git config --global user.email "me@mooowu.xyz"
git config --global user.name "moco-ci"
git add infrastructure/helm-charts/applications/backend/values-prod.yaml
git commit -m "update moco-backend version to ${VERSION}"
git push origin main
"""
}
}
}
}
}
}
}
post {
always {
echo 'Pipeline finished.'
}
success {
echo 'Pipeline succeeded!'
// Slack/Email notification can be added here
}
failure {
echo 'Pipeline failed!'
// Slack/Email notification for failure can be added here
}
}
}