first commit

This commit is contained in:
2025-01-07 18:37:10 -05:00
commit c3ef3f2dd5
5 changed files with 90 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
**/__pycache__/*
venv/*

9
Makefile Normal file
View File

@@ -0,0 +1,9 @@
build:
@:
deploy:
venv/bin/waitress-serve --host 0.0.0.0 --port 5000 listen:app
stop:
@:

22
deploy.py Normal file
View File

@@ -0,0 +1,22 @@
from dataclasses import dataclass
import subprocess
import os
@dataclass
class DeployerRequest:
repo: str
branch: str
commit_hash: str
def handle_request(rq: DeployerRequest):
subprocess.run(
["./deploy.sh"],
env=dict(
os.environ,
REPO=rq.repo,
BRANCH=rq.branch,
COMMIT_HASH=rq.commit_hash,
),
)

39
deploy.sh Normal file
View File

@@ -0,0 +1,39 @@
#!/bin/bash
$rootdir=$(pwd)
$deploydir=$rootdir/deployments/$REPO/$BRANCH
mkdir -p $deploydir
cd $deploydir
git clone $CLONE_URL .
git fetch origin $BRANCH
git reset --hard origin/$BRANCH
git checkout $COMMIT_HASH
make build
echo "
[Unit]
Description=Deployment of $REPO/$BRANCH
After=network.target
[Service]
Type=simple
ExecStart=make deploy
User=drm
WorkingDirectory=$deploydir
Restart=on-failure
StandardOutput=file:$deploydir/server.log
StandardError=file:$deploydir/server.log
[Install]
WantedBy=multi-user.target
" > deploy.service
systemctl daemon reload
$service="deployer-$repo-$branch.service"
ln -s $deploydir/deploy.service /etc/systemd/system/$service
systemctl stop $service
systemctl start $service

18
listen.py Normal file
View File

@@ -0,0 +1,18 @@
from flask import Flask, request
from .deploy import handle_request, DeployerRequest
app = Flask(__name__)
@app.route("/gitea", methods=["POST"])
def gitea():
data = request.get_json()
handle_request(
DeployerRequest(
repo = data["repository"]["name"],
branch = data["ref"].strip("refs/heads/"),
commit_hash = data["after"]
)
)