pj2
This commit is contained in:
@@ -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():
|
def get_quote():
|
||||||
try:
|
try:
|
||||||
response = requests.get(API_URL, timeout=10)
|
response = requests.get(API_URL, timeout=10)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
data = response.json()
|
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("-" * 30)
|
||||||
print(f"CITAT: {data['content']}")
|
print(f"CITAT: {quote}")
|
||||||
print(f"AUTOR: {data['author']}")
|
print(f"AUTOR: {author}")
|
||||||
print("-" * 30)
|
print("-" * 30)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Eroare la apelarea API-ului: {e}")
|
print(f"Eroare la apelarea API-ului: {e}")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
get_quote()
|
|
||||||
@@ -2,21 +2,12 @@ pipeline {
|
|||||||
agent any
|
agent any
|
||||||
|
|
||||||
stages {
|
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') {
|
stage('Lint & Build') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
dir('project2') {
|
dir('project2') {
|
||||||
sh "docker build -t quotes-app ."
|
sh "docker build -t quotes-app ."
|
||||||
// Linting folosind imaginea construită
|
// Linting simplu
|
||||||
sh "docker run --rm quotes-app python -m py_compile famousquotes.py"
|
sh "docker run --rm quotes-app python -m py_compile famousquotes.py"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -26,29 +17,27 @@ pipeline {
|
|||||||
stage('Run App') {
|
stage('Run App') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
try {
|
|
||||||
dir('project2') {
|
dir('project2') {
|
||||||
// Folosim un API compatibil care returnează cheia 'content'
|
// Folosim --network host și un DNS public pentru a forța ieșirea la internet
|
||||||
// și --network host pentru a asigura ieșirea spre Discord ulterior
|
sh "docker run --rm --network host --dns 8.8.8.8 -e QUOTE_API_URL='https://api.quotable.io/random' quotes-app"
|
||||||
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}")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Funcție separată pentru a fi siguri că apelul curl este curat
|
post {
|
||||||
def sendDiscordNotification(String message) {
|
always {
|
||||||
def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJwk38mqa"
|
script {
|
||||||
|
def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjJjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJWK38mqa"
|
||||||
|
def status = currentBuild.result ?: 'SUCCESS'
|
||||||
|
|
||||||
sh """
|
sh """
|
||||||
curl -X POST -H 'Content-Type: application/json' \
|
curl -X POST -H 'Content-Type: application/json' \
|
||||||
-d '{"content": "${message}"}' \
|
-d '{"content": "🚀 Build #${env.BUILD_NUMBER} s-a terminat cu status: ${status}"}' \
|
||||||
${discordUrl}
|
'${discordUrl}'
|
||||||
"""
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user