-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
162 lines (154 loc) Β· 7.26 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
pipeline {
agent {
label 'jk-worker1'
}
// agent any
tools {
nodejs 'nodejs'
}
environment {
DOCKER_REGISTRY = 'kimheang68'
IMAGE_NAME = 'react-jenkin'
CONTAINER_NAME = 'my-container'
TELEGRAM_BOT_TOKEN = credentials('telegram-token')
TELEGRAM_CHAT_ID = credentials('chat-id')
BUILD_INFO = "${currentBuild.number}"
COMMITTER = sh(script: 'git log -1 --pretty=format:%an', returnStdout: true).trim()
BRANCH = sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim()
SONARQUBE_TOKEN = credentials('sonarqube-token')
}
stages {
stage('Notify Start') {
steps {
script {
sendTelegramMessage("π Pipeline Started:\nJob Name: ${env.JOB_NAME}\nJob Description: ${env.JOB_DESCRIPTION}\nVersion: ${BUILD_INFO}\nCommitter: ${COMMITTER}\nBranch: ${BRANCH}")
}
}
}
stage('Code Quality Check via SonarQube') {
steps {
script {
def scannerHome = tool 'sonarqube-scanner'
withSonarQubeEnv("sonarqube-server") {
// Define environment variables securely
def scannerCommand = """
${scannerHome}/bin/sonar-scanner \
-Dsonar.projectKey=test-node-js \
-Dsonar.sources=. \
-Dsonar.css.node=. \
-Dsonar.host.url=http://34.143.129.86:9000 \
-Dsonar.login=${env.SONARQUBE_TOKEN}
"""
def codeQualityLogs = sh script: scannerCommand, returnStatus: true
if (codeQualityLogs != 0) {
sendTelegramMessage("β Code Quality Check via SonarQube failed")
currentBuild.result = 'FAILURE'
error("Code Quality Check via SonarQube failed")
} else {
echo "β
Code Quality Check via SonarQube succeeded"
}
}
}
}
}
stage('Build') {
steps {
script {
try {
sh 'npm install'
sh 'npm run build'
} catch (Exception e) {
currentBuild.result = 'FAILURE'
def errorMessage = "β Build stage <b> failed </b>:\n${e.getMessage()}\nVersion: ${BUILD_INFO}\nCommitter: ${COMMITTER}\nBranch: ${BRANCH}\nConsole Output: ${env.BUILD_URL}console"
sendTelegramMessage(errorMessage)
error(errorMessage)
}
}
}
}
stage('Test') {
steps {
script {
try {
// sh 'npm run test'
echo "Test"
sh "echo IMAGE_NAME is ${env.IMAGE_NAME}"
// sendTelegramMessage("β
Test stage succeeded\nVersion: ${BUILD_INFO}\nCommitter: ${COMMITTER}\nBranch: ${BRANCH}")
} catch (Exception e) {
currentBuild.result = 'FAILURE'
sendTelegramMessage("β Test stage <b> failed </b>: ${e.message}\nVersion: ${BUILD_INFO}\nCommitter: ${COMMITTER}\nBranch: ${BRANCH}")
error("Test stage failed: ${e.message}")
}
}
}
}
stage('Check for Existing Container') {
steps {
script {
try {
def containerId = sh(script: "docker ps -a --filter name=${env.CONTAINER_NAME} -q", returnStdout: true).trim()
sh "echo containerId is ${containerId}"
if (containerId) {
sh "docker stop ${containerId}"
sh "docker rm ${containerId}"
// sendTelegramMessage("β
Container cleanup succeeded\nVersion: ${BUILD_INFO}\nCommitter: ${COMMITTER}\nBranch: ${BRANCH}")
} else {
// sendTelegramMessage("β
No existing container to remove\nVersion: ${BUILD_INFO}\nCommitter: ${COMMITTER}\nBranch: ${BRANCH}")
}
} catch (Exception e) {
currentBuild.result = 'FAILURE'
sendTelegramMessage("β Check for Existing Container stage failed: ${e.message}\nVersion: ${BUILD_INFO}\nCommitter: ${COMMITTER}\nBranch: ${BRANCH}")
error("Check for Existing Container stage failed: ${e.message}")
}
}
}
}
stage('Build Image') {
steps {
script {
try {
def buildNumber = currentBuild.number
def imageTag = "${IMAGE_NAME}:${buildNumber}"
sh "docker build -t ${DOCKER_REGISTRY}/${imageTag} ."
withCredentials([usernamePassword(credentialsId: 'docker-hub-cred',
passwordVariable: 'PASS', usernameVariable: 'USER')]) {
sh "echo \$PASS | docker login -u \$USER --password-stdin"
sh "docker push ${DOCKER_REGISTRY}/${imageTag}"
// sendTelegramMessage("β
Build Image stage succeeded\nVersion: ${BUILD_INFO}\nCommitter: ${COMMITTER}\nBranch: ${BRANCH}")
}
} catch (Exception e) {
currentBuild.result = 'FAILURE'
sendTelegramMessage("β Build Image stage failed: ${e.message}\nVersion: ${BUILD_INFO}\nCommitter: ${COMMITTER}\nBranch: ${BRANCH}")
error("Build Image stage failed: ${e.message}")
}
}
}
}
stage('Trigger ManifestUpdate') {
steps {
script {
try {
build job: 'test2', parameters: [string(name: 'DOCKERTAG', value: env.BUILD_NUMBER)]
// sendTelegramMessage("β
Trigger ManifestUpdate stage succeeded\nVersion: ${BUILD_INFO}\nCommitter: ${COMMITTER}\nBranch: ${BRANCH}")
} catch (Exception e) {
currentBuild.result = 'FAILURE'
sendTelegramMessage("β Trigger ManifestUpdate stage failed: ${e.message}\nVersion: ${BUILD_INFO}\nCommitter: ${COMMITTER}\nBranch: ${BRANCH}")
error("Trigger ManifestUpdate stage failed: ${e.message}")
}
}
}
}
}
post {
success {
sendTelegramMessage("β
All stages succeeded\nVersion: ${BUILD_INFO}\nCommitter: ${COMMITTER}\nBranch: ${BRANCH}")
}
}
}
def sendTelegramMessage(message) {
script {
sh """
curl -s -X POST https://api.telegram.org/bot\${TELEGRAM_BOT_TOKEN}/sendMessage -d chat_id=\${TELEGRAM_CHAT_ID} -d parse_mode="HTML" -d text="${message}"
"""
}
}