-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigma-update.groovy
96 lines (84 loc) · 3.84 KB
/
sigma-update.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
pipeline {
agent any
environment {
GIT_REPO = 'https://github.com/SigmaHQ/sigma'
DAYS_TO_CHECK = '7' // String, to be converted to integer
EMAIL_RECIPIENTS = 'recipient1@example.com,recipient2@example.com'
EMAIL_SENDER = 'sender@example.com'
}
stages {
stage('Clone Repository') {
steps {
git branch: 'master', url: "$GIT_REPO"
}
}
stage('Check for Changes') {
steps {
script {
// Convert DAYS_TO_CHECK to integer
def daysToCheck = DAYS_TO_CHECK.toInteger()
// Get the current date and subtract days
def currentDate = new Date()
def sdf = new java.text.SimpleDateFormat("yyyy-MM-dd")
// Calculate the cutoff date
def calendar = Calendar.getInstance()
calendar.setTime(currentDate)
calendar.add(Calendar.DATE, -daysToCheck) // Subtract the number of days
def cutoffDate = calendar.getTime()
def cutoffDateFormatted = sdf.format(cutoffDate)
// Use a more lenient format like "7 days ago"
def sinceOption = "${daysToCheck} days ago"
// Debug: Print out the date for troubleshooting
echo "Checking for changes since: ${sinceOption}"
// Run git log command with relative date format and filter for .yml files inside the rules/ directory
def gitLogOutput = sh(script: "git log --since='${sinceOption}' --name-only --pretty=format: -- 'rules/**/*' | grep -E '\\.yml\$' | sort | uniq", returnStdout: true).trim()
// Debug: Output the full git log result for troubleshooting
echo "Git log output:\n${gitLogOutput}"
if (gitLogOutput) {
// Save the output to an environment variable for post actions
env.GIT_LOG_OUTPUT = gitLogOutput
echo "New or edited .yml files found in the 'rules/' folder:"
echo gitLogOutput
} else {
env.GIT_LOG_OUTPUT = ""
echo "No new or updated .yml files found in the 'rules/' folder."
}
}
}
}
}
post {
success {
script {
if (env.GIT_LOG_OUTPUT) {
// Send email with the list of changed files
emailext(
subject: "Jenkins Pipeline: New or Updated .yml Files Sigma Found",
body: """
<p>The following .yml files have been added or updated in the last ${DAYS_TO_CHECK} days:</p>
<pre>${env.GIT_LOG_OUTPUT}</pre>
""",
recipientProviders: [[$class: 'DevelopersRecipientProvider']],
to: "${EMAIL_RECIPIENTS}",
from: "${EMAIL_SENDER}",
mimeType: 'text/html'
)
} else {
echo "No changes detected, email notification will not be sent."
}
}
}
failure {
emailext(
subject: "Jenkins Pipeline Failed",
body: """
<p>The pipeline failed during execution. Please check the logs for more details.</p>
""",
recipientProviders: [[$class: 'CulpritsRecipientProvider']],
to: "${EMAIL_RECIPIENTS}",
from: "${EMAIL_SENDER}",
mimeType: 'text/html'
)
}
}
}