From 77604629d617832583671d4bb5f43c4233533771 Mon Sep 17 00:00:00 2001 From: Ionel Andrei Cataon Date: Thu, 12 Feb 2026 16:32:01 +0200 Subject: [PATCH] pj2 --- project2/famousquotes.py | 26 +++++++++++----------- project2/jenkinsfile | 47 +++++++++++++++------------------------- 2 files changed, 31 insertions(+), 42 deletions(-) diff --git a/project2/famousquotes.py b/project2/famousquotes.py index eb3b793..cbcaa42 100644 --- a/project2/famousquotes.py +++ b/project2/famousquotes.py @@ -1,21 +1,21 @@ -import requests -import os -import sys - -API_URL = os.getenv("QUOTE_API_URL", "https://api.quotable.io/random") - def get_quote(): try: response = requests.get(API_URL, timeout=10) - response.raise_for_status() + 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" + print("-" * 30) - print(f"CITAT: {data['content']}") - print(f"AUTOR: {data['author']}") + print(f"CITAT: {quote}") + print(f"AUTOR: {author}") print("-" * 30) except Exception as e: print(f"Eroare la apelarea API-ului: {e}") - sys.exit(1) - -if __name__ == "__main__": - get_quote() \ No newline at end of file + sys.exit(1) \ No newline at end of file diff --git a/project2/jenkinsfile b/project2/jenkinsfile index e05af8d..8766cdd 100644 --- a/project2/jenkinsfile +++ b/project2/jenkinsfile @@ -2,21 +2,12 @@ pipeline { agent any stages { - stage('Initialize & Notify') { - steps { - script { - // Notificăm imediat ce pornește build-ul - sendDiscordNotification("🚀 **Proiect 2**: Build #${env.BUILD_NUMBER} a început pe ramura develop.") - } - } - } - stage('Lint & Build') { steps { script { dir('project2') { sh "docker build -t quotes-app ." - // Linting folosind imaginea construită + // Linting simplu sh "docker run --rm quotes-app python -m py_compile famousquotes.py" } } @@ -26,29 +17,27 @@ pipeline { stage('Run App') { steps { script { - try { - dir('project2') { - // Folosim un API compatibil care returnează cheia 'content' - // și --network host pentru a asigura ieșirea spre Discord ulterior - sh "docker run --rm --network host -e QUOTE_API_URL='http://api.staging.quotable.io/random' quotes-app" - } - sendDiscordNotification("✅ **Proiect 2**: Build #${env.BUILD_NUMBER} finalizat cu succes!") - } catch (Exception e) { - sendDiscordNotification("❌ **Proiect 2**: Build #${env.BUILD_NUMBER} a eșuat la execuție.") - error("Build failed: ${e.message}") + dir('project2') { + // Folosim --network host și un DNS public pentru a forța ieșirea la internet + sh "docker run --rm --network host --dns 8.8.8.8 -e QUOTE_API_URL='https://api.quotable.io/random' quotes-app" } } } } } -} -// Funcție separată pentru a fi siguri că apelul curl este curat -def sendDiscordNotification(String message) { - def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJwk38mqa" - sh """ - curl -X POST -H 'Content-Type: application/json' \ - -d '{"content": "${message}"}' \ - ${discordUrl} - """ + post { + always { + script { + def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjJjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJWK38mqa" + def status = currentBuild.result ?: 'SUCCESS' + + sh """ + curl -X POST -H 'Content-Type: application/json' \ + -d '{"content": "🚀 Build #${env.BUILD_NUMBER} s-a terminat cu status: ${status}"}' \ + '${discordUrl}' + """ + } + } + } } \ No newline at end of file