Skip to content

Commit 12a369a

Browse files
Merge pull request #36 from pvlltvk/master
Add support for organization runners + use handler
2 parents 6678230 + 23f948c commit 12a369a

File tree

7 files changed

+106
-5
lines changed

7 files changed

+106
-5
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ github_server: "https://github.com"
6060
# Personal Access Token
6161
access_token: "{{ lookup('env', 'PERSONAL_ACCESS_TOKEN') }}"
6262

63+
# Is it runner for organization or not
64+
runner_org: false
65+
6366
# Account used for Runner registration (GitHub Repository user with admin rights or Organization owner)
6467
# github_account: "youruser"
6568

@@ -85,6 +88,21 @@ Runner service will run under the same user as the Ansible is using for ssh conn
8588
- role: monolithprojects.github_actions_runner
8689
```
8790
91+
Same example, but runner will be added to an organization
92+
93+
```yaml
94+
---
95+
- name: GitHub Actions Runner
96+
hosts: all
97+
user: ansible
98+
become: yes
99+
vars:
100+
- github_account: my_awesome_org
101+
- runner_org: true
102+
roles:
103+
- role: monolithprojects.github_actions_runner
104+
```
105+
88106
In this example the Ansible role will deploy (or redeploy) 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`.
89107

90108
```yaml

defaults/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ github_server: "https://github.com"
2323
# Personal Access Token for your GitHub account
2424
access_token: "{{ lookup('env', 'PERSONAL_ACCESS_TOKEN') }}"
2525

26+
# Is it runner for organization or not
27+
runner_org: false
28+
2629
# GitHub Repository user or Organization owner used for Runner registration
2730
# github_account: "youruser"
2831

handlers/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
---
22
# handlers file for ansible-github_actions_runner
3+
- name: Restart runner service
4+
service:
5+
name: "{{ runner_service }}"
6+
state: restarted

tasks/collect_info_org.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
- name: Get registration token (RUN ONCE)
3+
uri:
4+
url: "https://api.github.com/orgs/{{ github_account }}/actions/runners/registration-token"
5+
headers:
6+
Authorization: "token {{ access_token }}"
7+
Accept: "application/vnd.github.v3+json"
8+
method: POST
9+
status_code: 201
10+
force_basic_auth: yes
11+
register: registration
12+
run_once: yes
13+
tags:
14+
- install
15+
- uninstall
16+
17+
- name: Check currently registered runners (RUN ONCE)
18+
uri:
19+
url: "https://api.github.com/orgs/{{ github_account }}/actions/runners"
20+
headers:
21+
Authorization: "token {{ access_token }}"
22+
Accept: "application/vnd.github.v3+json"
23+
method: GET
24+
status_code: 200
25+
force_basic_auth: yes
26+
register: registered_runners
27+
run_once: yes
28+
tags:
29+
- install
30+
- uninstall
31+
32+
- name: Check service facts
33+
service_facts:
34+
tags:
35+
- install
36+
- uninstall
37+
38+
- name: Build service name
39+
set_fact:
40+
runner_service: "actions.runner.{{ github_account[:45] }}.{{ ansible_hostname }}.service"
41+
tags:
42+
- install
43+
- uninstall
File renamed without changes.

tasks/install_runner.yml

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,56 @@
7272
owner: "{{ runner_user }}"
7373
mode: 0755
7474
when: runner_version not in runner_installed.stdout
75+
notify:
76+
- Restart runner service
7577
tags:
7678
- install
7779

78-
- name: Register runner (if new installation)
80+
- name: Register runner (if new installation) for repo
7981
command: "{{ runner_dir }}/./config.sh --url {{ github_server }}/{{ github_owner | default(github_account) }}/{{ github_repo }} \
8082
--token {{ registration.json.token }} --unattended"
8183
args:
8284
chdir: "{{ runner_dir }}"
8385
become: yes
8486
become_user: "{{ runner_user }}"
8587
no_log: "{{ hide_sensitive_logs | bool }}"
86-
when: ansible_hostname not in registered_runners.json.runners|map(attribute='name')|list
88+
when: ansible_hostname not in registered_runners.json.runners|map(attribute='name')|list and not runner_org
8789
tags:
8890
- install
8991

90-
- name: Replace registered runner
92+
- name: Register runner (if new installation) for organization
93+
command: "{{ runner_dir }}/./config.sh --url {{ github_server }}/{{ github_owner | default(github_account) }} \
94+
--token {{ registration.json.token }} --unattended"
95+
args:
96+
chdir: "{{ runner_dir }}"
97+
become: yes
98+
become_user: "{{ runner_user }}"
99+
no_log: "{{ hide_sensitive_logs | bool }}"
100+
when: ansible_hostname not in registered_runners.json.runners|map(attribute='name')|list and runner_org
101+
tags:
102+
- install
103+
104+
- name: Replace registered runner for repo
91105
command: "{{ runner_dir }}/config.sh --url {{ github_server }}/{{ github_owner | default(github_account) }}/{{ github_repo }} \
92106
--token {{ registration.json.token }} --unattended --replace"
93107
args:
94108
chdir: "{{ runner_dir }}"
95109
become: yes
96110
become_user: "{{ runner_user }}"
97111
no_log: "{{ hide_sensitive_logs | bool }}"
98-
when: replace_runner and ansible_hostname in registered_runners.json.runners|map(attribute='name')|list
112+
when: replace_runner and ansible_hostname in registered_runners.json.runners|map(attribute='name')|list and not github_org
113+
tags:
114+
- install
115+
116+
- name: Replace registered runner for organization
117+
command: "{{ runner_dir }}/config.sh --url {{ github_server }}/{{ github_owner | default(github_account) }} \
118+
--token {{ registration.json.token }} --unattended --replace"
119+
args:
120+
chdir: "{{ runner_dir }}"
121+
become: yes
122+
become_user: "{{ runner_user }}"
123+
no_log: "{{ hide_sensitive_logs | bool }}"
124+
when: replace_runner and ansible_hostname in registered_runners.json.runners|map(attribute='name')|list and github_org
99125
tags:
100126
- install
101127

tasks/main.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
---
2-
- include_tasks: collect_info.yml
2+
- include_tasks: collect_info_org.yml
3+
when: runner_org
4+
tags:
5+
- install
6+
- uninstall
7+
8+
- include_tasks: collect_info_repo.yml
9+
when: not runner_org
310
tags:
411
- install
512
- uninstall

0 commit comments

Comments
 (0)