---
- name: Create Forgejo directories
  ansible.builtin.file:
    path: "{{ item }}"
    state: directory
    mode: "0755"
  loop:
    - /opt/forgejo
    - /opt/forgejo/data
    - /opt/forgejo/runner

- name: Deploy Forgejo Docker Compose file
  ansible.builtin.template:
    src: docker-compose.forgejo.yml.j2
    dest: /opt/forgejo/docker-compose.yml
    mode: "0644"

- name: Start Forgejo services
  ansible.builtin.shell: docker compose up -d
  args:
    chdir: /opt/forgejo

- name: Wait for Forgejo to be ready
  ansible.builtin.uri:
    url: "http://localhost:{{ forgejo_port }}"
    status_code: 200
  register: forgejo_health
  retries: 15
  delay: 5
  until: forgejo_health.status == 200
