p2
This commit is contained in:
@@ -1,64 +1,37 @@
|
|||||||
pipeline {
|
import requests
|
||||||
agent any
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
stages {
|
# API stabil pentru a evita erorile de rețea anterioare
|
||||||
stage('Build & Lint') {
|
API_URL = os.getenv("QUOTE_API_URL", "https://api.adviceslip.com/advice")
|
||||||
steps {
|
|
||||||
script {
|
|
||||||
dir('project2') {
|
|
||||||
// 1. Construim imaginea
|
|
||||||
sh "docker build -t quotes-app ."
|
|
||||||
|
|
||||||
// 2. LINT CHECK: Verificăm sintaxa Python în interiorul containerului
|
def get_quote():
|
||||||
echo "🔍 Verificăm sintaxa codului..."
|
try:
|
||||||
sh "docker run --rm quotes-app python -m py_compile famousquotes.py"
|
# Timeout de 20s pentru a preveni blocarea pipeline-ului
|
||||||
}
|
response = requests.get(API_URL, timeout=20)
|
||||||
}
|
response.raise_for_status()
|
||||||
}
|
data = response.json()
|
||||||
}
|
|
||||||
|
|
||||||
stage('Run & Extract') {
|
# Extragem mesajul (format AdviceSlip)
|
||||||
steps {
|
if 'slip' in data:
|
||||||
script {
|
quote = data['slip'].get('advice')
|
||||||
dir('project2') {
|
author = "AdviceSlip"
|
||||||
try {
|
else:
|
||||||
// 3. Rulăm cu DNS forțat pentru a evita "NameResolutionError"
|
quote = data.get('content') or data.get('q') or "No message found."
|
||||||
sh "docker run --name quotes-worker --network host --dns 8.8.8.8 quotes-app"
|
author = data.get('author') or data.get('a') or "Unknown"
|
||||||
|
|
||||||
// 4. Extragem rezultatul
|
message = f"📜 *\"{quote}\"* \n\n✍️ **{author}**"
|
||||||
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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
post {
|
# Salvăm fișierul local în container (/app)
|
||||||
always {
|
with open("quote.txt", "w", encoding="utf-8") as f:
|
||||||
script {
|
f.write(message)
|
||||||
// !!! Pune Webhook-ul tău aici !!!
|
|
||||||
def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjJjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJWK38mqa"
|
|
||||||
|
|
||||||
def quoteText = "Nu am putut citi fișierul."
|
print(f"Succes! Mesaj pregătit.")
|
||||||
try {
|
|
||||||
quoteText = readFile('project2/quote.txt').trim()
|
|
||||||
} catch (err) {
|
|
||||||
echo "Fișierul nu a fost găsit."
|
|
||||||
}
|
|
||||||
|
|
||||||
def statusEmoji = (currentBuild.result == 'SUCCESS') ? "✅" : "⚠️"
|
except Exception as e:
|
||||||
|
with open("quote.txt", "w", encoding="utf-8") as f:
|
||||||
|
f.write(f"⚠️ Eroare la preluare: {str(e)[:80]}")
|
||||||
|
sys.exit(0) # Nu oprim pipeline-ul forțat
|
||||||
|
|
||||||
sh """
|
if __name__ == "__main__":
|
||||||
curl -X POST -H 'Content-Type: application/json' \
|
get_quote()
|
||||||
-d '{"content": "${statusEmoji} **Pipeline Finalizat (Build #${env.BUILD_NUMBER})**\\n\\n${quoteText}"}' \
|
|
||||||
'${discordUrl}'
|
|
||||||
"""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,30 +1,56 @@
|
|||||||
pipeline {
|
pipeline {
|
||||||
agent any
|
agent any
|
||||||
|
|
||||||
stages {
|
stages {
|
||||||
stage('Build & Run') {
|
stage('Build & Lint') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
dir('project2') {
|
dir('project2') {
|
||||||
|
// 1. Construim imaginea
|
||||||
sh "docker build -t quotes-app ."
|
sh "docker build -t quotes-app ."
|
||||||
|
|
||||||
|
// 2. LINT CHECK: Verificăm sintaxa Python în interiorul containerului
|
||||||
|
echo "🔍 Verificăm sintaxa codului..."
|
||||||
|
sh "docker run --rm quotes-app python -m py_compile famousquotes.py"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stage('Run & Extract') {
|
||||||
|
steps {
|
||||||
|
script {
|
||||||
|
dir('project2') {
|
||||||
try {
|
try {
|
||||||
// Adăugăm --dns 8.8.8.8 pentru a rezolva problema de rețea
|
// 3. Rulăm cu DNS forțat pentru a evita "NameResolutionError"
|
||||||
sh "docker run --name quotes-container --network host --dns 8.8.8.8 quotes-app"
|
sh "docker run --name quotes-worker --network host --dns 8.8.8.8 quotes-app"
|
||||||
sh "docker cp quotes-container:/app/quote.txt ."
|
|
||||||
|
// 4. Extragem rezultatul
|
||||||
|
sh "docker cp quotes-worker:/app/quote.txt ."
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
echo "Eroare la execuție."
|
echo "Eroare la rulare, dar încercăm notificarea."
|
||||||
} finally {
|
} finally {
|
||||||
sh "docker rm -f quotes-container || true"
|
sh "docker rm -f quotes-worker || true"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 = "Nu am putut citi fișierul."
|
||||||
|
try {
|
||||||
|
quoteText = readFile('project2/quote.txt').trim()
|
||||||
|
} catch (err) {
|
||||||
|
echo "Fișierul nu a fost găsit."
|
||||||
|
}
|
||||||
|
|
||||||
def statusEmoji = (currentBuild.result == 'SUCCESS') ? "✅" : "⚠️"
|
def statusEmoji = (currentBuild.result == 'SUCCESS') ? "✅" : "⚠️"
|
||||||
|
|
||||||
sh """
|
sh """
|
||||||
|
|||||||
Reference in New Issue
Block a user