--- - 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 %}