Skip to content

Commit d6844c6

Browse files
Merge pull request #166 from danielefranceschi/main
support for enterprise self-hosted runners
2 parents a4c37f3 + ac3137e commit d6844c6

File tree

10 files changed

+157
-111
lines changed

10 files changed

+157
-111
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
push:
66
branches:
77
- master
8+
- main
89

910
jobs:
1011
lint:

.github/workflows/tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
pull_request:
55
branches:
66
- master
7+
- main
78
types: [opened, synchronize, reopened]
89
paths:
910
- 'defaults/**'

.pre-commit-config.yaml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
repos:
2-
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.0.1
4-
hooks:
5-
- id: check-yaml
6-
args: [--allow-multiple-documents]
7-
- id: end-of-file-fixer
8-
- id: trailing-whitespace
9-
args: [--markdown-linebreak-ext=md]
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.0.1
4+
hooks:
5+
- id: check-yaml
6+
args: [--allow-multiple-documents]
7+
- id: end-of-file-fixer
8+
- id: trailing-whitespace
9+
args: [--markdown-linebreak-ext=md]
1010

11-
- repo: https://github.com/adrienverge/yamllint
12-
rev: v1.26.3
13-
hooks:
14-
- id: yamllint
15-
args: [-c=.yamllint]
11+
- repo: https://github.com/adrienverge/yamllint
12+
rev: v1.26.3
13+
hooks:
14+
- id: yamllint
15+
args: [-c=.yamllint]
1616

