Skip to content

Commit deca666

Browse files
Merge pull request #247 from staffanf/master
Add support for using --no-default-labels
2 parents fca72a8 + df05967 commit deca666

File tree

9 files changed

+128
-31
lines changed

9 files changed

+128
-31
lines changed

README.md

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ runner_org: false
113113
# Labels to apply to the runner
114114
runner_labels: []
115115
116+
# Disable default labels (self-hosted, Linux, X64) and require custom labels. Set `runner_no_default_labels: true` and provide at least one label in `runner_labels` to use this feature.
117+
runner_no_default_labels: false
118+
116119
# Group to add organization runner to
117120
runner_group: ""
118121
@@ -262,34 +265,9 @@ In this example the Ansible role will uninstall the runner service and unregiste
262265
1. Install Python, Docker, and Ansible if you haven't already.
263266
2. Install Molecule and its Docker driver with pip:
264267

265-
```bash
266-
pip install "molecule-plugins[docker]"
267-
```
268-
Sure, here's a basic example of how you might structure a README to explain how to test the `monolithprojects.github_actions_runner` Ansible role with Molecule:
269-
270-
```markdown
271-
# monolithprojects.github_actions_runner
272-
273-
This is an Ansible role for setting up GitHub Actions runners.
274-
275-
## Testing with Molecule
276-
277-
[Molecule](https://molecule.readthedocs.io/) is a testing framework for Ansible that we use to test the `monolithprojects.github_actions_runner` role.
278-
279-
### Prerequisites
280-
281-
- Python
282-
- Docker
283-
- Ansible
284-
- Molecule
285-
286-
### Installation
287-
288-
1. Install Python, Docker, and Ansible if you haven't already.
289-
2. Install Molecule and its Docker driver with pip:
290-
291268
```bash
292269
pip install molecule[docker]
270+
pip install "molecule-plugins[docker]"
293271
```
294272

295273
### Running Tests

defaults/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ runner_download_repository: "actions/runner"
4545
# Several arguments must be set as one string (i.e. "--ephemeral --my_special_fork")
4646
runner_extra_config_args: ""
4747

48+
# Disable default labels (self-hosted, Linux, X64) and require custom labels. Set `runner_no_default_labels: true` and provide at least one label in `runner_labels` to use this feature.
49+
runner_no_default_labels: false
50+
4851
# Name to assign to this runner in GitHub (System hostname as default)
4952
runner_name: "{{ ansible_facts.hostname }}"
5053

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
- name: Test no_default_labels scenario
3+
hosts: all
4+
become: yes
5+
vars:
6+
runner_user: ansible
7+
github_repo: "{{ lookup('env', 'GITHUB_REPO') }}"
8+
github_account: "{{ lookup('env', 'GITHUB_ACCOUNT') }}"
9+
runner_version: "latest"
10+
runner_name: test_name
11+
runner_no_default_labels: true
12+
runner_labels:
13+
- testlabel1
14+
- testlabel2
15+
roles:
16+
- role: monolithprojects.github_actions_runner
17+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
provisioner:
3+
name: ansible
4+
config_options:
5+
defaults:
6+
verbosity: 0
7+
playbooks:
8+
converge: converge.yml
9+
cleanup: ../default/cleanup.yml
10+
verify: verify.yml
11+
dependency:
12+
name: galaxy
13+
driver:
14+
name: docker
15+
platforms:
16+
- name: "${MOLECULE_IMAGE:-ubuntu22}-latest"
17+
image: "${namespace:-monolithprojects}/systemd-${MOLECULE_IMAGE:-ubuntu22}:latest"
18+
volumes:
19+
- "/sys/fs/cgroup:/sys/fs/cgroup:${MOLECULE_DOCKER_VOLUMES:-rw}" # Use "ro" for cgroup v1 and "rw" for cgroup v2
20+
cgroupns_mode: ${MOLECULE_DOCKER_CGROUPS_MODE:-"host"} # Use "private" for cgroup v1 and "host" for cgroup v2
21+
command: ${MOLECULE_DOCKER_COMMAND:-""}
22+
privileged: true
23+
pre_build_image: true
24+
verifier:
25+
name: ansible
26+
scenario:
27+
name: no_default_labels
28+
test_sequence:
29+
- dependency
30+
- destroy
31+
- syntax
32+
- create
33+
- prepare
34+
- converge
35+
- idempotence
36+
- side_effect
37+
- verify
38+
- cleanup
39+
- destroy
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
3+
roles:
4+
- name: robertdebock.epel
5+
version: 3.0.1
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
- name: Validate Repo runners
3+
user: ansible
4+
hosts: all
5+
gather_facts: yes
6+
become: yes
7+
vars:
8+
runner_user: ansible
9+
github_repo: "{{ lookup('env', 'GITHUB_REPO') }}"
10+
github_account: "{{ lookup('env', 'GITHUB_ACCOUNT') }}"
11+
github_api_url: "https://api.github.com"
12+
access_token: "{{ lookup('env', 'PERSONAL_ACCESS_TOKEN') }}"
13+
runner_name: "{{ ansible_facts.hostname }}"
14+
15+
tasks:
16+
- name: Check currently registered runners
17+
ansible.builtin.uri:
18+
url: "{{ github_api_url }}/repos/{{ github_owner | default(github_account) }}/{{ github_repo }}/actions/runners"
19+
headers:
20+
Authorization: "token {{ access_token }}"
21+
Accept: "application/vnd.github.v3+json"
22+
method: GET
23+
status_code: 200
24+
force_basic_auth: yes
25+
register: registered_runners
26+
27+
- name: Check Runner
28+
ansible.builtin.assert:
29+
that:
30+
- registered_runners.json.runners.0.status == "online"
31+
quiet: true
32+
33+
- debug:
34+
var: registered_runners.json.runners.0
35+
36+
- name: Set fact - current labels
37+
ansible.builtin.set_fact:
38+
current_labels: "{{ registered_runners.json.runners.0 | json_query('labels[*].name') | list }}"
39+
40+
- name: Check Labels (skipped if labels are OK)
41+
ansible.builtin.assert:
42+
that:
43+
- current_labels == ['testlabel1', 'testlabel2']
44+
fail_msg: "Expected only the custom labels 'testlabel1' and 'testlabel2', but got {{ current_labels }}"

tasks/assert.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,10 @@
3939
fail_msg: "runner_user_win_password was not defined, but it is required on a windows system"
4040
run_once: true
4141
when: github_actions_system == "win"
42+
43+
- name: Check runner_labels is not empty if runner_no_default_labels is true (RUN ONCE)
44+
ansible.builtin.assert:
45+
that:
46+
- not (runner_no_default_labels | bool) or (runner_labels is defined and runner_labels | length > 0)
47+
fail_msg: "runner_labels must be set and not empty when runner_no_default_labels is true."
48+
run_once: true

tasks/install_runner_unix.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
--labels {{ runner_labels | join(',') }} \
8686
--runnergroup {{ runner_group }} \
8787
--unattended \
88+
{{ '--no-default-labels' if runner_no_default_labels | bool else '' }} \
8889
{{ runner_extra_config_args }}"
8990
args:
9091
chdir: "{{ runner_dir }}"
@@ -123,6 +124,7 @@
123124
--name '{{ runner_name }}' \
124125
--labels {{ runner_labels | join(',') }} \
125126
--unattended \
127+
{{ '--no-default-labels' if runner_no_default_labels | bool else '' }} \
126128
{{ runner_extra_config_args }} \
127129
--replace"
128130
args:

tasks/install_runner_win.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@
7878
--runasservice \
7979
--windowslogonaccount {{ runner_user }} \
8080
--windowslogonpassword {{ runner_user_win_password }} \
81-
--unattended \
82-
{{ runner_extra_config_args }}"
81+
--unattended \
82+
{{ '--no-default-labels' if runner_no_default_labels | bool else '' }} \
83+
{{ runner_extra_config_args }}"
8384
args:
8485
chdir: "{{ runner_dir }}"
8586
changed_when: true
@@ -101,9 +102,10 @@
101102
--runasservice \
102103
--windowslogonaccount {{ runner_user }} \
103104
--windowslogonpassword {{ runner_user_win_password }} \
104-
--unattended \
105-
{{ runner_extra_config_args }} \
106-
--replace"
105+
--unattended \
106+
{{ '--no-default-labels' if runner_no_default_labels | bool else '' }} \
107+
{{ runner_extra_config_args }} \
108+
--replace"
107109
args:
108110
chdir: "{{ runner_dir }}"
109111
changed_when: true

0 commit comments

Comments
 (0)