added a bunch of features, but runner doesn't start if docker not present ??

This commit is contained in:
Bertrand Lanson 2023-08-12 16:01:42 +02:00
parent f8cfff489c
commit 86a444c110
17 changed files with 271 additions and 49 deletions

View File

@ -6,21 +6,91 @@ This role installs and configure a gitea act runner on **debian-based** distribu
Requirements Requirements
------------ ------------
None. If `deploy_gitea_runner_deploy_method` is set to `docker`, this role assumes you have `docker`, `docker-compose` installed on the target hosts. The role will not install these components, but you can install them using the [install_docker](https://github.com/ednxzu/install_docker) role.
Role Variables Role Variables
-------------- --------------
Available variables are listed below, along with default values. A sample file for the default values is available in `default/deploy_gitea_runner.yml.sample` in case you need it for any `group_vars` or `host_vars` configuration. Available variables are listed below, along with default values. A sample file for the default values is available in `default/deploy_gitea_runner.yml.sample` in case you need it for any `group_vars` or `host_vars` configuration.
```yaml ```yaml
your_defaults_here: default_value # by default, set to default_value deploy_gitea_runner_version: latest # by default, set to latest
``` ```
A quick description of the variable, what it does, and how to use it. This variable defines the version that will be deployed to your host. In case you use `deploy_gitea_runner_deploy_method: host`, this has to match a release version on [gitea act runner repository](https://gitea.com/gitea/act_runner/releases). If using `deploy_gitea_runner_deploy_method: docker`, this has to match a tag on the [gitea act runner docker registry](https://hub.docker.com/r/gitea/act_runner/tags)
```yaml
deploy_gitea_runner_deploy_method: host # by default, set to host
```
This variable defines whether the gitea-runner should be deployed as a binary on the host, or as a docker container. This defaults to `host` but can also be `docker`.
```yaml
deploy_gitea_runner_directory: /opt/gitea-actions # by default, set to /opt/gitea-actions
```
This variable defines where to store the files for the gitea-runner (config, potential docker-compose, etc...)
```yaml
deploy_gitea_runner_timezone: "Europe/Paris" # by default, set to Europe/Paris
```
This variable is only used for if `deploy_gitea_runner_deploy_method` is `docker`, to set the timezone inside the container.
```yaml
deploy_gitea_runner_register: false # by default, set to false
```
This variable sets whether or not the role will register the runner against your gitea instance. It will only register if it cannot find the `.runner` file that is generated when registering, and if `deploy_gitea_runner_server_token` is not empty. If `deploy_gitea_runner_deploy_method` is `docker`, this has no impact, since the registration will be handle automatically when to container starts up, given that you have provided a valid URL and token (either via the role's variable, or manually after deploying).
```yaml
deploy_gitea_runner_start_service: false # by default, set to false
```
This variable sets whether to start the service immediately or not. In case you manually register the runner after deployment, this should be set to `false`.
```yaml
deploy_gitea_runner_server_url: https://git.example.com # by default, set to https://git.example.com
```
This is the url of your gitea instance, and should be resolvable by the runner.
```yaml
deploy_gitea_runner_server_token: "" # by default, set to an empty string
```
This is your gitea token. if it isn't set, you cannot run auto-registration. THIS IS A SENSITIVE VALUE, AND SHOULD NOT APPEAR IN CLEAR TEXT IN YOUR REPOSITORY.
```yaml
deploy_gitea_runner_name: gitea-runner # by default, set to gitea-runner
```
This is the name under which the runner will register itself against your gitea server.
```yaml
deploy_gitea_runner_config: # by default, set to the following
log:
level: info
runner:
file: "{{ deploy_gitea_runner_directory }}/.runner" # this HAS TO BE .runner if deploy_gitea_runner_deploy_method is docker
capacity: 1
timeout: 3h
insecure: false
fetch_timeout: 5s
fetch_interval: 2s
labels: []
cache:
enabled: true
dir: "{{ deploy_gitea_runner_directory }}/cache" # this HAS TO BE /cache if deploy_gitea_runner_deploy_method is docker
host:
port: 0
external_server:
container:
network: ""
privileged: false
options:
workdir_parent:
valid_volumes: []
docker_host: ""
host:
workdir_parent:
```
This is the config file for gitea, put into a variable. The default values are from the default config.yaml generated when running `act_runner generate-config`. Some of the values, like `cache.dir` and `runner.file` have to be set to specific values in case you're running this role with `deploy_gitea_runner_deploy_method` set to `docker`. The rest is configurable according to the standard documentation.
Dependencies Dependencies
------------ ------------
None.
Example Playbook Example Playbook
---------------- ----------------

View File

@ -4,6 +4,7 @@ deploy_gitea_runner_version: latest
deploy_gitea_runner_deploy_method: host # deployment method, either host or docker. deploy_gitea_runner_deploy_method: host # deployment method, either host or docker.
deploy_gitea_runner_directory: /opt/gitea-actions deploy_gitea_runner_directory: /opt/gitea-actions
deploy_gitea_runner_timezone: "Europe/Paris" deploy_gitea_runner_timezone: "Europe/Paris"
deploy_gitea_runner_register: false
deploy_gitea_runner_start_service: false deploy_gitea_runner_start_service: false
deploy_gitea_runner_server_url: https://git.example.com deploy_gitea_runner_server_url: https://git.example.com
deploy_gitea_runner_server_token: "" deploy_gitea_runner_server_token: ""
@ -12,7 +13,7 @@ deploy_gitea_runner_config:
log: log:
level: info level: info
runner: runner:
file: .runner file: "{{ deploy_gitea_runner_directory }}/.runner" # this HAS TO BE .runner if deploy_gitea_runner_deploy_method is docker
capacity: 1 capacity: 1
timeout: 3h timeout: 3h
insecure: false insecure: false
@ -21,16 +22,16 @@ deploy_gitea_runner_config:
labels: [] labels: []
cache: cache:
enabled: true enabled: true
dir: dir: "{{ deploy_gitea_runner_directory }}/cache" # this HAS TO BE /cache if deploy_gitea_runner_deploy_method is docker
host: host: 127.0.0.1
port: 0 port: 9898
external_server: external_server:
container: container:
network: "" network: ""
privileged: false privileged: false
options: options: ""
workdir_parent: workdir_parent:
valid_volumes: [] valid_volumes: []
docker_host: "" docker_host: "-"
host: host:
workdir_parent: workdir_parent:

View File

@ -5,16 +5,16 @@
daemon_reload: true daemon_reload: true
listen: "systemctl-daemon-reload" listen: "systemctl-daemon-reload"
- name: "Enable gitea-runnner service" - name: "Enable gitea-runner service"
ansible.builtin.service: ansible.builtin.service:
name: gitea-runnner name: gitea-runner
enabled: true enabled: true
listen: "systemctl-enable-gitea-runnner" listen: "systemctl-enable-gitea-runner"
- name: "Start gitea-runnner service" - name: "Start gitea-runner service"
ansible.builtin.service: ansible.builtin.service:
name: gitea-runnner name: gitea-runner
state: restarted state: restarted
listen: "systemctl-restart-gitea-runnner" listen: "systemctl-restart-gitea-runner"
throttle: 1 throttle: 1
when: deploy_gitea_runner_start_service when: deploy_gitea_runner_start_service

View File

@ -0,0 +1,7 @@
---
- name: Converge
hosts: all
tasks:
- name: "Include ednxzu.deploy_gitea_runner"
ansible.builtin.include_role:
name: "ednxzu.deploy_gitea_runner"

View File

@ -0,0 +1,36 @@
---
deploy_gitea_runner_version: latest
deploy_gitea_runner_deploy_method: host # deployment method, either host or docker.
deploy_gitea_runner_directory: /opt/gitea-actions
deploy_gitea_runner_timezone: "Europe/Paris"
deploy_gitea_runner_register: true
deploy_gitea_runner_start_service: true
deploy_gitea_runner_server_url: https://git.example.com
deploy_gitea_runner_server_token: ""
deploy_gitea_runner_name: gitea-runner
deploy_gitea_runner_config:
log:
level: info
runner:
file: "{{ deploy_gitea_runner_directory }}/.runner" # this HAS TO BE .runner if deploy_gitea_runner_deploy_method is docker
capacity: 1
timeout: 3h
insecure: false
fetch_timeout: 5s
fetch_interval: 2s
labels: ["ubuntu-latest"]
cache:
enabled: true
dir: "{{ deploy_gitea_runner_directory }}/cache" # this HAS TO BE /cache if deploy_gitea_runner_deploy_method is docker
host: 127.0.0.1
port: 9898
external_server:
container:
network: ""
privileged: false
options: ""
workdir_parent:
valid_volumes: []
docker_host: "-"
host:
workdir_parent:

View File

@ -0,0 +1,37 @@
---
dependency:
name: galaxy
options:
requirements-file: ./requirements.yml
driver:
name: docker
platforms:
- name: instance
image: geerlingguy/docker-${MOLECULE_TEST_OS}-ansible
command: ""
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup
cgroupns_mode: host
privileged: true
pre_build_image: true
provisioner:
name: ansible
config_options:
defaults:
remote_tmp: /tmp/.ansible
verifier:
name: ansible
scenario:
name: with_docker_host
test_sequence:
- dependency
- cleanup
- destroy
- syntax
- create
- prepare
- converge
- idempotence
- verify
- cleanup
- destroy

View File

@ -0,0 +1,19 @@
---
- name: Verify
hosts: all
gather_facts: false
tasks:
- name: "Test: file /etc/hosts"
block:
- name: "Stat file /etc/hosts"
ansible.builtin.stat:
path: "/etc/hosts"
register: stat_etc_hosts
- name: "Verify file /etc/hosts"
ansible.builtin.assert:
that:
- stat_etc_hosts.stat.exists
- stat_etc_hosts.stat.isreg
- stat_etc_hosts.stat.pw_name == 'root'
- stat_etc_hosts.stat.gr_name == 'root'

View File

@ -2,27 +2,14 @@
# task/configure file for deploy_adguard # task/configure file for deploy_adguard
- name: "Copy gitea-runnner.service file" - name: "Copy gitea-runnner.service file"
ansible.builtin.template: ansible.builtin.template:
src: "gitea-runnner.service.j2" src: "gitea-runner.service.j2"
dest: "/etc/systemd/system/gitea-runnner.service" dest: "/etc/systemd/system/gitea-runner.service"
owner: root owner: root
group: root group: root
mode: '0644' mode: '0644'
notify: notify:
- systemctl-daemon-reload - systemctl-daemon-reload
- name: "Configure docker-compose deployment"
block:
- name: "Copy docker-compose.yml template"
ansible.builtin.template:
src: "docker-compose.yml.j2"
dest: "{{ deploy_gitea_runner_directory }}/docker-compose.yml"
owner: root
group: root
mode: '0600'
notify:
- systemctl-enable-gitea-runnner
- systemctl-restart-gitea-runnner
- name: "Copy config.yaml" - name: "Copy config.yaml"
ansible.builtin.template: ansible.builtin.template:
src: "config.yaml.j2" src: "config.yaml.j2"
@ -32,4 +19,5 @@
mode: '0600' mode: '0600'
when: deploy_gitea_runner_config != {} when: deploy_gitea_runner_config != {}
notify: notify:
- systemctl-restart-gitea-runnner - systemctl-enable-gitea-runner
- systemctl-restart-gitea-runner

View File

@ -1,2 +1,14 @@
--- ---
# task/install_docker file for deploy_gitea_runner # task/install_docker file for deploy_gitea_runner
- name: "Configure docker-compose deployment"
block:
- name: "Copy docker-compose.yml template"
ansible.builtin.template:
src: "docker-compose.yml.j2"
dest: "{{ deploy_gitea_runner_directory }}/docker-compose.yml"
owner: root
group: root
mode: '0600'
notify:
- systemctl-enable-gitea-runner
- systemctl-restart-gitea-runner

View File

@ -46,6 +46,8 @@
owner: root owner: root
group: root group: root
mode: '0755' mode: '0755'
notify:
- systemctl-restart-gitea-runner
- name: "Update version file" - name: "Update version file"
ansible.builtin.copy: ansible.builtin.copy:

View File

@ -8,3 +8,8 @@
- name: "Import configure.yml" - name: "Import configure.yml"
ansible.builtin.include_tasks: configure.yml ansible.builtin.include_tasks: configure.yml
- name: "Import register.yml"
ansible.builtin.include_tasks: register.yml
when: deploy_gitea_runner_register
and deploy_gitea_runner_deploy_method == 'host'

View File

@ -15,10 +15,11 @@
owner: root owner: root
group: root group: root
mode: '0755' mode: '0755'
when: deploy_gitea_runner_deploy_method == "docker"
- name: "Create gitea_actions/cache directory" - name: "Create gitea_actions/cache directory"
ansible.builtin.file: ansible.builtin.file:
path: "{{ deploy_gitea_runner_directory }}/cache" path: "{{ deploy_gitea_runner_config['cache']['dir'] if deploy_gitea_runner_deploy_method == 'host' else deploy_gitea_runner_directory + '/data' }}"
state: directory state: directory
owner: root owner: root
group: root group: root

28
tasks/register.yml Normal file
View File

@ -0,0 +1,28 @@
---
# task/register file for deploy_gitea_runner
- name: "Verify runner isnt already register"
ansible.builtin.stat:
path: "{{ deploy_gitea_runner_directory }}/.runner"
register: stat_gitea_runner_file
- name: "Register gitea-runner"
when: not stat_gitea_runner_file.stat.exists and
not deploy_gitea_runner_deploy_method == 'docker' and
not deploy_gitea_runner_server_token == ''
block:
- name: "Register gitea-runner"
ansible.builtin.command: "{{ deploy_gitea_runner_path }} register --no-interactive --instance $GITEA_URL --token $GITEA_TOKEN --name $GITEA_RUNNER_NAME --config $CONFIG_FILE"
environment:
GITEA_URL: "{{ deploy_gitea_runner_server_url }}"
GITEA_TOKEN: "{{ deploy_gitea_runner_server_token }}"
GITEA_RUNNER_NAME: "{{ deploy_gitea_runner_name }}"
CONFIG_FILE: "{{ deploy_gitea_runner_directory }}/config.yaml"
changed_when: false
register: register_gitea_runner
- name: "Ensure registration is successful"
ansible.builtin.assert:
that:
- register_gitea_runner.rc == 0
success_msg: "Runner {{ deploy_gitea_runner_name }} registered correctly !"
fail_msg: "Runner {{ deploy_gitea_runner_name }} Did not register correctly: {{ register_gitea_runner.stderr }}"

View File

@ -1,2 +1,2 @@
# {{ ansible_managed }} # {{ ansible_managed }}
{{ deploy_gitea_runner_config | to_nice_yaml }} {{ deploy_gitea_runner_config | to_yaml }}

View File

@ -3,7 +3,7 @@ version: '3.9'
services: services:
runner: runner:
image: gitea/act_runner:latest image: gitea/act_runner:{{ deploy_gitea_runner_version }}
configs: configs:
- source: config_yaml - source: config_yaml
target: /config.yaml target: /config.yaml

View File

@ -0,0 +1,29 @@
{% if deploy_gitea_runner_deploy_method == 'docker' %}
[Unit]
Description=Act runner is a runner for Gitea based on Gitea fork of act.
After=docker.service
Requires=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -c "docker-compose -f {{ deploy_gitea_runner_directory }}/docker-compose.yml up --detach"
ExecStop=/bin/bash -c "docker-compose -f {{ deploy_gitea_runner_directory }}/docker-compose.yml down"
[Install]
WantedBy=multi-user.target
{% elif deploy_gitea_runner_deploy_method == 'host' %}
[Unit]
Description=Act runner is a runner for Gitea based on Gitea fork of act.
After=network.target
Requires=network.target
[Service]
Type=simple
EnvironmentFile=-{{ deploy_gitea_runner_directory }}/gitea-runner.env
WorkingDirectory={{ deploy_gitea_runner_directory }}
ExecStart={{ deploy_gitea_runner_path }} daemon
[Install]
WantedBy=multi-user.target
{% endif %}

View File

@ -1,13 +0,0 @@
[Unit]
Description=Act runner is a runner for Gitea based on Gitea fork of act.
After=docker.service
Requires=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -c "docker-compose -f {{ deploy_gitea_runner_directory }}/docker-compose.yml up --detach"
ExecStop=/bin/bash -c "docker-compose -f {{ deploy_gitea_runner_directory }}/docker-compose.yml down"
[Install]
WantedBy=multi-user.target