-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
159 lines (140 loc) · 8 KB
/
Copy pathJenkinsfile
File metadata and controls
159 lines (140 loc) · 8 KB
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
pipeline {
agent any
environment {
AWS_REGION = 'eu-west-1'
INSTANCE_ID = 'i-06de104551729bd9f'
SCRIPT_PATH = '/var/www/<nome progetto>le/deploy.sh'
GITLAB_TOKEN_ID = 'gitlab-access-token-<nome progetto>le'
MAX_WAIT_TIME = '300' // Come stringa per evitare problemi di cast
POLL_INTERVAL = '5' // Come stringa per evitare problemi di cast
EMAIL_TO = 'g.casati@exabytesrl.it,m.sottana@exabytesrl.it'
}
stages {
stage('Lancio script di deploy') {
steps {
script {
withAWS(credentials: 'aws-jenkins', region: env.AWS_REGION) {
withCredentials([usernamePassword(credentialsId: env.GITLAB_TOKEN_ID, usernameVariable: 'GIT_USERNAME', passwordVariable: 'GITLAB_TOKEN')]) {
// inizializza variabili per il polling
def status = "InProgress"
def startTime = System.currentTimeMillis()
def elapsedTime = 0
def command = "GITLAB_TOKEN=${GITLAB_TOKEN} ${SCRIPT_PATH}"
// esegui il comando ssm e cattura il CommandId
commandId = sh(
script: """
aws ssm send-command \
--region ${AWS_REGION} \
--instance-ids ${INSTANCE_ID} \
--document-name "AWS-RunShellScript" \
--parameters 'commands=["${command}"]' \
--output text \
--query 'Command.CommandId'
""",
returnStdout: true
).trim()
echo "Comando SSM inviato, CommandId: ${commandId}"
// converti le variabili d'ambiente in interi
int maxWaitTimeMs = env.MAX_WAIT_TIME.toInteger() * 1000
int pollIntervalSec = env.POLL_INTERVAL.toInteger()
// esegui il polling fino a quando il comando non è completato o scade il timeout
while (status == "InProgress" && elapsedTime < maxWaitTimeMs) {
sleep(time: pollIntervalSec, unit: 'SECONDS')
// recupera lo stato del comando
status = sh(
script: """
aws ssm get-command-invocation \
--command-id ${commandId} \
--instance-id ${INSTANCE_ID} \
--query 'Status' \
--output text
""",
returnStdout: true
).trim()
// recupera lo standard output del comando
env.SCRIPT_STDOUT = sh(
script: """
aws ssm get-command-invocation \
--command-id ${commandId} \
--instance-id ${INSTANCE_ID} \
--query 'StandardOutputContent' \
--output text
""",
returnStdout: true
).trim()
// recupera lo standard error del comando
env.SCRIPT_STDERR = sh(
script: """
aws ssm get-command-invocation \
--command-id ${commandId} \
--instance-id ${INSTANCE_ID} \
--query 'StandardErrorContent' \
--output text
""",
returnStdout: true
).trim()
elapsedTime = System.currentTimeMillis() - startTime
echo "Stato comando: ${status}, Tempo trascorso: ${elapsedTime/1000} secondi"
}
// calcola il tempo totale trascorso e salvalo in una variabile d'ambiente
elapsedTime = System.currentTimeMillis() - startTime
env.ELAPSED_TIME = "${elapsedTime/1000}"
echo "Standard output dello script: ${env.SCRIPT_STDOUT}"
echo "Standard error dello script: ${env.SCRIPT_STDERR}"
// gestione timeout
if (status == "InProgress") {
env.ERROR_MESSAGE = "Deploy fallito: timeout raggiunto dopo ${env.MAX_WAIT_TIME} secondi"
error(env.ERROR_MESSAGE)
}
// gestione fallimento del comando ssm
else if (status == "Failed") {
env.ERROR_MESSAGE = "Deploy fallito: il comando SSM è terminato con stato 'Failed'."
error(env.ERROR_MESSAGE)
}
// gestione standard output che non termina con DEPLOY_SUCCESS
else if (!env.SCRIPT_STDOUT.endsWith("DEPLOY_SUCCESS")) {
env.ERROR_MESSAGE = "Deploy fallito: Lo standard output dello script non termina con 'DEPLOY_SUCCESS'."
error(env.ERROR_MESSAGE)
}
// tutto ok
else {
env.ERROR_MESSAGE = ""
echo "Deploy completato con successo!"
}
}
}
}
}
}
}
post {
always {
script {
def subjectResult = currentBuild.result == 'FAILURE' ? 'fallito' : 'eseguito'
def contentResult = currentBuild.result == 'FAILURE' ? 'fallito' : 'stato eseguito con successo'
echo "Invio mail a ${env.EMAIL_TO}"
def formattedErrorMessage = env.ERROR_MESSAGE ? env.ERROR_MESSAGE.replaceAll('(\r\n|\r|\n)', '<br/>') : '-'
def formattedScriptStdOut = env.SCRIPT_STDOUT ? env.SCRIPT_STDOUT.replaceAll('(\r\n|\r|\n)', '<br/>') : '-'
def formattedScriptStdErr = env.SCRIPT_STDERR ? env.SCRIPT_STDERR.replaceAll('(\r\n|\r|\n)', '<br/>') : '-'
emailext(
subject: "Deploy ${subjectResult} per ${env.JOB_NAME} #${env.BUILD_NUMBER}",
body: """
Il deploy è ${contentResult} per il progetto <b>${env.JOB_NAME}</b>.
<br/><br/><b>URL:</b>
<br/>${env.BUILD_URL}
<br/><br/><b>Tempo di esecuzione:</b>
<br/>${env.ELAPSED_TIME} secondi
<br/><br/><b>Errore:</b>
<br/>${formattedErrorMessage}
<br/><br/><b>Standard output dello script:</b>
<br/>${formattedScriptStdOut}
<br/><br/><b>Standard error dello script:</b>
<br/>${formattedScriptStdErr}
""",
to: env.EMAIL_TO,
from: "jenkins@jenkins.exacloud.it"
)
}
}
}
}