diff --git a/scripts/post-receive.sh b/scripts/post-receive.sh new file mode 100644 index 0000000..adb36df --- /dev/null +++ b/scripts/post-receive.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# Configuration (or set in environment variables on VPS) +export SENDER_EMAIL="your-gmail-id@gmail.com" +export SENDER_PASSWORD="your-16-digit-app-password" +export RECIPIENT_EMAIL="your-recipient-email@gmail.com" + +while read oldrev newrev refname +do + BRANCH=$(echo $refname | cut -d/ -f3) + COMMIT_HASH=$(git log -1 --format="%h" $newrev) + AUTHOR=$(git log -1 --format="%an <%ae>" $newrev) + MESSAGE=$(git log -1 --format="%B" $newrev) + TIMESTAMP=$(git log -1 --format="%cd" --date=local $newrev) + CHANGED_FILES=$(git diff-tree --no-commit-id --name-status -r $newrev | head -n 15) + + python3 /var/www/freshers-hub/scripts/send_git_email.py \ + "$BRANCH" \ + "$AUTHOR" \ + "$COMMIT_HASH" \ + "$TIMESTAMP" \ + "$MESSAGE" \ + "$CHANGED_FILES" + +done diff --git a/scripts/send_git_email.py b/scripts/send_git_email.py new file mode 100644 index 0000000..4a57bd4 --- /dev/null +++ b/scripts/send_git_email.py @@ -0,0 +1,83 @@ +import sys +import smtplib +import os +from email.mime.text import MIMEText +from email.mime.multipart import MIMEMultipart + +# Load configuration from environment or fallback defaults +SMTP_SERVER = os.environ.get("SMTP_SERVER", "smtp.gmail.com") +SMTP_PORT = int(os.environ.get("SMTP_PORT", 587)) +SENDER_EMAIL = os.environ.get("SENDER_EMAIL", "YOUR_EMAIL@gmail.com") +SENDER_PASSWORD = os.environ.get("SENDER_PASSWORD", "YOUR_GMAIL_APP_PASSWORD") +RECIPIENT_EMAIL = os.environ.get("RECIPIENT_EMAIL", "YOUR_EMAIL@gmail.com") + +if len(sys.argv) < 7: + print("Usage: python send_git_email.py ") + sys.exit(1) + +branch = sys.argv[1] +author = sys.argv[2] +commit_hash = sys.argv[3] +timestamp = sys.argv[4] +message = sys.argv[5] +changed_files = sys.argv[6] + +subject = f"[Freshers-Hub VPS] Code Push Report - Commit {commit_hash} ({branch})" + +html_content = f""" + + + + + + + +
+
🚀 Freshers-Hub Deployment & Push Report
+ +
Branch: {branch}
+
Author: {author}
+
Commit Hash: {commit_hash}
+
Time: {timestamp}
+ +
+ Commit Message: +
{message}
+
+ +
+ Changed Files (Top Modified): +
{changed_files}
+
+ + +
+ + +""" + +msg = MIMEMultipart("alternative") +msg["Subject"] = subject +msg["From"] = SENDER_EMAIL +msg["To"] = RECIPIENT_EMAIL +msg.attach(MIMEText(html_content, "html")) + +try: + with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server: + server.starttls() + server.login(SENDER_EMAIL, SENDER_PASSWORD) + server.sendmail(SENDER_EMAIL, RECIPIENT_EMAIL, msg.as_string()) + print("✅ Email report sent successfully.") +except Exception as e: + print(f"❌ Failed to send email report: {e}")