Compare commits

..

2 Commits

Author SHA1 Message Date
97b0dde75b feat: Add Ansible playbook to check and restart Gitea
Some checks failed
🚀 Build & Deploy Image / Determine Build Necessity (push) Successful in 30s
Security Vulnerability Scan / Check for Dependency Changes (push) Successful in 35s
🚀 Build & Deploy Image / Build Runtime Base Image (push) Successful in 15s
🚀 Build & Deploy Image / Run Tests & Quality Checks (push) Has been skipped
Security Vulnerability Scan / Composer Security Audit (push) Has been skipped
🚀 Build & Deploy Image / Build Docker Image (push) Successful in 14s
🚀 Build & Deploy Image / Auto-deploy to Staging (push) Failing after 1m11s
🚀 Build & Deploy Image / Auto-deploy to Production (push) Has been skipped
- Check Gitea container status
- Check Gitea health endpoint
- Display container logs
- Restart container if unhealthy or not running
- Wait for Gitea to be ready after restart
- Display comprehensive status summary
- Helps diagnose and fix 504 Gateway Timeout issues
2025-11-08 17:03:22 +01:00
891c73d0af fix: Wait for Docker socket and login to registry before compose up
- Change Docker daemon wait from TCP port 2375 to Unix socket /var/run/docker.sock
- Add Docker registry login task before docker compose up
- Ensures authentication is available when pulling images
- Fixes 'no basic auth credentials' error during image pull
2025-11-08 16:53:22 +01:00
2 changed files with 138 additions and 2 deletions

View File

@@ -0,0 +1,126 @@
---
- 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 %}

View File

@@ -225,13 +225,23 @@
- name: Wait for Docker daemon to be ready
wait_for:
port: 2375
host: localhost
path: /var/run/docker.sock
timeout: 10
when: docker_daemon_updated.changed | default(false)
ignore_errors: yes
become: no
- name: Login to Docker registry before compose up
community.docker.docker_login:
registry_url: "{{ docker_registry }}"
username: "{{ docker_registry_username | default('admin') }}"
password: "{{ registry_password }}"
when:
- registry_password | string | trim != ''
- registry_accessible == 'true'
no_log: yes
ignore_errors: yes
- name: Deploy application stack with new image
shell: |
cd {{ application_code_dest }}