This commit is contained in:
Ionel Andrei Cataon
2026-02-12 16:47:46 +02:00
parent ddce0166aa
commit f6b3f8ad85
3 changed files with 67 additions and 54 deletions

View File

@@ -2,12 +2,11 @@ FROM python:3.9-slim
WORKDIR /app WORKDIR /app
COPY requirements.txt . RUN pip install --no-cache-dir requests
RUN pip install --no-cache-dir -r requirements.txt
COPY famousquotes.py . COPY famousquotes.py .
# Defines o varianta de baza RUN touch quote.txt && chmod 666 quote.txt
ENV QUOTE_API_URL="https://api.quotable.io/random"
# Comanda de rulare
CMD ["python", "famousquotes.py"] CMD ["python", "famousquotes.py"]

View File

@@ -1,37 +1,64 @@
import requests pipeline {
import os agent any
import sys
# Folosim variabila de mediu sau un fallback stages {
API_URL = os.getenv("QUOTE_API_URL", "https://api.quotable.io/random") stage('Build & Lint') {
steps {
script {
dir('project2') {
// 1. Construim imaginea
sh "docker build -t quotes-app ."
def get_quote(): // 2. LINT CHECK: Verificăm sintaxa Python în interiorul containerului
try: echo "🔍 Verificăm sintaxa codului..."
# Timeout de 10 secunde ca să nu stea Jenkins blocat dacă API-ul nu răspunde sh "docker run --rm quotes-app python -m py_compile famousquotes.py"
response = requests.get(API_URL, timeout=10) }
response.raise_for_status() }
data = response.json() }
}
if isinstance(data, list): stage('Run & Extract') {
data = data[0] steps {
script {
dir('project2') {
try {
// 3. Rulăm cu DNS forțat pentru a evita "NameResolutionError"
sh "docker run --name quotes-worker --network host --dns 8.8.8.8 quotes-app"
quote = data.get('content') or data.get('advice') or data.get('q') or "No quote today." // 4. Extragem rezultatul
author = data.get('author') or data.get('a') or "Unknown" sh "docker cp quotes-worker:/app/quote.txt ."
} catch (e) {
echo "Eroare la rulare, dar încercăm notificarea."
} finally {
sh "docker rm -f quotes-worker || true"
}
}
}
}
}
}
# Mesaj formatat pentru Discord post {
message = f"📜 *\"{quote}\"* \n\n✍️ **{author}**" always {
script {
// !!! Pune Webhook-ul tău aici !!!
def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjJjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJWK38mqa"
with open("quote.txt", "w", encoding="utf-8") as f: def quoteText = "Nu am putut citi fișierul."
f.write(message) try {
quoteText = readFile('project2/quote.txt').trim()
} catch (err) {
echo "Fișierul nu a fost găsit."
}
print(f"Citat salvat cu succes!") def statusEmoji = (currentBuild.result == 'SUCCESS') ? "" : "⚠️"
except Exception as e: sh """
print(f"Eroare API: {e}") curl -X POST -H 'Content-Type: application/json' \
with open("quote.txt", "w", encoding="utf-8") as f: -d '{"content": "${statusEmoji} **Pipeline Finalizat (Build #${env.BUILD_NUMBER})**\\n\\n${quoteText}"}' \
f.write("⚠️ Nu am putut prelua citatul din cauza unei erori de rețea.") '${discordUrl}'
# Ieșim cu 0 ca să lăsăm Jenkins să trimită notificarea chiar dacă API-ul a dat fail """
sys.exit(0) }
}
if __name__ == "__main__": }
get_quote() }

View File

@@ -1,21 +1,18 @@
pipeline { pipeline {
agent any agent any
stages { stages {
stage('Build & Run') { stage('Build & Run') {
steps { steps {
script { script {
dir('project2') { dir('project2') {
sh "docker build -t quotes-app ." sh "docker build -t quotes-app ."
try { try {
// Rulăm containerul și dăm un nume pentru a putea copia fișierul afară // Adăugăm --dns 8.8.8.8 pentru a rezolva problema de rețea
sh "docker run --name quotes-container --network host quotes-app" sh "docker run --name quotes-container --network host --dns 8.8.8.8 quotes-app"
sh "docker cp quotes-container:/app/quote.txt ." sh "docker cp quotes-container:/app/quote.txt ."
} catch (e) { } catch (e) {
echo "Eroare la execuție, trecem la notificare." echo "Eroare la execuție."
} finally { } finally {
// Curățăm containerul ca să nu dea eroare de 'name already in use' la următorul build
sh "docker rm -f quotes-container || true" sh "docker rm -f quotes-container || true"
} }
} }
@@ -23,23 +20,13 @@ pipeline {
} }
} }
} }
post { post {
always { always {
script { script {
// !!! PUNE WEBHOOK-UL TĂU AICI !!!
def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjJjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJWK38mqa" def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjJjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJWK38mqa"
def quoteText = readFile('project2/quote.txt').trim()
def quoteText = "Fișierul cu citate nu a fost găsit."
try {
quoteText = readFile('project2/quote.txt').trim()
} catch (err) {
echo "Eroare la citirea fișierului."
}
def statusEmoji = (currentBuild.result == 'SUCCESS') ? "✅" : "⚠️" def statusEmoji = (currentBuild.result == 'SUCCESS') ? "✅" : "⚠️"
// Trimitem notificarea
sh """ sh """
curl -X POST -H 'Content-Type: application/json' \ curl -X POST -H 'Content-Type: application/json' \
-d '{"content": "${statusEmoji} **Pipeline Finalizat (Build #${env.BUILD_NUMBER})**\\n\\n${quoteText}"}' \ -d '{"content": "${statusEmoji} **Pipeline Finalizat (Build #${env.BUILD_NUMBER})**\\n\\n${quoteText}"}' \