-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
180 lines (165 loc) · 7.76 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
173
174
175
176
177
178
179
180
def sendTelegramMessage(token, chatId, message) {
sh """
curl -s -X POST https://api.telegram.org/bot${token}/sendMessage -d chat_id=${chatId} -d text="${message}"
"""
}
pipeline{
agent any
environment{
PATH_PROJECT = '/home/projects/VDT-midterm-api'
PATH_CONFIG_PROJECT = '/home/projects/VDT-config/VDT-config-helm-api'
SONAR_PROJECT_KEY = credentials('vdt-midterm-api-sonar-project-key')
SONAR_TOKEN = credentials('sonarqube-token')
SONAR_HOST_URL= credentials('sonar-host-url')
IP_GITLAB_SERVER = credentials('ip-gitlab-server')
NAME_REPO_CONFIG = 'vdt-config-helm-api'
DOCKER_HUB ='tuanquang1811'
DOCKER_HUB_CREDENTIALS = credentials('dockerhub-credentials')
NAME_API = 'vdt-midterm-api'
DOCKER_TAG = "${GIT_BRANCH.tokenize('/').pop()}-${GIT_COMMIT.substring(0,7)}"
ENV_FILE_VDT_MIDTERM_API = ''
TELEGRAM_TOKEN = credentials('telegram-token')
TELEGRAM_CHAT_ID = credentials('telegram-chatId')
TEXT_PRE_BUILD = "Jenkins is building ${JOB_NAME}"
}
stages{
stage("Prepare pipeline") {
steps {
script {
sendTelegramMessage(TELEGRAM_TOKEN, TELEGRAM_CHAT_ID, TEXT_PRE_BUILD)
}
}
}
stage('Checkout source' ){
steps{
sh "sudo cp -r . $PATH_PROJECT \
&& sudo chown -R jenkins:jenkins $PATH_PROJECT \
&& sudo chmod -R 755 $PATH_PROJECT \
"
}
}
stage('Test with sonarqube'){
steps{
withSonarQubeEnv('sonarqube connection') {
sh "cd $PATH_PROJECT && docker run --rm \
-e SONAR_HOST_URL=${SONAR_HOST_URL} \
-e SONAR_SCANNER_OPTS='-Dsonar.projectKey=${SONAR_PROJECT_KEY}' \
-e SONAR_TOKEN=${SONAR_TOKEN} \
-v '.:/usr/src' \
sonarsource/sonar-scanner-cli"
}
}
}
stage('Check lint and prettier'){
steps{
sh "cd $PATH_PROJECT && npm install && npm run lint && npm run prettier"
}
}
stage('Unit test with Jest'){
steps{
script{
withCredentials([file(credentialsId: 'vdt-midterm-api-env', variable: 'ENV_FILE_VDT_MIDTERM_API')]) {
ENV_FILE_VDT_MIDTERM_API = readFile(file:"$ENV_FILE_VDT_MIDTERM_API").trim()
}
sh "echo \"$ENV_FILE_VDT_MIDTERM_API\" > $PATH_PROJECT/.env.production"
sh "cd $PATH_PROJECT && npm run test:prod"
}
}
}
stage('Build and push image'){
when {
expression {
return sh(script: 'git describe --exact-match --tags HEAD', returnStatus: true) == 0
}
}
steps{
script {
try {
timeout(time: 3, unit: 'MINUTES') {
env.userChoice = input message: 'Do you want to build and push image to docker hub?',
parameters: [choice(name: 'Versioning service', choices: 'Yes\nNo', description: 'Choose "Yes" if you want to build and push image to docker hub')]
}
if(env.userChoice == 'Yes') {
env.IMAGE_TAG = DOCKER_TAG
sh " cd $PATH_PROJECT \
&& IMAGE_TAG=${IMAGE_TAG} \
&& NAME_API=${NAME_API} \
&& docker-compose build --parallel \
&& docker tag ${NAME_API}:$DOCKER_TAG ${DOCKER_HUB}/${NAME_API}:$DOCKER_TAG \
&& echo $DOCKER_HUB_CREDENTIALS_PSW | docker login -u $DOCKER_HUB_CREDENTIALS_USR --password-stdin \
&& docker push ${DOCKER_HUB}/${NAME_API}:$DOCKER_TAG \
&& docker rmi ${DOCKER_HUB}/${NAME_API}:$DOCKER_TAG "
} else {
echo "build and push image to docker hub cancelled"
}
} catch(Exception err) {
def user = err.getCauses()[0].getUser()
if('SYSTEM' == user.toString()) {
def didTimeout = true
echo "Timeout. Build and push image to docker hub cancelled"
} else {
echo "Build and push image to docker hub cancelled by: ${user}"
}
}
}
}
}
stage('Update version image in helm chart'){
when {
expression {
return sh(script: 'git describe --exact-match --tags HEAD', returnStatus: true) == 0
}
}
steps{
script{
try {
timeout(time: 3, unit: 'MINUTES') {
env.userChoice = input message: 'Do you want to update version image in helm chart?',
parameters: [choice(name: 'Versioning service', choices: 'Yes\nNo', description: 'Choose "Yes" if you want to update version image in helm chart')]
}
if(env.userChoice == 'Yes') {
withCredentials([usernamePassword(credentialsId: 'gitlab-credentials', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD')]) {
sh """
rm -rf ${PATH_CONFIG_PROJECT}
# Clone the repository
git clone http://${GIT_USERNAME}:${GIT_PASSWORD}@${IP_GITLAB_SERVER}/tuanmaintainer/${NAME_REPO_CONFIG}.git ${PATH_CONFIG_PROJECT}
cd ${PATH_CONFIG_PROJECT}
# Update the version in the values file
sed -i 's/tag:.*/tag: ${DOCKER_TAG}/' values.yaml
# Commit and push the changes
git config user.name "${GIT_USERNAME}"
git config user.email "${GIT_USERNAME}@git-server.com"
git add .
git commit -m "Update version to ${DOCKER_TAG}"
git push origin main
"""
}
} else {
echo "deploy cancelled"
}
}
catch(Exception err) {
def user = err.getCauses()[0].getUser()
if('SYSTEM' == user.toString()) {
def didTimeout = true
echo "Timeout. update version image in helm chart cancelled"
} else {
echo "Update version image in helm chart cancelled by: ${user}"
}
}
}
}
}
}
post{
success {
sendTelegramMessage(TELEGRAM_TOKEN, TELEGRAM_CHAT_ID, "JOB ${JOB_NAME} is Success")
}
failure {
sendTelegramMessage(TELEGRAM_TOKEN, TELEGRAM_CHAT_ID, "JOB ${JOB_NAME} is Failure")
}
aborted {
sendTelegramMessage(TELEGRAM_TOKEN, TELEGRAM_CHAT_ID, "JOB ${JOB_NAME} is Aborted")
}
}
}