--- - name: Check and Restart Gitea if Unhealthy hosts: production gather_facts: yes become: no vars: gitea_stack_path: "{{ stacks_base_path | default('/home/deploy/deployment/stacks') }}/gitea" gitea_url: "https://git.michaelschiemer.de" gitea_container_name: "gitea" tasks: - name: Check if Gitea stack directory exists stat: path: "{{ gitea_stack_path }}" register: gitea_stack_exists - name: Fail if Gitea stack directory does not exist fail: msg: "Gitea stack directory not found at {{ gitea_stack_path }}" when: not gitea_stack_exists.stat.exists - name: Check Gitea container status shell: | cd {{ gitea_stack_path }} docker compose ps {{ gitea_container_name }} --format json register: gitea_container_status changed_when: false failed_when: false - name: Display Gitea container status debug: msg: | Gitea Container Status: {{ gitea_container_status.stdout | default('Container not found or error') }} - name: Check Gitea health endpoint uri: url: "{{ gitea_url }}/api/healthz" method: GET status_code: [200] validate_certs: false timeout: 10 register: gitea_health ignore_errors: yes changed_when: false - name: Display Gitea health check result debug: msg: | Gitea Health Check: - Status Code: {{ gitea_health.status | default('UNREACHABLE') }} - Response Time: {{ gitea_health.elapsed | default('N/A') }}s {% if gitea_health.status == 200 %} - Status: ✅ HEALTHY {% else %} - Status: ❌ UNHEALTHY or TIMEOUT {% endif %} - name: Get Gitea container logs (last 50 lines) shell: | cd {{ gitea_stack_path }} docker compose logs --tail=50 {{ gitea_container_name }} 2>&1 || echo "LOGS_NOT_AVAILABLE" register: gitea_logs changed_when: false failed_when: false - name: Display Gitea container logs debug: msg: | Gitea Container Logs (last 50 lines): {{ gitea_logs.stdout | default('No logs available') }} - name: Check if Gitea container is running set_fact: gitea_is_running: "{{ 'State":"running' in gitea_container_status.stdout | default('') }}" - name: Check if Gitea is healthy set_fact: gitea_is_healthy: "{{ gitea_health.status | default(0) == 200 }}" - name: Restart Gitea container if unhealthy or not running shell: | cd {{ gitea_stack_path }} docker compose restart {{ gitea_container_name }} when: not gitea_is_healthy or not gitea_is_running register: gitea_restart changed_when: gitea_restart.rc == 0 - name: Wait for Gitea to be ready after restart uri: url: "{{ gitea_url }}/api/healthz" method: GET status_code: [200] validate_certs: false timeout: 10 register: gitea_health_after_restart until: gitea_health_after_restart.status == 200 retries: 30 delay: 2 when: not gitea_is_healthy or not gitea_is_running ignore_errors: yes - name: Display final status debug: msg: | ======================================== Gitea Status Summary ======================================== Container Running: {{ '✅ YES' if gitea_is_running else '❌ NO' }} Health Check: {{ '✅ HEALTHY' if gitea_is_healthy else '❌ UNHEALTHY' }} {% if not gitea_is_healthy or not gitea_is_running %} Action Taken: 🔄 Container restarted Final Status: {{ '✅ HEALTHY' if gitea_health_after_restart.status | default(0) == 200 else '❌ STILL UNHEALTHY' }} {% else %} Action Taken: ℹ️ No action needed {% endif %} ======================================== {% if gitea_health_after_restart.status | default(0) == 200 %} ✅ Gitea is now accessible and healthy! {% elif not gitea_is_healthy and not gitea_is_running %} ⚠️ Gitea container was restarted but may still be starting up. Please check logs manually: docker compose -f {{ gitea_stack_path }}/docker-compose.yml logs gitea {% endif %}