From c5467dfa4773431b7c2cc1d1c86c2e691c24c333 Mon Sep 17 00:00:00 2001 From: Benjamin Labrecque Date: Tue, 14 Jul 2026 12:27:24 +0200 Subject: [PATCH] cicd: add deploying and restarting services on change --- .../workflows/deploy-and-restart-service.yml | 53 ++++++++++++++ .../deploy-build-restart-service.yml | 73 ------------------- bin/start_service.sh | 1 + 3 files changed, 54 insertions(+), 73 deletions(-) create mode 100644 .gitea/workflows/deploy-and-restart-service.yml delete mode 100644 .gitea/workflows/deploy-build-restart-service.yml diff --git a/.gitea/workflows/deploy-and-restart-service.yml b/.gitea/workflows/deploy-and-restart-service.yml new file mode 100644 index 0000000..2542438 --- /dev/null +++ b/.gitea/workflows/deploy-and-restart-service.yml @@ -0,0 +1,53 @@ +name: Deploy and restart modified services +# TODO: take care of .venv building when required (i.e when uv.lock changed) + +on: + push: + # TODO: if someone tags 'prod' to a (broken) feature branch + # this still triggers (and deploys to prod). + branches: + - main + tags: + - 'prod' + - 'v*' + paths: + - 'services/**' + +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 # needed for git diff tracking + + # TODO: (when) do we need to restart the master service or reinstall/restart the master ioc? + + - name: Run Ansible Orchestrator + run: | + set -x + + # Disable pipefail temporarily or add an '|| true' fallback so grep doesn't kill the script + set +e + CHANGED_FILES=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }}) + set -e + + # 1. Parse changed directory names into space-separated string (e.g. "master other-service") + CHANGED_SVC=$(echo "$CHANGED_FILES" | awk -F/ '$1=="services" {print $2}' | sort -u | xargs) + + echo "Detected changed services: '$CHANGED_SVC'" + + # Safely halt the execution if there is nothing to pass to Ansible + if [ -z "$CHANGED_SVC" ]; then + echo "No services changed in this commit range. Skipping deployment smoothly." + exit 0 + fi + + # 3. Fire the playbook passing variables down + ansible-playbook ansible/deploy-and-restart-modified-services.yml -e "changed_services_raw='$CHANGED_SVC' agebd_env='$AGEBD_ENV'" diff --git a/.gitea/workflows/deploy-build-restart-service.yml b/.gitea/workflows/deploy-build-restart-service.yml deleted file mode 100644 index 6688520..0000000 --- a/.gitea/workflows/deploy-build-restart-service.yml +++ /dev/null @@ -1,73 +0,0 @@ -# name: Deploy, build and restart MASTER - -# on: -# push: -# # TODO: if someone tags 'prod' to a (broken) feature branch -# # this still triggers (and deploys to prod). -# branches: -# - main -# tags: -# - 'prod' -# - 'v*' -# # paths: -# # - 'services/**' - -# jobs: -# deploy: -# runs-on: hla-dev - -# env: -# # commit tagged ? yes->prod : no->dev -# AGEBD_ENV: ${{ github.ref_type == 'tag' && 'prod' || 'dev' }} -# SVC_CURRRENT_DIR: /sls/bd/hla/${{ github.ref_type == 'tag' && 'prod' || 'dev' }}/services/000-master/current - -# steps: -# - name: Checkout repository -# uses: actions/checkout@v4 -# with: -# fetch-depth: 0 # needed for git diff tracking - - # - 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: Run Ansible Orchestrator - # run: | - # set -x - - # # Disable pipefail temporarily or add an '|| true' fallback so grep doesn't kill the script - # set +e - # CHANGED_FILES=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }}) - # set -e - - # # 1. Parse changed directory names into space-separated string (e.g. "000-master 001-secondary") - # CHANGED_SVC=$(echo "$CHANGED_FILES" | awk -F/ '$1=="services" {print $2}' | sort -u | xargs) - - # echo "Detected changed services: '$CHANGED_SVC'" - - # # Safely halt the execution if there is nothing to pass to Ansible - # if [ -z "$CHANGED_SVC" ]; then - # echo "No services changed in this commit range. Skipping deployment smoothly." - # exit 0 - # fi - - # # 2. Figure out the destination target environment - # ENV_TARGET="${{ github.ref_type == 'tag' && 'prod' || 'dev' }}" - - # # 3. Fire the playbook passing variables down - # ansible-playbook ansible/deploy-service.yml -e "changed_services_raw='$CHANGED_SVC' agebd_env='$ENV_TARGET'" \ No newline at end of file diff --git a/bin/start_service.sh b/bin/start_service.sh index 1723242..c5c2faa 100755 --- a/bin/start_service.sh +++ b/bin/start_service.sh @@ -20,6 +20,7 @@ export EPICS_CA_ADDR_LIST='sls-cagw.psi.ch:5062 129.129.146.88' # activate conda environment . /opt/gfa/python-3.10/latest/bin/activate +# TODO: building the .venv should be done in CICD (only when uv.lock changes) pip install uv # to run `uv ...` PATH="${HOME}/.local/bin:${PATH}"