Skip to content

Instantly share code, notes, and snippets.

@GGontijo
Last active September 16, 2024 20:33
Show Gist options
  • Save GGontijo/bed21fe5601b2bcc985108dfe10515a5 to your computer and use it in GitHub Desktop.
Save GGontijo/bed21fe5601b2bcc985108dfe10515a5 to your computer and use it in GitHub Desktop.
webhook deploy
from fastapi import FastAPI, Request, HTTPException
from pydantic import BaseModel
import os
import requests
app = FastAPI()
TELEGRAM_BOT_TOKEN = ""
TELEGRAM_CHAT_ID = ""
def notificar(message: str):
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
payload = {
'chat_id': TELEGRAM_CHAT_ID,
'text': message
}
try:
response = requests.post(url, data=payload)
if response.status_code == 200:
print("Notificação enviada com sucesso!")
else:
print(f"Falha ao enviar notificação. Status code: {response.status_code}")
except Exception as e:
print(f"Erro ao enviar mensagem ao Telegram: {e}")
@app.post("/webhook")
async def webhook(request: Request):
ip_remetente = request.headers.get('X-Forwarded-For', request.client.host)
# Faz o git pull e reinicia o docker-compose
os.system('cd /root/bot-video-downloader/ && git pull origin main')
os.system('cd /root/bot-video-downloader/ && docker-compose up --build -d')
# Notificar via Telegram
mensagem = (
f"Deploy realizado com sucesso!\n\n"
f"IP do remetente: {ip_remetente}\n"
)
notificar(mensagem)
return {"message": "Deploy realizado com sucesso!"}, 200
#sudo nano /etc/systemd/system/webhook.service
[Unit]
Description=Servidor Webhook para Deploy Automático
After=network.target
[Service]
ExecStart=/usr/local/bin/uvicorn deploy:app --host 0.0.0.0 --port 8089 --reload
WorkingDirectory=/caminho/para/
StandardOutput=inherit
StandardError=inherit
Restart=always
User=seu_usuario
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment