This commit is contained in:
Ionel Andrei Cataon
2026-02-12 16:38:57 +02:00
parent 04f2f26561
commit 8713c62e82
2 changed files with 45 additions and 18 deletions

View File

@@ -1,21 +1,34 @@
import requests
import os
import sys
# Luăm URL-ul din variabila de mediu sau folosim unul default
API_URL = os.getenv("QUOTE_API_URL", "https://api.quotable.io/random")
def get_quote():
try:
response = requests.get(API_URL, timeout=10)
response = requests.get(API_URL, timeout=10, verify=True)
response.raise_for_status()
data = response.json()
# Dacă API-ul returnează o listă (ca ZenQuotes), luăm primul element
if isinstance(data, list):
data = data[0]
# Căutăm citatul și autorul în mai multe locuri posibile
quote = data.get('content') or data.get('advice') or data.get('q') or "Citatul nu a putut fi găsit."
author = data.get('author') or data.get('a') or "Autor necunoscut"
quote = data.get('content') or data.get('advice') or data.get('q') or "No quote found."
author = data.get('author') or data.get('a') or "Unknown Author"
print(f"CITAT: {quote}")
print(f"AUTOR: {author}")
with open("quote.txt", "w") as f:
# Afișăm în consola Jenkins pentru debug
print(f"Citat extras: {quote} - {author}")
with open("quote.txt", "w", encoding="utf-8") as f:
f.write(f"📜 *\"{quote}\"* \n\n✍️ **{author}**")
except Exception as e:
print(f"Eroare la apelarea API-ului: {e}")
sys.exit(1)
# În caz de eroare, scriem un mesaj de fallback în fișier
with open("quote.txt", "w", encoding="utf-8") as f:
f.write(f"⚠️ Nu am putut recupera citatul, dar build-ul a reușit.")
sys.exit(1)
if __name__ == "__main__":
get_quote()

View File

@@ -2,10 +2,17 @@ pipeline {
agent any
stages {
stage('Lint & Build') {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build Image') {
steps {
script {
dir('project2') {
// Construim imaginea aplicației
sh "docker build -t quotes-app ."
}
}
@@ -16,7 +23,10 @@ pipeline {
steps {
script {
dir('project2') {
// Folosim -v $(pwd):/app pentru ca Python să poată scrie quote.txt în folderul proiectului
echo "🚀 Rulăm aplicația și salvăm citatul..."
/* -v \$(pwd):/app -> mapează folderul curent din Jenkins în /app din container.
Astfel, quote.txt creat de Python va apărea direct în folderul project2.
*/
sh "docker run --rm --network host -v \$(pwd):/app -e QUOTE_API_URL='https://api.quotable.io/random' quotes-app"
}
}
@@ -27,22 +37,26 @@ pipeline {
post {
success {
script {
// Citim fișierul generat de Python din folderul project2
def messageContent = readFile('project2/quote.txt').trim()
def discordUrl = "URL_UL_TAU_DE_DISCORD"
def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjJjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJWK38mqa"
// Trimitem mesajul formatat
def quoteContent = readFile('project2/quote.txt').trim()
echo "Trimitem citatul pe Discord..."
sh """
curl -X POST -H 'Content-Type: application/json' \
-d '{"content": "✅ **Citatul Zilei (Build #${env.BUILD_NUMBER})**\\n\\n${messageContent}"}' \
-d '{"content": "✅ **Build #${env.BUILD_NUMBER} Finalizat cu Succes!**\\n\\n${quoteContent}"}' \
'${discordUrl}'
"""
}
}
failure {
script {
def discordUrl = "URL_UL_TAU_DE_DISCORD"
sh "curl -X POST -H 'Content-Type: application/json' -d '{\"content\": \"❌ **Build #${env.BUILD_NUMBER}** a eșuat. Verifică log-urile din Jenkins!\"}' '${discordUrl}'"
def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjJjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJWK38mqa"
sh """
curl -X POST -H 'Content-Type: application/json' \
-d '{"content": "❌ **Build #${env.BUILD_NUMBER} a eșuat!** Verifică log-urile din Jenkins."}' \
'${discordUrl}'
"""
}
}
}