This commit is contained in:
Ionel Andrei Cataon
2026-02-12 17:23:11 +02:00
parent 768f997044
commit 043e60906a

View File

@@ -1,7 +1,7 @@
pipeline { pipeline {
agent any agent any
// Această secțiune spune Jenkins-ului să ruleze build-ul la fiecare 5 minute // Programarea automată a rulării la fiecare 5 minute
triggers { triggers {
cron('*/5 * * * *') cron('*/5 * * * *')
} }
@@ -11,40 +11,73 @@ pipeline {
steps { steps {
script { script {
dir('project2') { dir('project2') {
// --no-cache asigură că Docker preia mereu ultima variantă a codului Python
sh "docker build --no-cache -t quotes-app ." sh "docker build --no-cache -t quotes-app ."
} }
} }
} }
} }
stage('Run') {
stage('Run & Extract') {
steps { steps {
script { script {
dir('project2') { dir('project2') {
// Curățăm preventiv containerul anterior
sh "docker rm -f quotes-worker || true" sh "docker rm -f quotes-worker || true"
// Rulăm cu DNS setat pentru a evita erorile de rețea anterioare
sh "docker run --name quotes-worker --dns 8.8.8.8 quotes-app" try {
sh "docker cp quotes-worker:/app/quote.txt . " // Rulăm containerul cu DNS setat pentru a evita erorile de Name Resolution
sh "docker run --name quotes-worker --dns 8.8.8.8 quotes-app"
// Copiem fișierul generat din container în workspace-ul Jenkins
sh "docker cp quotes-worker:/app/quote.txt ."
} catch (e) {
echo "⚠️ Eroare la execuție, dar verificăm fișierul pentru notificare."
} finally {
sh "docker rm -f quotes-worker || true"
}
} }
} }
} }
} }
} }
post { post {
always { always {
script { script {
def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjJjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJWK38mqa" def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjJjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJWK38mqa"
def quote = fileExists('project2/quote.txt') ? readFile('project2/quote.txt').trim() : "Nu am putut genera citatul."
def safeQuote = quote.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n') // Citirea citatului și gestionarea erorilor de fișier
def quoteContent = "Nu am putut citi citatul."
if (fileExists('project2/quote.txt')) {
quoteContent = readFile('project2/quote.txt').trim()
// Escapăm caracterele speciale pentru a nu strica formatul JSON
quoteContent = quoteContent.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n')
}
def resultStatus = currentBuild.result ?: 'SUCCESS'
def statusEmoji = (resultStatus == 'SUCCESS') ? "✅" : "⚠️"
def header = "${statusEmoji} **Pipeline Automat (Build #${env.BUILD_NUMBER})**"
echo "🚀 Trimitere notificare către Discord..."
// Crearea payload-ului JSON manual (fără a depinde de plugin-ul writeJSON)
sh """ sh """
cat << 'EOF' > payload.json cat << 'EOF' > discord_payload.json
{ {
"content": "✅ **Pipeline Automat (Build #${env.BUILD_NUMBER})**\\n\\n${safeQuote}" "content": "${header}\\n\\n${quoteContent}"
} }
EOF EOF
""" """
sh "curl -X POST -H 'Content-Type: application/json' --data-binary @payload.json '${discordUrl}'"
// Trimiterea către Discord folosind curl
sh "curl -X POST -H 'Content-Type: application/json' --data-binary @discord_payload.json '${discordUrl}'"
// CURĂȚARE: Ștergem imaginile Docker inutile (dangling images) pentru a nu umple discul
sh "docker image prune -f"
// Curățăm workspace-ul curent
deleteDir()
} }
} }
} }