diff --git a/.gitea/workflows/deploy-build-restart-service.yml b/.gitea/workflows/deploy-build-restart-service.yml index 77f210d..fcf3e88 100644 --- a/.gitea/workflows/deploy-build-restart-service.yml +++ b/.gitea/workflows/deploy-build-restart-service.yml @@ -1,4 +1,4 @@ -name: Deploy, build and restart a service +name: Deploy, build and restart MASTER on: push: @@ -9,20 +9,26 @@ on: tags: - 'prod' - 'v*' + # paths: + # - 'services/000-master/**' jobs: deploy: runs-on: hla-dev + defaults: + run: + working-directory: ./ansible 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 # Fetches all history for all branches and tags -- needed for diff + fetch-depth: 0 # needed for git diff tracking # - name: Copy files and replace {{ agebd_env }} with dev or prod # run: | @@ -43,42 +49,13 @@ jobs: # 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 + - name: Run Ansible Orchestrator 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) + # 1. Parse changed directory names into space-separated string (e.g. "000-master 001-secondary") + CHANGED_SVC=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep "^services/" | awk -F/ '{print $2}' | sort -u | xargs) - 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 \ No newline at end of file + # 2. Figure out the destination target environment + ENV_TARGET="${{ github.ref_type == 'tag' && 'prod' || 'dev' }}" + + # 3. Fire the playbook passing variables down + ansible-playbook deploy-service.yml -e "changed_services_raw='$CHANGED_SVC' agebd_env='$ENV_TARGET'" \ No newline at end of file diff --git a/ansible/deploy-service.yml b/ansible/deploy-service.yml new file mode 100644 index 0000000..6a89a59 --- /dev/null +++ b/ansible/deploy-service.yml @@ -0,0 +1,21 @@ +- name: Dynamic HLA Microservices Deployer + hosts: hla-dev + gather_facts: yes + + vars: + # Passed from the Gitea workflow, e.g., "000-master 001-secondary" + changed_services_raw: "" + agebd_env: "dev" + + tasks: + # 1. Fail early if no services were modified + - name: Check if there are services to deploy + ansible.builtin.meta: end_play + when: changed_services_raw | trim == "" + + # 2. Main Loop: Loop through the list of modified service names + - name: Deploy Modified Services + include_tasks: tasks/deploy_single_service.yml + loop: "{{ changed_services_raw.split() }}" + loop_control: + loop_var: service_dir_name \ No newline at end of file diff --git a/ansible/plays/service-deploy.yml b/ansible/plays/service-deploy.yml index 706740b..5725e3e 100644 --- a/ansible/plays/service-deploy.yml +++ b/ansible/plays/service-deploy.yml @@ -1,45 +1,35 @@ -# - name: Deploy Service -# hosts: localhost -# gather_facts: yes # Required to get the user UID dynamically -# -# vars: -# - workspace_dir: "/tmp/hla_framework_bd" -# - svc_current_dir: /sls/bd/hla/dev/services/000-master/current -# - app_dir: "{{ svc_current_dir }}/app" -# -# tasks: -# - name: Clone this repository -# ansible.builtin.git: -# repo: "git@gitea.psi.ch:sls/hla_framework_bd.git" -# dest: "{{ workspace_dir }}" -# version: main -# accept_hostkey: true -# -# - name: Sync service deployment files -# ansible.builtin.copy: -# src: ./services/000-master/ -# dest: "{{ svc_current_dir }}/" -# mode: 'preserve' -# -# - name: Initialize and sync virtual environment with uv -# ansible.builtin.command: -# cmd: uv venv --allow-existing --system-site-packages .venv -# chdir: "{{ app_dir }}" -# changed_when: false -# -# - name: Sync dependencies with uv -# ansible.builtin.command: -# cmd: uv sync -# chdir: "{{ app_dir }}" -# changed_when: false -# -# - name: Manage systemd user service -# ansible.builtin.systemd: -# name: "{{ svc_current_dir }}/systemd/AGEBD-SERVICE-MASTER.service" -# state: restarted -# enabled: yes -# force: yes -# scope: user -# daemon_reload: yes -# environment: -# XDG_RUNTIME_DIR: "/run/user/{{ ansible_effective_user_id }}" \ No newline at end of file +- name: "[{{ service_dir_name }}] Define service specific paths and names" + ansible.builtin.set_fact: + # 1. The literal directory name on disk (e.g., "000-master") + svc_current_dir: "/sls/bd/hla/{{ agebd_env }}/services/{{ service_dir_name }}/current" + # 2. Extract logical service name by dropping the first 4 characters and converting to UPPERCASE (e.g., "MASTER") + service_unit_name: "AGEBD-SERVICE-{{ service_dir_name[4:] | upper }}.service" + +- name: "[{{ service_dir_name }}] Ensure target directory structure exists" + ansible.builtin.file: + path: "{{ svc_current_dir }}" + state: directory + mode: '0755' + +- name: "[{{ service_dir_name }}] Sync service deployment files" + ansible.builtin.copy: + src: "./services/{{ service_dir_name }}/" + dest: "{{ svc_current_dir }}/" + mode: 'preserve' + +- name: "[{{ service_dir_name }}] Substitute environment variable inline" + ansible.builtin.replace: + path: "{{ svc_current_dir }}/systemd/{{ service_unit_name }}" + regexp: '\{\{\s*agebd_env\s*\}\}' + replace: "{{ agebd_env }}" + +- name: "[{{ service_dir_name }}] Reload, enable, and restart systemd user space" + ansible.builtin.systemd: + name: "{{ svc_current_dir }}/systemd/{{ service_unit_name }}" + state: restarted + enabled: yes + force: yes + scope: user + daemon_reload: yes + environment: + XDG_RUNTIME_DIR: "/run/user/{{ ansible_effective_user_id }}" \ No newline at end of file