17-
- repo: https://github.com/robertdebock/pre-commit
18-
rev: v1.2.3
19-
hooks:
20-
- id: ansible_role_find_unused_variable
21-
- id: ansible_role_find_empty_files
22-
- id: ansible_role_find_empty_directories
23-
- id: ansible_role_fix_readability
17+
- repo: https://github.com/robertdebock/pre-commit
18+
rev: v1.2.3
19+
hooks:
20+
- id: ansible_role_find_unused_variable
21+
- id: ansible_role_find_empty_files
22+
- id: ansible_role_find_empty_directories
23+
- id: ansible_role_fix_readability

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
This role will deploy/redeploy/uninstall and register/unregister local GitHub Actions Runner on Linux and macOS Systems (see [compatibility list](#supported-operating-systems) ).
12-
It supports both, Organization and Repository Runners.
12+
It supports Enterprise, Organization and Repository Runners.
1313

1414
## Requirements
1515

@@ -18,7 +18,8 @@ It supports both, Organization and Repository Runners.
1818
* The role require Personal Access Token to access the GitHub. The token can be set as `PERSONAL_ACCESS_TOKEN` environment variable.
1919

2020
> **Note**
21-
> The token must have the `repo` scope (when creating a repo runner) or the `admin:org` scope (when creating a runner for an organization).
21+
> The token must have the `repo` scope (when creating a repo runner), the `admin:org` scope (when creating a runner for an organization),
22+
> the `manage_runners:enterprise` scope (when creating a enterprise runner).
2223
Personal Access Token for GitHub account can be created [here](https://github.com/settings/tokens).
2324

2425
> **Warning**
@@ -112,6 +113,9 @@ runner_name: "{{ ansible_hostname }}"
112113
# Github repository name
113114
# github_repo: "yourrepo"
114115

116+
# GitHub Enterprise name
117+
# github_enterprise: "yourenterprise"
118+
115119
# Configuring a custom .env file
116120
# custom_env: |
117121
# http_proxy=YOUR_URL_HERE
@@ -122,7 +126,7 @@ runner_name: "{{ ansible_hostname }}"
122126
# HTTP_PROXY=
123127
```
124128

125-
## Example Playbook
129+
## Example Playbooks
126130

127131
In this example the Ansible role will install (or update) the GitHub Actions Runner service (latest available version). The runner will be registered for *my_awesome_repo* GitHub repo.
128132
Runner service will be stated and will run under the same user as the Ansible is using for ssh connection (*ansible*).
@@ -156,6 +160,20 @@ Same example as above, but runner will be added to an organization and deployed
156160
- role: monolithprojects.github_actions_runner
157161
```
158162
163+
If you have a Github Enterprise Cloud license and you want to manage all the self-hosted runners from the enterprise:
164+
```yaml
165+
---
166+
- name: Install GitHub Actions Runner
167+
hosts: all
168+
user: automation
169+
become: yes
170+
vars:
171+
- github_enterprise: my_awesome_enterprise
172+
- runner_org: no
173+
roles:
174+
- role: monolithprojects.github_actions_runner
175+
```
176+
159177
In this example the Ansible role will deploy (or update) the GitHub Actions runner service (version 2.165.2) and register the runner for the GitHub repo. Runner service will run under the user `runner-user`. Runner will be registered with two labels.
160178
The runner service will be *stopped* and disabled. Runner will use custom environment variables (from file named `.env` in the self-hosted runner application directory).
161179

defaults/main.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ runner_version: "latest"
1212
runner_state: "started"
1313

1414
# If found on the server, delete already existing runner service and install it again
15-
reinstall_runner: no
15+
reinstall_runner: false
1616

1717
# Do not show Ansible logs which may contain sensitive data (registration token)
18-
hide_sensitive_logs: yes
18+
hide_sensitive_logs: true
1919

2020
# GitHub address
2121
github_url: "https://github.com"
@@ -27,7 +27,7 @@ github_api_url: "https://api.github.com"
2727
access_token: "{{ lookup('env', 'PERSONAL_ACCESS_TOKEN') }}"
2828

2929
# Is it the runner for organization or not?
30-
runner_org: no
30+
runner_org: false
3131

3232
# Labels to apply to the runner
3333
runner_labels: []
@@ -54,6 +54,9 @@ runner_name: "{{ ansible_hostname }}"
5454
# Github repository name
5555
# github_repo: "yourrepo"
5656

57+
# GitHub Enterprise name
58+
# github_enterprise: "yourenterprise"
59+
5760
# Configuring a custom .env file
5861
# custom_env: |
5962
# http_proxy=YOUR_URL_HERE

tasks/assert.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- github_account is defined
66
fail_msg: "github_account is not defined"
77
run_once: true
8+
when: not github_enterprise
89

910
- name: Check access_token variable (RUN ONCE)
1011
ansible.builtin.assert:
@@ -20,6 +21,7 @@
2021
- runner_org | bool == True or runner_org == False
2122
fail_msg: "runner_org should be a boolean value"
2223
run_once: true
24+
when: not github_enterprise
2325

2426
- name: Check github_repo variable (RUN ONCE)
2527
ansible.builtin.assert:
@@ -28,4 +30,4 @@
2830
- github_repo | length > 0
2931
fail_msg: "github_repo was not found or is using an invalid format."
3032
run_once: true
31-
when: not runner_org
33+
when: not runner_org and not github_enterprise

tasks/collect_info.yml

Lines changed: 65 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,76 @@
22
- name: Info collections
33
check_mode: false
44
block:
5-
- name: Set complete API url for repo runner
6-
ansible.builtin.set_fact:
7-
github_full_api_url: "{{ github_api_url }}/repos/{{ github_owner | default(github_account) }}/{{ github_repo }}/actions/runners"
8-
when: not runner_org
5+
- name: Set complete API url for repo runner
6+
ansible.builtin.set_fact:
7+
github_full_api_url: "{{ github_api_url }}/repos/{{ github_owner | default(github_account) }}/{{ github_repo }}/actions/runners"
8+
when: not runner_org and not github_enterprise
99

10-
- name: Set complete API url for org runner
11-
ansible.builtin.set_fact:
12-
github_full_api_url: "{{ github_api_url }}/orgs/{{ github_owner | default(github_account) }}/actions/runners"
13-
when: runner_org | bool
10+
- name: Set complete API url for org runner
11+
ansible.builtin.set_fact:
12+
github_full_api_url: "{{ github_api_url }}/orgs/{{ github_owner | default(github_account) }}/actions/runners"
13+
when: runner_org | bool and not github_enterprise
1414

15-
- name: Get registration token (RUN ONCE)
16-
ansible.builtin.uri:
17-
url: "{{ github_full_api_url }}/registration-token"
18-
headers:
19-
Authorization: "token {{ access_token }}"
20-
Accept: "application/vnd.github.v3+json"
21-
method: POST
22-
status_code: 201
23-
force_basic_auth: yes
24-
register: registration
25-
run_once: true
15+
- name: Set complete API url for enterprise runner
16+
ansible.builtin.set_fact:
17+
github_full_api_url: "{{ github_api_url }}/enterprises/{{ github_enterprise }}/actions/runners"
18+
when: github_enterprise
2619

27-
- name: Check currently registered runners for repo (RUN ONCE)
28-
ansible.builtin.uri:
29-
url: "{{ github_full_api_url }}"
30-
headers:
31-
Authorization: "token {{ access_token }}"
32-
Accept: "application/vnd.github.v3+json"
33-
method: GET
34-
status_code: 200
35-
force_basic_auth: yes
36-
register: registered_runners
37-
run_once: true
20+
- name: Get registration token (RUN ONCE)
21+
ansible.builtin.uri:
22+
url: "{{ github_full_api_url }}/registration-token"
23+
headers:
24+
Authorization: "token {{ access_token }}"
25+
Accept: "application/vnd.github.v3+json"
26+
method: POST
27+
status_code: 201
28+
force_basic_auth: true
29+
register: registration
30+
run_once: true
3831

39-
- name: Get Runner User IDs
40-
ansible.builtin.command: id -u "{{ runner_user }}"
41-
changed_when: false
42-
register: runner_user_id
32+
- name: Check currently registered runners (RUN ONCE)
33+
ansible.builtin.uri:
34+
url: "{{ github_full_api_url }}"
35+
headers:
36+
Authorization: "token {{ access_token }}"
37+
Accept: "application/vnd.github.v3+json"
38+
method: GET
39+
status_code: 200
40+
force_basic_auth: true
41+
register: registered_runners
42+
run_once: true
4343

44-
- name: Get Runner Group IDs
45-
ansible.builtin.command: id -g "{{ runner_user }}"
46-
changed_when: false
47-
register: runner_user_group_id
44+
- name: Get Runner User IDs
45+
ansible.builtin.command: id -u "{{ runner_user }}"
46+
changed_when: false
47+
register: runner_user_id
4848

49-
- name: Set runner_system variable
50-
ansible.builtin.set_fact:
51-
runner_system: "{{ 'osx' if ansible_system == 'Darwin' else 'linux' }}"
49+
- name: Get Runner Group IDs
50+
ansible.builtin.command: id -g "{{ runner_user }}"
51+
changed_when: false
52+
register: runner_user_group_id
5253

53-
- name: Find the latest runner version (RUN ONCE)
54-
ansible.builtin.uri:
55-
url: "https://api.github.com/repos/{{ runner_download_repository }}/releases/latest"
56-
headers:
57-
Content-Type: "application/json"
58-
method: GET
59-
return_content: yes
60-
status_code: 200
61-
body_format: json
62-
check_mode: false
63-
register: api_response
64-
run_once: true
65-
become: false
66-
delegate_to: localhost
67-
when: runner_version == "latest"
54+
- name: Set runner_system variable
55+
ansible.builtin.set_fact:
56+
runner_system: "{{ 'osx' if ansible_system == 'Darwin' else 'linux' }}"
6857

69-
- name: Get systemd service facts
70-
ansible.builtin.service_facts:
71-
register: service_facts
72-
when: ansible_system == "Linux"
58+
- name: Find the latest runner version (RUN ONCE)
59+
ansible.builtin.uri:
60+
url: "https://api.github.com/repos/{{ runner_download_repository }}/releases/latest"
61+
headers:
62+
Content-Type: "application/json"
63+
method: GET
64+
return_content: true
65+
status_code: 200
66+
body_format: json
67+
check_mode: false
68+
register: api_response
69+
run_once: true
70+
become: false
71+
delegate_to: localhost
72+
when: runner_version == "latest"
73+
74+
- name: Get systemd service facts
75+
ansible.builtin.service_facts:
76+
register: service_facts
77+
when: ansible_system == "Linux"

tasks/install_deps.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- libssl1.1
1111
- libicu57
1212
state: present
13-
update_cache: yes
13+
update_cache: true
1414
when: (ansible_distribution == "Debian" and ansible_distribution_major_version == "9")
1515

1616
- name: Install dependencies on Debian Buster
@@ -23,7 +23,7 @@
2323
- libssl1.1
2424
- libicu63
2525
state: present
26-
update_cache: yes
26+
update_cache: true
2727
when: (ansible_distribution == "Debian" and ansible_distribution_major_version == "10")
2828

2929
- name: Install dependencies on Debian Bullseye
@@ -36,7 +36,7 @@
3636
- libssl1.1
3737
- libicu67
3838
state: present
39-
update_cache: yes
39+
update_cache: true
4040
when: (ansible_distribution == "Debian" and ansible_distribution_major_version == "11")
4141

4242
- name: Install dependencies on Debian Bookworm
@@ -49,7 +49,7 @@
4949
- libssl3
5050
- libicu72
5151
state: present
52-
update_cache: yes
52+
update_cache: true
5353
when: (ansible_distribution == "Debian" and ansible_distribution_major_version == "12")
5454

5555
- name: Install dependencies on Ubuntu Xenial systems
@@ -62,7 +62,7 @@
6262
- libssl1.0.0
6363
- libicu55
6464
state: present
65-
update_cache: yes
65+
update_cache: true
6666
when: (ansible_distribution == "Ubuntu" and ansible_distribution_major_version == "16")
6767

6868
- name: Install dependencies on Ubuntu Bionic systems
@@ -75,7 +75,7 @@
7575
- libssl1.1
7676
- libicu60
7777
state: present
78-
update_cache: yes
78+
update_cache: true
7979
when: (ansible_distribution == "Ubuntu" and ansible_distribution_major_version == "18")
8080

8181
- name: Install dependencies on Ubuntu Focal systems
@@ -88,7 +88,7 @@
8888
- libssl1.1
8989
- libicu66
9090
state: present
91-
update_cache: yes
91+
update_cache: true
9292
when: (ansible_distribution == "Ubuntu" and ansible_distribution_major_version == "20")
9393

9494
- name: Install dependencies on Ubuntu Jammy systems
@@ -100,7 +100,7 @@
100100
- zlib1g
101101
- libicu70
102102
state: present
103-
update_cache: yes
103+
update_cache: true
104104
when: (ansible_distribution == "Ubuntu" and ansible_distribution_major_version == "22")
105105

106106
- name: Install dependencies on RHEL/CentOS/Fedora systems
@@ -112,7 +112,7 @@
112112
- zlib
113113
- libicu
114114
state: present
115-
update_cache: yes
115+
update_cache: true
116116
when: (ansible_distribution == "RedHat") or
117117
(ansible_distribution == "CentOS") or
118118
(ansible_distribution == "Fedora") or

0 commit comments

Comments
 (0)