diff --git a/project2/Dockerfile b/project2/Dockerfile new file mode 100644 index 0000000..4ffd785 --- /dev/null +++ b/project2/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.9-slim + +WORKDIR /app + +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY quotes.py . + +# Defines o varianta de baza +ENV QUOTE_API_URL="https://api.quotable.io/random" + +CMD ["python", "quotes.py"] \ No newline at end of file diff --git a/project2/famousquotes.py b/project2/famousquotes.py new file mode 100644 index 0000000..89a99a9 --- /dev/null +++ b/project2/famousquotes.py @@ -0,0 +1,22 @@ +import requests +import os +import sys + +# URL-ul API-ului poate fi setat prin variabila de mediu QUOTE_API_URL, altfel se folosește valoarea implicită +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() # Aruncă eroare dacă statusul nu e 200 + data = response.json() + print("-" * 30) + print(f"CITAT: {data['content']}") + print(f"AUTOR: {data['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 diff --git a/project2/jenkinsfile b/project2/jenkinsfile new file mode 100644 index 0000000..6ceb342 --- /dev/null +++ b/project2/jenkinsfile @@ -0,0 +1,49 @@ +pipeline { + agent any + + stages { + stage('Lint Check') { + steps { + script { + echo "Verificăm sintaxa Python..." + sh 'docker run --rm python:3.9-slim python -m py_compile project2/quotes.py' + } + } + } + + stage('Build Docker Image') { + steps { + script { + sh 'cd project2 && docker build -t quotes-app .' + } + } + } + + stage('Run & Test API') { + steps { + script { + echo "Rulăm aplicația și verificăm conexiunea API..." + // Pornim containerul și îi pasăm URL-ul API-ului ca variabilă de mediu + sh 'docker run --rm -e QUOTE_API_URL="https://api.quotable.io/random" quotes-app' + } + } + } + } + + post { + failure { + script { + def discordUrl = "https://discord.com/api/webhooks/1471492658336891013/T5s6ZKZjJjDMHXc3k3jjZdk6m5EV12bKF1wda9d5I_gZJrsDZQ1m1m078IiLJWK38mqa" + def payload = """ + { + "content": "❌ **FAILED: Quotes App**\\nEtapa a eșuat la build-ul #${env.BUILD_NUMBER}. Verifica codul Python!" + } + """ + sh "curl -H 'Content-Type: application/json' -d '${payload}' ${discordUrl}" + } + } + success { + echo "Proiectul 2 a fost build-uit cu succes!" + } + } +} \ No newline at end of file diff --git a/project2/requirements.txt b/project2/requirements.txt new file mode 100644 index 0000000..077c95d --- /dev/null +++ b/project2/requirements.txt @@ -0,0 +1 @@ +requests==2.31.0 \ No newline at end of file