Add deployment actor attribution

This commit is contained in:
2026-05-28 12:04:12 +02:00
parent 5be315b4c1
commit 6fab554657
2 changed files with 94 additions and 17 deletions
+56 -8
View File
@@ -18,6 +18,13 @@ DB_FILE="/opt/appfactory/data/appfactory/appfactory.db"
APP_SOURCE_DIR="$WORKSPACE_DIR/$APP_ID" APP_SOURCE_DIR="$WORKSPACE_DIR/$APP_ID"
TOOLS_DIR="$WORKSPACE_DIR/appfactory-tools" TOOLS_DIR="$WORKSPACE_DIR/appfactory-tools"
TRIGGER_SOURCE="${APPFACTORY_TRIGGER_SOURCE:-cli}"
TRIGGERED_BY_USER_ID="${APPFACTORY_TRIGGERED_BY_USER_ID:-}"
TRIGGERED_BY_USERNAME="${APPFACTORY_TRIGGERED_BY_USERNAME:-}"
TRIGGERED_BY_DISPLAY_NAME="${APPFACTORY_TRIGGERED_BY_DISPLAY_NAME:-}"
COMMIT_AUTHOR="${APPFACTORY_COMMIT_AUTHOR:-}"
PUSHER="${APPFACTORY_PUSHER:-}"
if [ ! -d "$APP_SOURCE_DIR" ]; then if [ ! -d "$APP_SOURCE_DIR" ]; then
echo "Missing app source: $APP_SOURCE_DIR" echo "Missing app source: $APP_SOURCE_DIR"
exit 1 exit 1
@@ -28,12 +35,18 @@ if [ ! -f "$APP_SOURCE_DIR/Dockerfile" ]; then
exit 1 exit 1
fi fi
DEPLOY_ID="$(python3 - "$DB_FILE" "$APP_ID" <<'PY' DEPLOY_ID="$(python3 - "$DB_FILE" "$APP_ID" "$TRIGGER_SOURCE" "$TRIGGERED_BY_USER_ID" "$TRIGGERED_BY_USERNAME" "$TRIGGERED_BY_DISPLAY_NAME" "$COMMIT_AUTHOR" "$PUSHER" <<'PY'
import sqlite3 import sqlite3
import sys import sys
db_file = sys.argv[1] db_file = sys.argv[1]
app_id = sys.argv[2] app_id = sys.argv[2]
trigger_source = sys.argv[3]
triggered_by_user_id = sys.argv[4] or None
triggered_by_username = sys.argv[5] or None
triggered_by_display_name = sys.argv[6] or None
commit_author = sys.argv[7] or None
pusher = sys.argv[8] or None
con = sqlite3.connect(db_file) con = sqlite3.connect(db_file)
@@ -43,11 +56,27 @@ cur = con.execute(
app_id, app_id,
kind, kind,
status, status,
started_at started_at,
trigger_source,
triggered_by_user_id,
triggered_by_username,
triggered_by_display_name,
commit_author,
pusher
) )
VALUES (?, ?, ?, CURRENT_TIMESTAMP) VALUES (?, ?, ?, CURRENT_TIMESTAMP, ?, ?, ?, ?, ?, ?)
""", """,
(app_id, "app", "running"), (
app_id,
"app",
"running",
trigger_source,
triggered_by_user_id,
triggered_by_username,
triggered_by_display_name,
commit_author,
pusher,
),
) )
con.execute( con.execute(
@@ -70,6 +99,8 @@ TMP_DIR="$(mktemp -d)"
STDOUT_FILE="$TMP_DIR/stdout.log" STDOUT_FILE="$TMP_DIR/stdout.log"
STDERR_FILE="$TMP_DIR/stderr.log" STDERR_FILE="$TMP_DIR/stderr.log"
touch "$STDOUT_FILE" "$STDERR_FILE"
finish_deploy() { finish_deploy() {
local status="$1" local status="$1"
local returncode="$2" local returncode="$2"
@@ -120,8 +151,6 @@ con.execute(
con.commit() con.commit()
con.close() con.close()
PY PY
rm -rf "$TMP_DIR"
} }
run_step() { run_step() {
@@ -141,6 +170,7 @@ set +e
run_step "Updating source code" bash -c " run_step "Updating source code" bash -c "
cd '$APP_SOURCE_DIR' cd '$APP_SOURCE_DIR'
if [ -d '.git' ]; then if [ -d '.git' ]; then
git -c safe.directory='$APP_SOURCE_DIR' fetch origin main git -c safe.directory='$APP_SOURCE_DIR' fetch origin main
git -c safe.directory='$APP_SOURCE_DIR' reset --hard origin/main git -c safe.directory='$APP_SOURCE_DIR' reset --hard origin/main
@@ -150,7 +180,11 @@ fi
RESULT=$? RESULT=$?
if [ "$RESULT" -eq 0 ]; then if [ "$RESULT" -eq 0 ]; then
run_step "Building Docker image" docker build --pull --no-cache -t "$APP_ID:latest" "$APP_SOURCE_DIR" run_step "Building Docker image" \
docker build --pull --no-cache \
-t "$APP_ID:latest" \
"$APP_SOURCE_DIR"
RESULT=$? RESULT=$?
fi fi
@@ -161,14 +195,20 @@ if [ "$RESULT" -eq 0 ]; then
'$TOOLS_DIR/scripts/generate-caddyfile.sh' '$TOOLS_DIR/scripts/generate-caddyfile.sh'
'$TOOLS_DIR/scripts/generate-apps-page.sh' '$TOOLS_DIR/scripts/generate-apps-page.sh'
" "
RESULT=$? RESULT=$?
fi fi
if [ "$RESULT" -eq 0 ]; then if [ "$RESULT" -eq 0 ]; then
run_step "Starting app container" bash -c " run_step "Starting app container" bash -c "
cd '$APPFACTORY_DIR/deploy' cd '$APPFACTORY_DIR/deploy'
docker compose -f docker-compose.yml -f apps-compose.yml up -d --force-recreate '$APP_ID'
docker compose \
-f docker-compose.yml \
-f apps-compose.yml \
up -d --force-recreate '$APP_ID'
" "
RESULT=$? RESULT=$?
fi fi
@@ -176,18 +216,26 @@ set -e
if [ "$RESULT" -eq 0 ]; then if [ "$RESULT" -eq 0 ]; then
finish_deploy "success" 0 finish_deploy "success" 0
cat "$STDOUT_FILE" cat "$STDOUT_FILE"
cat "$STDERR_FILE" >&2 cat "$STDERR_FILE" >&2
echo "" echo ""
echo "DONE" echo "DONE"
echo "Open:" echo "Open:"
echo "$APPFACTORY_PUBLIC_URL/apps/$APP_ID/docs" echo "$APPFACTORY_PUBLIC_URL/apps/$APP_ID/docs"
else else
finish_deploy "failed" "$RESULT" finish_deploy "failed" "$RESULT"
cat "$STDOUT_FILE" cat "$STDOUT_FILE"
cat "$STDERR_FILE" >&2 cat "$STDERR_FILE" >&2
echo "" echo ""
echo "FAILED" echo "FAILED"
echo "Deployment ID: $DEPLOY_ID" echo "Deployment ID: $DEPLOY_ID"
rm -rf "$TMP_DIR"
exit "$RESULT" exit "$RESULT"
fi fi
rm -rf "$TMP_DIR"
+38 -9
View File
@@ -19,6 +19,13 @@ DB_FILE="/opt/appfactory/data/appfactory/appfactory.db"
SERVICE_DIR="$WORKSPACE_DIR/$SERVICE_NAME" SERVICE_DIR="$WORKSPACE_DIR/$SERVICE_NAME"
DEPLOY_DIR="$APPFACTORY_DIR/deploy" DEPLOY_DIR="$APPFACTORY_DIR/deploy"
TRIGGER_SOURCE="${APPFACTORY_TRIGGER_SOURCE:-cli}"
TRIGGERED_BY_USER_ID="${APPFACTORY_TRIGGERED_BY_USER_ID:-}"
TRIGGERED_BY_USERNAME="${APPFACTORY_TRIGGERED_BY_USERNAME:-}"
TRIGGERED_BY_DISPLAY_NAME="${APPFACTORY_TRIGGERED_BY_DISPLAY_NAME:-}"
COMMIT_AUTHOR="${APPFACTORY_COMMIT_AUTHOR:-}"
PUSHER="${APPFACTORY_PUSHER:-}"
if [ ! -d "$SERVICE_DIR" ]; then if [ ! -d "$SERVICE_DIR" ]; then
echo "Missing service directory: $SERVICE_DIR" echo "Missing service directory: $SERVICE_DIR"
exit 1 exit 1
@@ -29,12 +36,18 @@ if [ ! -f "$SERVICE_DIR/Dockerfile" ]; then
exit 1 exit 1
fi fi
DEPLOY_ID="$(python3 - "$DB_FILE" "$SERVICE_NAME" <<'PY' DEPLOY_ID="$(python3 - "$DB_FILE" "$SERVICE_NAME" "$TRIGGER_SOURCE" "$TRIGGERED_BY_USER_ID" "$TRIGGERED_BY_USERNAME" "$TRIGGERED_BY_DISPLAY_NAME" "$COMMIT_AUTHOR" "$PUSHER" <<'PY'
import sqlite3 import sqlite3
import sys import sys
db_file = sys.argv[1] db_file = sys.argv[1]
app_id = sys.argv[2] app_id = sys.argv[2]
trigger_source = sys.argv[3]
triggered_by_user_id = sys.argv[4] or None
triggered_by_username = sys.argv[5] or None
triggered_by_display_name = sys.argv[6] or None
commit_author = sys.argv[7] or None
pusher = sys.argv[8] or None
con = sqlite3.connect(db_file) con = sqlite3.connect(db_file)
@@ -44,17 +57,31 @@ cur = con.execute(
app_id, app_id,
kind, kind,
status, status,
started_at started_at,
trigger_source,
triggered_by_user_id,
triggered_by_username,
triggered_by_display_name,
commit_author,
pusher
) )
VALUES (?, ?, ?, CURRENT_TIMESTAMP) VALUES (?, ?, ?, CURRENT_TIMESTAMP, ?, ?, ?, ?, ?, ?)
""", """,
(app_id, "core", "running"), (
app_id,
"core",
"running",
trigger_source,
triggered_by_user_id,
triggered_by_username,
triggered_by_display_name,
commit_author,
pusher,
),
) )
con.commit() con.commit()
print(cur.lastrowid) print(cur.lastrowid)
con.close() con.close()
PY PY
)" )"
@@ -63,6 +90,8 @@ TMP_DIR="$(mktemp -d)"
STDOUT_FILE="$TMP_DIR/stdout.log" STDOUT_FILE="$TMP_DIR/stdout.log"
STDERR_FILE="$TMP_DIR/stderr.log" STDERR_FILE="$TMP_DIR/stderr.log"
touch "$STDOUT_FILE" "$STDERR_FILE"
finish_deploy() { finish_deploy() {
local status="$1" local status="$1"
local returncode="$2" local returncode="$2"
@@ -76,7 +105,6 @@ db_file = sys.argv[1]
deploy_id = sys.argv[2] deploy_id = sys.argv[2]
status = sys.argv[3] status = sys.argv[3]
returncode = int(sys.argv[4]) returncode = int(sys.argv[4])
stdout_file = Path(sys.argv[5]) stdout_file = Path(sys.argv[5])
stderr_file = Path(sys.argv[6]) stderr_file = Path(sys.argv[6])
@@ -101,8 +129,6 @@ con.execute(
con.commit() con.commit()
con.close() con.close()
PY PY
rm -rf "$TMP_DIR"
} }
run_step() { run_step() {
@@ -172,5 +198,8 @@ else
echo "FAILED" echo "FAILED"
echo "Deployment ID: $DEPLOY_ID" echo "Deployment ID: $DEPLOY_ID"
rm -rf "$TMP_DIR"
exit "$RESULT" exit "$RESULT"
fi fi
rm -rf "$TMP_DIR"