-
Notifications
You must be signed in to change notification settings - Fork 8
/
Jenkinsfile
166 lines (143 loc) · 5.77 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
pipeline {
agent any
parameters {
string(name: 'namespace', defaultValue: "mom-server", description: 'namespace to deploy')
}
environment {
// Access environment variables using Jenkins credentials
DOCKER_REGISTRY = credentials('momentum-server-docker-registry')
GKE_CLUSTER = credentials('mom-core-gke-cluster')
GKE_ZONE = credentials('gke-zone')
GCP_PROJECT = credentials('gcp-project')
GOOGLE_APPLICATION_CREDENTIALS = credentials('google-application-credentials')
}
stages {
stage('Checkout') {
steps {
script {
// Determine environment based on branch
def branch = env.GIT_BRANCH
if (branch == "origin/temp") {
env.ENVIRONMENT = 'temp'
} else if (branch == "origin/main") {
env.ENVIRONMENT = 'main'
} else if (branch == "origin/devops"){
env.ENVIORNMENT = 'devops'
} else {
error("Unknown branch: ${branch}. This pipeline only supports main and staging branches.")
}
checkout scm
// Capture the short Git commit hash to use as the image tag
env.GIT_COMMIT_HASH = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
}
}
}
stage('Configure Docker Authentication') {
steps {
script {
// Extract the registry's hostname for authentication
def registryHost = env.DOCKER_REGISTRY.tokenize('/')[0]
sh """
sudo gcloud auth configure-docker ${registryHost}
"""
}
}
}
stage('Build Docker Image') {
steps {
script {
// Use the Git commit hash as the image tag
def imageTag = env.GIT_COMMIT_HASH
def dockerRegistry = env.DOCKER_REGISTRY
echo "Printing the saved docker registry from env:"
echo "${dockerRegistry}"
sh "sudo docker build -t ${DOCKER_REGISTRY}/momentum-server:${imageTag} ."
}
}
}
stage('Push Docker Image') {
steps {
script {
// Use the Git commit hash as the image tag
def imageTag = env.GIT_COMMIT_HASH
echo "printing the user here"
sh "whoami && pwd"
sh "sudo docker push ${DOCKER_REGISTRY}/momentum-server:${imageTag}"
}
}
}
stage('Configure GKE Authentication') {
steps {
script {
// Use the service account path from credentials
sh """
sudo gcloud auth activate-service-account --key-file=${GOOGLE_APPLICATION_CREDENTIALS}
sudo gcloud container clusters get-credentials ${GKE_CLUSTER} --zone ${GKE_ZONE} --project ${GCP_PROJECT}
"""
}
}
}
stage('Ask User for Deployment Confirmation') {
steps {
script {
def deployConfirmation = input(
id: 'userInput',
message: 'Do you want to deploy the new Docker image?',
parameters: [
choice(name: 'Deploy', choices: ['Yes', 'No'], description: 'Select Yes to deploy the image or No to abort.')
]
)
if (deployConfirmation == 'No') {
error('User chose not to deploy the images, stopping the pipeline.')
}
}
}
}
stage('Deploy Image') {
steps {
script {
def imageDeploySucceeded = false
def imageTag = env.GIT_COMMIT_HASH
echo "this is the fetched docker image tag: ${imageTag}"
try {
sh """
kubectl set image deployment/momentum-server-deployment momentum-server=${DOCKER_REGISTRY}/momentum-server:${imageTag} -n ${params.namespace}
kubectl rollout status deployment/momentum-server-deployment -n ${params.namespace}
"""
imageDeploySucceeded = true
} catch (Exception e) {
echo "Deployment failed: ${e}"
}
if (!imageDeploySucceeded) {
echo 'Rolling back to previous revision...'
sh 'kubectl rollout undo deployment/momentum-server-deployment -n ${params.namespace}'
}
}
}
}
stage('Pipeline finished') {
steps {
script {
echo "Pipeline finished"
// Check the deployment status
sh """
echo "checking the deployment status" && kubectl get pods -n ${params.namespace}
"""
}
}
}
}
post {
always {
echo "Pipeline finished"
// Optional cleanup action
script {
// Clean up local Docker images
def imageTag = env.GIT_COMMIT_HASH
sh """
docker rmi ${DOCKER_REGISTRY}/momentum-server:${imageTag} || true
"""
}
}
}
}