forked from jaiswaladi246/3-Tier-DevSecOps-Mega-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile-slack_notifications
More file actions
110 lines (101 loc) · 3.97 KB
/
Copy pathJenkinsfile-slack_notifications
File metadata and controls
110 lines (101 loc) · 3.97 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
pipeline {
agent any
environment {
PROJECT_NAME = '🧰 My-App'
ENVIRONMENT = '🚀 Production'
}
stages {
stage('Compile') {
steps {
echo "🏗️ Compiling..."
}
}
stage('Test') {
steps {
echo "🧪 Running tests..."
}
}
stage('Build') {
steps {
echo "🧪 Building..."
}
}
stage('Security') {
steps {
echo "🧪 Security..."
}
}
stage('Deploy') {
steps {
echo "🧪 Deploy..."
}
}
}
post {
success {
withCredentials([string(credentialsId: 'slack-webhook', variable: 'SLACK_URL')]) {
script {
def message = """{
"text": "*✅ ${PROJECT_NAME} Build Successful!*",
"attachments": [
{
"color": "#36a64f",
"fields": [
{ "title": "Job", "value": "${env.JOB_NAME}", "short": true },
{ "title": "Build", "value": "#${env.BUILD_NUMBER}", "short": true },
{ "title": "Environment", "value": "${ENVIRONMENT}", "short": true }
],
"footer": "Jenkins CI",
"footer_icon": "https://www.jenkins.io/images/logos/jenkins/jenkins.png",
"ts": ${System.currentTimeMillis() / 1000},
"actions": [
{
"type": "button",
"text": "View Build",
"url": "${env.BUILD_URL}",
"style": "primary"
}
]
}
]
}"""
sh """curl -X POST -H 'Content-type: application/json' --data '${message}' $SLACK_URL"""
}
}
}
failure {
withCredentials([string(credentialsId: 'slack-webhook', variable: 'SLACK_URL')]) {
script {
def message = """{
"text": "<!here> *❌ ${PROJECT_NAME} Build Failed!*",
"attachments": [
{
"color": "#FF0000",
"fields": [
{ "title": "Job", "value": "${env.JOB_NAME}", "short": true },
{ "title": "Build", "value": "#${env.BUILD_NUMBER}", "short": true },
{ "title": "Environment", "value": "${ENVIRONMENT}", "short": true }
],
"footer": "Jenkins CI",
"footer_icon": "https://www.jenkins.io/images/logos/jenkins/jenkins.png",
"ts": ${System.currentTimeMillis() / 1000},
"actions": [
{
"type": "button",
"text": "View Build Logs",
"url": "${env.BUILD_URL}",
"style": "danger"
}
]
}
]
}"""
sh """curl -X POST -H 'Content-type: application/json' --data '${message}' $SLACK_URL"""
}
}
}
always {
echo "🎯 Post-build notification sent"
}
}
}