---
- name: Install act runner for gitea
  hosts: ulna

  tasks:
  - name: Download the binary
    become: true
    ansible.builtin.get_url:
      url: https://gitea.com/gitea/act_runner/releases/download/v0.2.0/act_runner-0.2.0-linux-arm64
      dest: /usr/local/bin/act_runner
      mode: '755'

  - name: Create the runner user
    become: true
    ansible.builtin.user:
      name: runner
      group: docker
      comment: Gitea runner
      shell: /bin/bash
      password: '!'
      home: /home/runner
      create_home: true

  - name: Register the runner
    ansible.builtin.command: act_runner register --no-interactive --instance https://{{ gitea_host }} --token {{ act_runner_token|quote }}
    become: true
    become_user: runner
    args:
      chdir: /home/runner
    ignore_errors: true

  - name: Service file
    become: true
    ansible.builtin.template:
      src: templates/gitea/runner.service.j2
      dest: /etc/systemd/system/runner.service

  - name: Make sure docker is running
    ansible.builtin.systemd:
      state: started
      name: docker

  - name: Re(start) the systemd service
    become: true
    ansible.builtin.systemd:
      state: restarted
      name: runner
      enabled: true
      daemon_reload: true
