This commit is contained in:
Ionel Andrei Cataon
2026-02-12 17:17:23 +02:00
parent 2e1da76f1f
commit 0f0f706960
2 changed files with 20 additions and 35 deletions

View File

@@ -1,28 +1,28 @@
import requests import requests
import json
def get_quote(): def get_quote():
url = "https://api.quotable.io/random" # Folosim un endpoint de fallback care e foarte stabil
url = "https://api.adviceslip.com/advice"
try: try:
print(f"Connecting to {url}...") print(f"Încercăm conectarea la {url}...")
response = requests.get(url, timeout=15) response = requests.get(url, timeout=10)
response.raise_for_status() response.raise_for_status()
data = response.json() data = response.json()
content = data.get('content', 'No content') advice = data.get('slip', {}).get('advice', 'Fii bun cu cei din jur.')
author = data.get('author', 'Unknown Author')
output = f"📜 *\"{content}\"*\n\n✍️ **Autor:** {author}" # Deoarece AdviceSlip e anonim, punem noi un autor simbolic
output = f"📜 *\"{advice}\"*\n\n✍️ **Autor:** AdviceSlip Bot"
with open("quote.txt", "w", encoding="utf-8") as f: with open("quote.txt", "w", encoding="utf-8") as f:
f.write(output) f.write(output)
print("Success: Quote and Author saved.") print("Succes: Sfatul a fost salvat.")
except Exception as e: except Exception as e:
error_msg = f"⚠️ Eroare API: {str(e)}" fallback = "📜 *\"Eroarea de astăzi este lecția de mâine.\"*\n\n✍️ **Autor:** Jenkins Debugger"
print(error_msg)
with open("quote.txt", "w", encoding="utf-8") as f: with open("quote.txt", "w", encoding="utf-8") as f:
f.write(error_msg) f.write(fallback)
print(f"Fallback activat din cauza: {str(e)}")
if __name__ == "__main__": if __name__ == "__main__":
get_quote() get_quote()

View File

@@ -1,63 +1,48 @@
pipeline { pipeline {
agent any agent any
stages { stages {
stage('Build & Lint') { stage('Build') {
steps { steps {
script { script {
dir('project2') { dir('project2') {
// Folosim --no-cache ca să fim siguri că ia noul Python
sh "docker build --no-cache -t quotes-app ." sh "docker build --no-cache -t quotes-app ."
echo "🔍 Verificăm sintaxa codului..."
sh "docker run --rm quotes-app python -m py_compile famousquotes.py"
} }
} }
} }
} }
stage('Run') {
stage('Run & Extract') {
steps { steps {
script { script {
dir('project2') { dir('project2') {
sh "docker rm -f quotes-worker || true" sh "docker rm -f quotes-worker || true"
// Rulăm și afișăm log-urile containerului // Testăm rețeaua înainte de rulare
sh "docker run --rm --dns 8.8.8.8 alpine ping -c 2 google.com || echo 'Internet indisponibil'"
sh "docker run --name quotes-worker --dns 8.8.8.8 quotes-app" sh "docker run --name quotes-worker --dns 8.8.8.8 quotes-app"
sh "docker logs quotes-worker"
sh "docker cp quotes-worker:/app/quote.txt ." sh "docker cp quotes-worker:/app/quote.txt ."
// DEBUG: Vedem ce e în fișier direct în Jenkins
echo "Conținut fișier extras:"
sh "cat quote.txt"
} }
} }
} }
} }
} }
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 quoteContent = "Eroare la citirea fișierului."
def quoteContent = "Fișierul cu citate lipsește."
if (fileExists('project2/quote.txt')) { if (fileExists('project2/quote.txt')) {
quoteContent = readFile('project2/quote.txt').trim() quoteContent = readFile('project2/quote.txt').trim()
// Escapăm caracterele speciale pentru a nu strica JSON-ul
quoteContent = quoteContent.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n') quoteContent = quoteContent.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n')
} }
def resultStatus = currentBuild.result ?: 'SUCCESS'
def statusEmoji = (resultStatus == 'SUCCESS') ? "✅" : "⚠️"
def header = "${statusEmoji} **Pipeline Finalizat (Build #${env.BUILD_NUMBER})**"
sh """ sh """
cat << 'EOF' > discord_payload.json cat << 'EOF' > discord_payload.json
{ {
"content": "${header}\\n\\n${quoteContent}" "content": "✅ **Pipeline Finalizat (Build #${env.BUILD_NUMBER})**\\n\\n${quoteContent}"
} }
EOF EOF
""" """
sh "curl -X POST -H 'Content-Type: application/json' --data-binary @discord_payload.json '${discordUrl}'" sh "curl -X POST -H 'Content-Type: application/json' --data-binary @discord_payload.json '${discordUrl}'"
} }
} }