-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile.groovy
146 lines (142 loc) · 6.51 KB
/
Jenkinsfile.groovy
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
@Library('shared-lib') _
pipeline {
agent any
environment {
CHAT_ID = credentials('whatsapp-group')
// CHAT_ID = "[email protected]"
// Telegram Message Pre Build
CURRENT_BUILD_NUMBER = "${currentBuild.number}"
GIT_MESSAGE = sh(returnStdout: true, script: "git log -n 1 --format=%s ${GIT_COMMIT}").trim()
GIT_AUTHOR = sh(returnStdout: true, script: "git log -n 1 --format=%ae ${GIT_COMMIT}").trim()
GIT_COMMIT_SHORT = sh(returnStdout: true, script: "git rev-parse --short ${GIT_COMMIT}").trim()
GIT_INFO = "Branch(Version): ${GIT_BRANCH}\nLast Message: ${GIT_MESSAGE}\nAuthor: ${GIT_AUTHOR}\nCommit: ${GIT_COMMIT_SHORT}"
TEXT_BREAK = "--------------------------------------------------------------"
TEXT_PRE_BUILD = "${TEXT_BREAK}\n${GIT_INFO}\n${JOB_NAME} -- 🤝 ${BUILD_URL}console"
// TEXT_PRE_BUILD = "${TEXT_BREAK}\n${GIT_INFO}\n${JOB_NAME} -- Please Approved Building @b_indrawan 🤝 ${BUILD_URL}console"
// Telegram Message Success and Failure
TEXT_SUCCESS_BUILD = "${JOB_NAME} Build SUCCESS ✅"
TEXT_FAILURE_BUILD = "${JOB_NAME} Build FAILURE ❌"
TEXT_ABORTED_BUILD = "${JOB_NAME} Build ABORTED ⛔️"
FILE_ENVIRONTMENT = '.env.example'
IMAGE_REGISTRY_PATH = 'reg.icc.private/icc/whatsapp-api-adnan'
MANIFEST_PATH = "whatsapp-api-adnan/statefulset.yml"
GIT_BRANCH_TO_SWITCH = "master"
}
parameters {
string(defaultValue: "[email protected]", description: 'git config user.email ', name: 'GIT_CONFIG_EMAIL')
string(defaultValue: "Adnan Khafabi", description: 'git config user.name', name: 'GIT_CONFIG_USERNAME')
// parameter of application
string(defaultValue: "main", description: "Branch of Application", name: 'Application_Branch')
string(defaultValue: "git.incenter.id/adnan/whatsapp-api.git", description: "url git", name: 'Url_Git')
string(defaultValue: "main", description: "default branch", name: 'BRANCH_NAME')
}
options { disableConcurrentBuilds(abortPrevious: true) }
stages {
stage('Send Notification'){
steps {
sendToWhatsappGroup("${env.TEXT_PRE_BUILD}", CHAT_ID)
}
}
stage('Change remote git') {
steps {
script {
withCredentials([usernamePassword(credentialsId: 'adnan-auth', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
commandSsh("git remote set-url origin https://${USERNAME}:${PASSWORD}@${Url_Git};")
}
}
}
}
stage('Pull Repository') {
steps {
// sh 'ssh [email protected] "cd whatsapp-api; git pull origin ${Application_Branch};"'
commandSsh("git pull origin ${Application_Branch};")
}
}
// stage('Copy-Config') {
// steps {
// // commandSsh("cp ${FILE_ENVIRONTMENT} .env && sed -i 's/X_API_KEY[^ ]*${'$'}/X_API_KEY=HELLOWORLD/g' .env")
// sh "cp ${FILE_ENVIRONTMENT} .env"
// sh "echo injection X_API_KEY"
// sh "sed -i 's/X_API_KEY[^ ]*${'$'}/X_API_KEY=HELLOWORLD/g' .env"
// }
// }
stage('Deploy to server') {
steps {
// sh 'ssh [email protected] "cd whatsapp-api; docker compose up -d --build;"'
commandSsh('docker compose up -d --build;')
// commandSsh("docker compose exec app npm run prisma:migrate")
// commandSsh("docker compose exec app npm run prisma:generate")
}
}
stage('Change to default origin') {
steps {
commandSsh("git remote set-url origin https://${Url_Git};")
}
}
// stage('Build Docker image') {
// steps {
// sh "docker build --no-cache -t ${IMAGE_REGISTRY_PATH}:${BUILD_NUMBER} -f Dockerfile ."
// }
// }
// stage('Docker push to Container Registry') {
// steps {
// sh "docker push ${IMAGE_REGISTRY_PATH}:${BUILD_NUMBER}"
// }
// }
// stage('Delete Docker image') {
// steps {
// sh "docker rmi -f ${IMAGE_REGISTRY_PATH}:${BUILD_NUMBER}"
// }
// }
// stage('Git Clone for the kubeconfig code') {
// steps {
// git branch: "${GIT_BRANCH_TO_SWITCH}", credentialsId: 'github', url: "https://git.incenter.id/infrastucture/kube-cat.git"
// sh 'ls -a'
// sh '[ -f ".env" ] && rm ".env"'
// }
// }
// stage('Update GIT') {
// steps {
// script {
// catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
// withCredentials([usernamePassword(credentialsId: 'adnan-auth', passwordVariable: 'git_password', usernameVariable: 'git_username')]) {
// sh "git config user.email '${params.GIT_CONFIG_EMAIL}' "
// sh "git config user.name '${params.GIT_CONFIG_USERNAME}' "
// sh "cat '${MANIFEST_PATH}' "
// sh "sed -i 's+${IMAGE_REGISTRY_PATH}:[^ ]*+${IMAGE_REGISTRY_PATH}:${env.BUILD_NUMBER}+g' ${MANIFEST_PATH}"
// sh "cat '${MANIFEST_PATH}' "
// sh "git add '${MANIFEST_PATH}' "
// sh "git commit -m 'Done by Jenkins Job changemanifest ${env.BUILD_NUMBER} WhatsApp-API' "
// sh "git push https://$git_username:'$git_password'@git.incenter.id/infrastucture/kube-cat.git HEAD:'${GIT_BRANCH_TO_SWITCH}'"
// }
// }
// }
// }
// }
}
post {
success {
script{
// sh "curl --location --request POST 'https://api.telegram.org/bot${TOKEN}/sendMessage' --form text='${TEXT_SUCCESS_BUILD}' --form chat_id='${CHAT_ID}'"
sendToWhatsappGroup("${TEXT_SUCCESS_BUILD}", CHAT_ID)
}
}
failure {
script{
// sh "curl --location --request POST 'https://api.telegram.org/bot${TOKEN}/sendMessage' --form text='${TEXT_FAILURE_BUILD}' --form chat_id='${CHAT_ID}'"
sendToWhatsappGroup("${TEXT_FAILURE_BUILD}", CHAT_ID)
}
}
aborted {
script{
// sh "curl --location --request POST 'https://api.telegram.org/bot${TOKEN}/sendMessage' --form text='${TEXT_ABORTED_BUILD}' --form chat_id='${CHAT_ID}'"
sendToWhatsappGroup("${TEXT_ABORTED_BUILD}", CHAT_ID)
}
}
}
}
def commandSsh(String command) {
sshagent (credentials: ['SSH_JENKINS_FRAPPE']) {
sh "ssh -o StrictHostKeyChecking=no [email protected] \"cd whatsapp-api; ${command}\""
}
}