-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
202 lines (169 loc) · 11.7 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
node {
load "$JENKINS_HOME/.envvars"
def exists=fileExists "src/server/package-lock.json"
def exists2=fileExists "src/client/package-lock.json"
def application_name= "app_base"
stage('Checkout') {
git branch: 'develop',
url: 'https://github.com/lapig-ufg/plataform-base.git'
}
stage('Validate') {
sh 'git pull origin develop'
}
stage('SonarQube analysis') {
def scannerHome = tool 'sonarqube-scanner';
withSonarQubeEnv("sonarqube") {
sh "${tool("sonarqube-scanner")}/bin/sonar-scanner \
-Dsonar.projectKey=plataforma-base \
-Dsonar.sources=. \
-Dsonar.css.node=. \
-Dsonar.host.url=$SonarUrl \
-Dsonar.login=$SonarKeyProject"
}
}
stage('Build') {
//INSTALL NVM BINARY AND INSTALL NODE VERSION AND USE NODE VERSION
nvm(nvmInstallURL: 'https://raw.githubusercontent.com/creationix/nvm/master/install.sh',
nvmIoJsOrgMirror: 'https://iojs.org/dist',
nvmNodeJsOrgMirror: 'https://nodejs.org/dist',
version: NODE_VERSION) {
//BUILD APPLICATION
echo "Build main site distribution"
sh "npm set progress=false"
if (exists) {
echo 'Yes'
sh "cd src/server && npm ci"
} else {
echo 'No'
sh "cd src/server && npm install"
}
if (exists2) {
echo 'Yes'
sh "cd src/client && npm ci"
} else {
echo 'No'
sh "cd src/client && npm install"
}
//VERIFY IF BUILD IS COMPLETE AND NOTIFY IN DISCORD ABOUT OF THE RESULT
sh "export NODE_OPTIONS=--max-old-space-size=8096"
def status = sh(returnStatus: true, script: "cd src/client && ng build --stats-json --source-map=false --no-progress")
if (status != 0) {
echo "FAILED BUILD!"
currentBuild.result = 'FAILED'
def discordImageSuccess = 'https://www.jenkins.io/images/logos/formal/256.png'
def discordImageError = 'https://www.jenkins.io/images/logos/fire/256.png'
def Author_Name=sh(script: "git show -s --pretty=%an", returnStdout: true).trim()
def Author_Email=sh(script: "git show -s --pretty=%ae", returnStdout: true).trim()
def Author_Data=sh(script: "git log -1 --format=%cd --date=local",returnStdout: true).trim()
def Project_Name=sh(script: "git config --local remote.origin.url",returnStdout: true).trim()
def Last_Commit=sh(script: "git show --summary | grep 'commit' | awk '{print \$2}'",returnStdout: true).trim()
def Comment_Commit=sh(script: "git log -1 --pretty=%B",returnStdout: true).trim()
def Date_Commit=sh(script: "git show -s --format=%ci",returnStdout: true).trim()
def discordDesc =
"Result: ${currentBuild.currentResult}\n" +
"Project: $Project_Name\n" +
"Commit: $Last_Commit\n" +
"Author: $Author_Name\n" +
"Author_Email: $Author_Email\n" +
"Message: $Comment_Commit\n" +
"Date: $Date_Commit\n" +
"Duration: ${currentBuild.durationString}"
//Variaveis de ambiente do Jenkins - NOME DO JOB E NÚMERO DO JOB
def discordFooter = "${env.JOB_BASE_NAME} (#${BUILD_NUMBER})"
def discordTitle = "${env.JOB_BASE_NAME} (build #${BUILD_NUMBER})"
def urlWebhook = "https://discord.com/api/webhooks/$DiscordKey"
discordSend description: discordDesc,
footer: discordFooter,
link: env.JOB_URL,
result: currentBuild.currentResult,
title: discordTitle,
webhookURL: urlWebhook,
successful: currentBuild.resultIsBetterOrEqualTo('SUCCESS'),
thumbnail: 'SUCCESS'.equals(currentBuild.currentResult) ? discordImageSuccess : discordImageError
autoCancelled = true
error('Aborting the build.')
}
}
}
stage('Building Image') {
dockerImage = docker.build registryhomol + "/$application_name:$BUILD_NUMBER", "--build-arg --no-cache -f Dockerfile ."
}
stage('Push Image to Registry') {
docker.withRegistry( "$Url_Private_Registry", "$registryCredential" ) {
dockerImage.push("${env.BUILD_NUMBER}")
dockerImage.push("latest")
}
}
stage('Removing image Locally') {
sh "docker rmi $registryhomol/$application_name:$BUILD_NUMBER"
sh "docker rmi $registryhomol/$application_name:latest"
}
stage ('Pull imagem on DEV') {
sshagent(credentials : ['DEV']) {
sh "$SERVER_HOMOL_SSH 'docker pull $registryhomol/$application_name:latest'"
}
}
stage('Deploy container on DEV') {
configFileProvider([configFile(fileId: "$File_Json_Id_APP_BASE_PROD", targetLocation: 'container-plataformbase-deploy-homol.json')]) {
def url = "http://$SERVER_HOMOL/containers/$application_name?force=true"
def response = sh(script: "curl -v -X DELETE $url", returnStdout: true).trim()
echo response
url = "http://$SERVER_HOMOL/containers/create?name=$application_name"
response = sh(script: "curl -v -X POST -H 'Content-Type: application/json' -d @container-plataformbase-deploy-homol.json -s $url", returnStdout: true).trim()
echo response
}
}
stage('Start container on DEV') {
final String url = "http://$SERVER_HOMOL/containers/$application_name/start"
final String response = sh(script: "curl -v -X POST -s $url", returnStdout: true).trim()
echo response
}
stage('Send message to Discord') {
//SEND DISCORD NOTIFICATION
def discordImageSuccess = 'https://www.jenkins.io/images/logos/formal/256.png'
def discordImageError = 'https://www.jenkins.io/images/logos/fire/256.png'
def Author_Name=sh(script: "git show -s --pretty=%an", returnStdout: true).trim()
def Author_Email=sh(script: "git show -s --pretty=%ae", returnStdout: true).trim()
def Author_Data=sh(script: "git log -1 --format=%cd --date=local",returnStdout: true).trim()
def Project_Name=sh(script: "git config --local remote.origin.url",returnStdout: true).trim()
def Last_Commit=sh(script: "git show --summary | grep 'commit' | awk '{print \$2}'",returnStdout: true).trim()
def Comment_Commit=sh(script: "git log -1 --pretty=%B",returnStdout: true).trim()
def Date_Commit=sh(script: "git show -s --format=%ci",returnStdout: true).trim()
def discordDesc =
"Result: ${currentBuild.currentResult}\n" +
"Project: $Project_Name\n" +
"Commit: $Last_Commit\n" +
"Author: $Author_Name\n" +
"Author_Email: $Author_Email\n" +
"Message: $Comment_Commit\n" +
"Date: $Date_Commit\n" +
"Duration: ${currentBuild.durationString}"
//Variaveis de ambiente do Jenkins - NOME DO JOB E NÚMERO DO JOB
def discordFooter = "${env.JOB_BASE_NAME} (#${BUILD_NUMBER})"
def discordTitle = "${env.JOB_BASE_NAME} (build #${BUILD_NUMBER})"
def urlWebhook = "https://discord.com/api/webhooks/$DiscordKey"
discordSend description: discordDesc,
footer: discordFooter,
link: env.JOB_URL,
result: currentBuild.currentResult,
title: discordTitle,
webhookURL: urlWebhook,
successful: currentBuild.resultIsBetterOrEqualTo('SUCCESS'),
thumbnail: 'SUCCESS'.equals(currentBuild.currentResult) ? discordImageSuccess : discordImageError
}
stage('Send message to Telegram') {
def Author_Name=sh(script: "git show -s --pretty=%an", returnStdout: true).trim()
def Author_Email=sh(script: "git show -s --pretty=%ae", returnStdout: true).trim()
def Author_Data=sh(script: "git log -1 --format=%cd --date=local",returnStdout: true).trim()
def Project_Name=sh(script: "git config --local remote.origin.url",returnStdout: true).trim()
def Last_Commit=sh(script: "git show --summary | grep 'commit' | awk '{print \$2}'",returnStdout: true).trim()
def Comment_Commit=sh(script: "git log -1 --pretty=%B",returnStdout: true).trim()
def Date_Commit=sh(script: "git show -s --format=%ci",returnStdout: true).trim()
def Branch_Name=sh(script: "git rev-parse --abbrev-ref HEAD",returnStdout: true).trim()
withCredentials([string(credentialsId: 'telegramToken', variable: 'TOKEN'), string(credentialsId: 'telegramChatId', variable: 'CHAT_ID')]) {
sh ("""
curl -s -X POST https://api.telegram.org/bot${TOKEN}/sendMessage -d chat_id=${CHAT_ID} -d parse_mode=markdown -d text='*Project*: *${Project_Name}* \n *Branch*: ${Branch_Name} \n *Author*: *${Author_Name}* \n *Author_Email*: *${Author_Email}* \n *Commit_ID*: *${Last_Commit}* \n *Message_Commit*: *${Comment_Commit}* \n *Date_Commit*: *${Date_Commit}* \n *Duration*: *${currentBuild.durationString}*'
""")
}
}
}