Files
hla_framework_bd/.gitea/workflows/deploy-build-restart-service.yml
T
Benjamin Labrecque 3728d76f11
Deploy bin / deploy (push) Successful in 3s
Deploy, build and restart a service / deploy (push) Failing after 3s
Deploy agebd python package / deploy (push) Successful in 2s
chore: deploy script for all services
2026-07-06 16:33:55 +02:00

84 lines
3.0 KiB
YAML

name: Deploy, build and restart a service
on:
push:
# TODO: if someone tags 'prod' to a (broken) feature branch
# this still triggers (and deploys to prod).
branches:
- main
tags:
- 'prod'
- 'v*'
jobs:
deploy:
runs-on: hla-dev
env:
# commit tagged ? yes->prod : no->dev
AGEBD_ENV: ${{ github.ref_type == 'tag' && 'prod' || 'dev' }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetches all history for all branches and tags -- needed for diff
# - name: Copy files and replace {{ agebd_env }} with dev or prod
# run: |
# mkdir -p ${SVC_CURRRENT_DIR}
# cp -r 000-master/* ${SVC_CURRRENT_DIR}
# sed -i 's/{{ agebd_env }}/${AGEBD_ENV}/g' ${SVC_CURRRENT_DIR}/systemd/AGEBD-SERVICE-MASTER.service
# - name: Create Python venv
# run: |
# cd ${SVC_CURRRENT_DIR}/app
# uv venv --allow-existing --system-site-packages .venv
# uv sync
# - name: Restart Service
# run: |
# export XDG_RUNTIME_DIR=/run/user/$(id -u)
# systemctl --user daemon-reload
# systemctl --user enable --force ${SVC_CURRRENT_DIR}/systemd/AGEBD-SERVICE-MASTER.service
# systemctl --user restart AGEBD-SERVICE-MASTER.service
- name: Detect and Deploy Changed Services
run: |
set -x
# 1. Identify which directories under 'services/' changed in this push
CHANGED_DIRS=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep "^services/" | awk -F/ '{print $2}' | sort -u)
if [ -z "$CHANGED_DIRS" ]; then
echo "No service files changed. Skipping deployment."
exit 0
fi
# 2. Set up environment
export XDG_RUNTIME_DIR=/run/user/$(id -u)
# 3. Loop and deploy ONLY the changed services
for SERVICE in $CHANGED_DIRS; do
echo "Deploying detected changes for: $SERVICE"
SVC_CURRENT_DIR="/sls/bd/hla/${AGEBD_ENV}/services/${SERVICE}/current"
# Run your standard deployment blocks dynamically targeting $SERVICE
mkdir -p ${SVC_CURRENT_DIR}
cp -r services/${SERVICE}/* ${SVC_CURRENT_DIR}
# Build python venv
cd ${SVC_CURRENT_DIR}/app
uv venv --allow-existing --system-site-packages .venv
uv sync
# Uppercase service name for systemd conversion, skip first 4 chars, then upper (000-master -> MASTER)
SVC_UPPER=$(echo "${SERVICE:4}" | tr '[:lower:]' '[:upper:]')
sed -i "s/{{ agebd_env }}/${AGEBD_ENV}/g" ${SVC_CURRENT_DIR}/systemd/AGEBD-SERVICE-${SVC_UPPER}.service
systemctl --user daemon-reload
systemctl --user enable --force ${SVC_CURRENT_DIR}/systemd/AGEBD-SERVICE-${SVC_UPPER}.service
systemctl --user restart AGEBD-SERVICE-${SVC_UPPER}.service
done