From 21e7c40c99f7aad7917472048bf1445501b6b1ed Mon Sep 17 00:00:00 2001 From: Michael Schiemer Date: Sat, 8 Nov 2025 17:14:19 +0100 Subject: [PATCH] feat: Add Ansible playbooks to fix Gitea runner configuration - Add fix-gitea-runner-config.yml to diagnose runner configuration issues - Add register-gitea-runner.yml to re-register runner with correct Gitea URL - Check for GitHub URLs in runner configuration (should only use git.michaelschiemer.de) - Verify .env file has correct GITEA_INSTANCE_URL - Fixes 504 timeouts caused by runner trying to connect to GitHub fallback --- .../playbooks/fix-gitea-runner-config.yml | 200 ++++++++++++++++++ .../playbooks/register-gitea-runner.yml | 142 +++++++++++++ 2 files changed, 342 insertions(+) create mode 100644 deployment/ansible/playbooks/fix-gitea-runner-config.yml create mode 100644 deployment/ansible/playbooks/register-gitea-runner.yml diff --git a/deployment/ansible/playbooks/fix-gitea-runner-config.yml b/deployment/ansible/playbooks/fix-gitea-runner-config.yml new file mode 100644 index 00000000..845ba92d --- /dev/null +++ b/deployment/ansible/playbooks/fix-gitea-runner-config.yml @@ -0,0 +1,200 @@ +--- +- name: Fix Gitea Runner Configuration + hosts: production + gather_facts: yes + become: no + + vars: + gitea_runner_path: "{{ runner_path | default('/home/deploy/deployment/gitea-runner') }}" + gitea_instance_url: "https://git.michaelschiemer.de" + runner_container_name: "gitea-runner" + + tasks: + - name: Check if Gitea runner directory exists + stat: + path: "{{ gitea_runner_path }}" + register: runner_dir_exists + + - name: Fail if runner directory does not exist + fail: + msg: "Gitea runner directory not found at {{ gitea_runner_path }}" + when: not runner_dir_exists.stat.exists + + - name: Check if runner container is running + shell: | + docker ps --format json | jq -r 'select(.Names == "{{ runner_container_name }}") | .State' + register: runner_container_state + changed_when: false + failed_when: false + + - name: Display runner container status + debug: + msg: | + Runner Container Status: {{ runner_container_state.stdout | default('NOT RUNNING') }} + + - name: Check if .runner file exists + stat: + path: "{{ gitea_runner_path }}/data/.runner" + register: runner_file_exists + + - name: Read .runner file content (if exists) + slurp: + src: "{{ gitea_runner_path }}/data/.runner" + register: runner_file_content + when: runner_file_exists.stat.exists + changed_when: false + + - name: Display .runner file content + debug: + msg: | + Runner Registration File Content: + {{ runner_file_content.content | b64decode | default('File not found') }} + when: runner_file_exists.stat.exists + + - name: Check for GitHub URLs in .runner file + shell: | + grep -i "github.com" "{{ gitea_runner_path }}/data/.runner" 2>/dev/null || echo "NO_GITHUB_URLS" + register: github_urls_check + when: runner_file_exists.stat.exists + changed_when: false + failed_when: false + + - name: Display GitHub URLs check result + debug: + msg: | + GitHub URLs in .runner file: {{ github_urls_check.stdout }} + + - name: Check runner logs for incorrect URLs + shell: | + docker logs {{ runner_container_name }} --tail=100 2>&1 | grep -E "(github.com|instance|repo)" || echo "NO_MATCHES" + register: runner_logs_check + changed_when: false + failed_when: false + + - name: Display runner logs analysis + debug: + msg: | + Runner Logs Analysis (last 100 lines): + {{ runner_logs_check.stdout }} + + - name: Check .env file for GITEA_INSTANCE_URL + shell: | + grep "^GITEA_INSTANCE_URL=" "{{ gitea_runner_path }}/.env" 2>/dev/null || echo "NOT_FOUND" + register: env_instance_url + changed_when: false + failed_when: false + + - name: Display GITEA_INSTANCE_URL from .env + debug: + msg: | + GITEA_INSTANCE_URL in .env: {{ env_instance_url.stdout }} + + - name: Check if .env has correct Gitea URL + set_fact: + env_has_correct_url: "{{ env_instance_url.stdout is defined and gitea_instance_url in env_instance_url.stdout }}" + + - name: Check if runner needs re-registration + set_fact: + runner_needs_reregistration: >- + {%- if not runner_file_exists.stat.exists -%} + true + {%- elif 'github.com' in (github_urls_check.stdout | default('')) -%} + true + {%- elif not env_has_correct_url -%} + true + {%- else -%} + false + {%- endif -%} + + - name: Display re-registration decision + debug: + msg: | + Runner Re-registration Needed: {{ runner_needs_reregistration | bool }} + + Reasons: + - Runner file exists: {{ runner_file_exists.stat.exists }} + - Contains GitHub URLs: {{ 'github.com' in (github_urls_check.stdout | default('')) }} + - .env has correct URL: {{ env_has_correct_url | bool }} + + - name: Stop runner container before re-registration + shell: | + cd {{ gitea_runner_path }} + docker compose stop {{ runner_container_name }} + when: runner_needs_reregistration | bool + register: stop_runner + changed_when: stop_runner.rc == 0 + + - name: Backup existing .runner file + copy: + src: "{{ gitea_runner_path }}/data/.runner" + dest: "{{ gitea_runner_path }}/data/.runner.backup.{{ ansible_date_time.epoch }}" + remote_src: yes + when: + - runner_file_exists.stat.exists + - runner_needs_reregistration | bool + + - name: Remove existing .runner file + file: + path: "{{ gitea_runner_path }}/data/.runner" + state: absent + when: runner_needs_reregistration | bool + + - name: Update .env file with correct GITEA_INSTANCE_URL + lineinfile: + path: "{{ gitea_runner_path }}/.env" + regexp: '^GITEA_INSTANCE_URL=' + line: "GITEA_INSTANCE_URL={{ gitea_instance_url }}" + create: yes + when: runner_needs_reregistration | bool + register: env_updated + + - name: Display instructions for manual re-registration + debug: + msg: | + ======================================== + Runner Re-registration Required + ======================================== + + The runner needs to be re-registered with the correct Gitea instance URL. + + Steps to re-register: + + 1. Get a new registration token from Gitea: + {{ gitea_instance_url }}/admin/actions/runners + Click "Create New Runner" and copy the token + + 2. Update .env file with the token: + GITEA_RUNNER_REGISTRATION_TOKEN= + + 3. Re-register the runner: + cd {{ gitea_runner_path }} + ./register.sh + + Or use Ansible to set the token and register: + ansible-playbook -i inventory/production.yml \ + playbooks/register-gitea-runner.yml \ + -e "runner_registration_token=" + + ======================================== + when: runner_needs_reregistration | bool + + - name: Display final status + debug: + msg: | + ======================================== + Gitea Runner Configuration Status + ======================================== + Runner Directory: {{ gitea_runner_path }} + Container Running: {{ 'YES' if runner_container_state.stdout == 'running' else 'NO' }} + Runner File Exists: {{ 'YES' if runner_file_exists.stat.exists else 'NO' }} + Contains GitHub URLs: {{ 'YES' if 'github.com' in (github_urls_check.stdout | default('')) else 'NO' }} + .env has correct URL: {{ 'YES' if env_has_correct_url else 'NO' }} + Re-registration Needed: {{ 'YES' if runner_needs_reregistration | bool else 'NO' }} + ======================================== + + {% if not runner_needs_reregistration | bool %} + ✅ Runner configuration looks correct! + {% else %} + ⚠️ Runner needs to be re-registered with correct Gitea URL + {% endif %} + diff --git a/deployment/ansible/playbooks/register-gitea-runner.yml b/deployment/ansible/playbooks/register-gitea-runner.yml new file mode 100644 index 00000000..e2eb1cb6 --- /dev/null +++ b/deployment/ansible/playbooks/register-gitea-runner.yml @@ -0,0 +1,142 @@ +--- +- name: Register Gitea Runner with Correct Instance URL + hosts: production + gather_facts: yes + become: no + + vars: + gitea_runner_path: "{{ runner_path | default('/home/deploy/deployment/gitea-runner') }}" + gitea_instance_url: "https://git.michaelschiemer.de" + runner_registration_token: "{{ runner_registration_token | default('') }}" + runner_name: "{{ runner_name | default('dev-runner-01') }}" + runner_labels: "{{ runner_labels | default('ubuntu-latest:docker://node:16-bullseye,ubuntu-22.04:docker://node:16-bullseye,php-ci:docker://php-ci:latest') }}" + runner_container_name: "gitea-runner" + + tasks: + - name: Fail if registration token is not provided + fail: + msg: "runner_registration_token must be provided via -e 'runner_registration_token='" + when: runner_registration_token | string | trim == '' + + - name: Check if Gitea runner directory exists + stat: + path: "{{ gitea_runner_path }}" + register: runner_dir_exists + + - name: Fail if runner directory does not exist + fail: + msg: "Gitea runner directory not found at {{ gitea_runner_path }}" + when: not runner_dir_exists.stat.exists + + - name: Stop runner container if running + shell: | + cd {{ gitea_runner_path }} + docker compose stop {{ runner_container_name }} + register: stop_result + changed_when: stop_result.rc == 0 + failed_when: false + + - name: Backup existing .runner file + copy: + src: "{{ gitea_runner_path }}/data/.runner" + dest: "{{ gitea_runner_path }}/data/.runner.backup.{{ ansible_date_time.epoch }}" + remote_src: yes + when: runner_file_exists.stat.exists + register: runner_file_exists + ignore_errors: yes + + - name: Check if .runner file exists + stat: + path: "{{ gitea_runner_path }}/data/.runner" + register: runner_file_exists + + - name: Remove existing .runner file + file: + path: "{{ gitea_runner_path }}/data/.runner" + state: absent + when: runner_file_exists.stat.exists + + - name: Update .env file with correct configuration + lineinfile: + path: "{{ gitea_runner_path }}/.env" + regexp: '^{{ item.key }}=' + line: "{{ item.key }}={{ item.value }}" + create: yes + loop: + - { key: 'GITEA_INSTANCE_URL', value: '{{ gitea_instance_url }}' } + - { key: 'GITEA_RUNNER_REGISTRATION_TOKEN', value: '{{ runner_registration_token }}' } + - { key: 'GITEA_RUNNER_NAME', value: '{{ runner_name }}' } + - { key: 'GITEA_RUNNER_LABELS', value: '{{ runner_labels }}' } + no_log: true + + - name: Start runner services + shell: | + cd {{ gitea_runner_path }} + docker compose up -d + register: start_services + changed_when: start_services.rc == 0 + + - name: Wait for services to be ready + pause: + seconds: 5 + + - name: Register runner with correct Gitea instance + shell: | + cd {{ gitea_runner_path }} + docker compose exec -T {{ runner_container_name }} act_runner register \ + --instance "{{ gitea_instance_url }}" \ + --token "{{ runner_registration_token }}" \ + --name "{{ runner_name }}" \ + --labels "{{ runner_labels }}" + register: register_result + no_log: true + changed_when: register_result.rc == 0 + + - name: Display registration result + debug: + msg: | + Runner Registration Result: + {{ register_result.stdout | default('No output') }} + when: register_result.rc == 0 + + - name: Verify .runner file was created + stat: + path: "{{ gitea_runner_path }}/data/.runner" + register: runner_file_created + + - name: Check .runner file for correct instance URL + shell: | + grep -i "{{ gitea_instance_url }}" "{{ gitea_runner_path }}/data/.runner" 2>/dev/null || echo "URL_NOT_FOUND" + register: runner_url_check + when: runner_file_created.stat.exists + changed_when: false + + - name: Check .runner file for GitHub URLs + shell: | + grep -i "github.com" "{{ gitea_runner_path }}/data/.runner" 2>/dev/null || echo "NO_GITHUB_URLS" + register: runner_github_check + when: runner_file_created.stat.exists + changed_when: false + + - name: Display final status + debug: + msg: | + ======================================== + Gitea Runner Registration Status + ======================================== + Registration: {{ '✅ SUCCESS' if register_result.rc == 0 else '❌ FAILED' }} + Runner File Created: {{ '✅ YES' if runner_file_created.stat.exists else '❌ NO' }} + Contains Correct URL: {{ '✅ YES' if 'URL_NOT_FOUND' not in runner_url_check.stdout else '❌ NO' }} + Contains GitHub URLs: {{ '❌ YES' if 'NO_GITHUB_URLS' not in runner_github_check.stdout else '✅ NO' }} + ======================================== + + {% if register_result.rc == 0 and runner_file_created.stat.exists %} + ✅ Runner registered successfully with {{ gitea_instance_url }}! + + Check runner status: + {{ gitea_instance_url }}/admin/actions/runners + {% else %} + ❌ Registration failed. Check logs: + docker logs {{ runner_container_name }} + {% endif %} +