Skip to content

Commit 52ce09e

Browse files
Merge pull request #91 from MonolithProjects/develop
Develop to Master
2 parents 4630dce + 0bac7d2 commit 52ce09e

File tree

8 files changed

+181
-15
lines changed

8 files changed

+181
-15
lines changed

README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![License](https://img.shields.io/github/license/MonolithProjects/ansible-github_actions_runner)](https://github.com/MonolithProjects/ansible-github_actions_runner/blob/main/LICENSE)
99

1010

11-
This role will deploy/redeploy/uninstall and register/unregister local GitHub Actions Runner.
11+
This role will deploy/redeploy/uninstall and register/unregister local GitHub Actions Runner.
1212
It supports both, Organization and Repository Runners.
1313

1414
## Requirements
@@ -103,6 +103,15 @@ runner_extra_config_args: ""
103103

104104
# Github repository name
105105
# github_repo: "yourrepo"
106+
107+
# Configuring a custom .env file
108+
# custom_env: |
109+
# http_proxy=YOUR_URL_HERE
110+
# ftp_proxy=YOUR_URL_HERE
111+
# HTTPS_PROXY=YOUR_URL_HERE
112+
# https_proxy=YOUR_URL_HERE
113+
# no_proxy=localhost,127.0.0.1,127.0.0.2
114+
# HTTP_PROXY=
106115
```
107116

108117
## Example Playbook
@@ -139,7 +148,7 @@ Same example as above, but runner will be added to an organization.
139148
```
140149
141150
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.
142-
The runner service will be *stopped* and disabled.
151+
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).
143152

144153
```yaml
145154
---
@@ -155,6 +164,13 @@ The runner service will be *stopped* and disabled.
155164
- runner_labels:
156165
- production
157166
- west
167+
- custom_env: |
168+
HTTP_PROXY=http://proxy.local:8080
169+
http_proxy=http://proxy.local:8080
170+
HTTPS_PROXY=http://proxy.local:8080
171+
https_proxy=http://proxy.local:8080
172+
no_proxy=localhost,127.0.0.1,127.0.0.2
173+
158174
roles:
159175
- role: monolithprojects.github_actions_runner
160176
```

defaults/main.yml

+9-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ runner_download_repository: "actions/runner"
4343

4444
# Extra arguments to pass to `config.sh`
4545
runner_extra_config_args: ""
46-
4746
# Custom service name when usign Github Enterprise server
4847
# service_name: actions.runner._services.{{ runner_name }}.service
4948

@@ -55,3 +54,12 @@ runner_extra_config_args: ""
5554

5655
# Github repository name
5756
# github_repo: "yourrepo"
57+
58+
# Configuring a custom .env file
59+
# custom_env: |
60+
# http_proxy=YOUR_URL_HERE
61+
# ftp_proxy=YOUR_URL_HERE
62+
# HTTPS_PROXY=YOUR_URL_HERE
63+
# https_proxy=YOUR_URL_HERE
64+
# no_proxy=localhost,127.0.0.1,127.0.0.2
65+
# HTTP_PROXY=

molecule/custom_env/cleanup.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
- name: Cleanup
3+
user: ansible
4+
hosts: all
5+
become: yes
6+
vars:
7+
- runner_user: ansible
8+
- github_repo: ansible-github_actions_runner-testrepo
9+
- github_account: monolithprojects-testorg
10+
- runner_state: absent
11+
roles:
12+
- ansible-github_actions_runner

molecule/custom_env/converge.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
- name: Dev test playbook
3+
user: ansible
4+
hosts: all
5+
gather_facts: yes
6+
become: yes
7+
vars:
8+
- runner_user: ansible
9+
- github_repo: ansible-github_actions_runner-testrepo
10+
- github_account: monolithprojects-testorg
11+
- runner_version: "latest"
12+
- runner_labels:
13+
- label1
14+
- repo-runner
15+
- custom_env: |
16+
# HTTPS_PROXY=YOUR_URL_HERE
17+
18+
roles:
19+
- robertdebock.epel
20+
- ansible-github_actions_runner

molecule/custom_env/molecule.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
provisioner:
3+
name: ansible
4+
config_options:
5+
defaults:
6+
verbosity: 0
7+
playbooks:
8+
converge: converge.yml
9+
cleanup: cleanup.yml
10+
verify: verify.yml
11+
dependency:
12+
name: galaxy
13+
lint: |
14+
yamllint .
15+
ansible-lint .
16+
driver:
17+
name: docker
18+
platforms:
19+
- name: "${os:-ubuntu20}-${tag:-latest}"
20+
image: "${namespace:-monolithprojects}/systemd-${os:-ubuntu20}:${tag:-latest}"
21+
command: /sbin/init
22+
tmpfs:
23+
- /run
24+
- /tmp
25+
volumes:
26+
- /sys/fs/cgroup:/sys/fs/cgroup:ro
27+
privileged: yes
28+
pre_build_image: yes
29+
verifier:
30+
name: ansible
31+
scenario:
32+
name: custom_env
33+
test_sequence:
34+
- dependency
35+
- lint
36+
- destroy
37+
- syntax
38+
- create
39+
- converge
40+
- idempotence
41+
- verify
42+
- cleanup
43+
- destroy

molecule/custom_env/requirements.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
- role: robertdebock.epel
3+
version: 3.0.1

molecule/custom_env/verify.yml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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: ansible-github_actions_runner-testrepo
10+
- github_account: monolithprojects-testorg
11+
- github_api_url: "https://api.github.com"
12+
- access_token: "{{ lookup('env', 'PERSONAL_ACCESS_TOKEN') }}"
13+
- runner_name: ubuntu16-latest
14+
15+
tasks:
16+
- name: Check currently registered runners
17+
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+
- debug:
28+
var: registered_runners.json.runners
29+
30+
- name: Check Runner
31+
assert:
32+
that:
33+
- runner_name in registered_runners.json.runners|map(attribute='name')|list
34+
- registered_runners.json.runners|map(attribute='status') == ["online"]
35+
quiet: true
36+
37+
- name: Check Labels (skipped if labels are OK)
38+
fail:
39+
msg: Woops some labels differ "{{ (registered_runners.json.runners.0 | json_query('labels[*].name') | difference(['self-hosted', 'Linux', 'X64', 'label1', 'repo-runner'])) }}"
40+
when: not (registered_runners.json.runners.0 | json_query('labels[*].name') | list ) == (['self-hosted', 'Linux', 'X64', 'label1', 'repo-runner'] | list)
41+
42+
- name: Check custom env file is configured
43+
lineinfile:
44+
dest: /opt/actions-runner/.env
45+
line: "# HTTPS_PROXY=YOUR_URL_HERE"
46+
check_mode: yes
47+
register: presence
48+
failed_when: presence.changed

tasks/install_runner.yml

+28-12
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
tags:
4242
- install
4343

44-
- name: Create temporary directory for archive
44+
- name: Create temporary directory for archive
4545
file:
4646
path: "{{ runner_pkg_tempdir }}"
4747
state: directory
@@ -56,8 +56,9 @@
5656

5757
- name: Download runner package version - "{{ runner_version }}" (RUN ONCE)
5858
get_url:
59-
url: "https://github.com/{{ runner_download_repository }}/releases/download/v{{ runner_version }}/\
60-
actions-runner-linux-{{ github_actions_architecture }}-{{ runner_version }}.tar.gz"
59+
url:
60+
"https://github.com/{{ runner_download_repository }}/releases/download/v{{ runner_version }}/\
61+
actions-runner-linux-{{ github_actions_architecture }}-{{ runner_version }}.tar.gz"
6162
dest: "{{ runner_pkg_tempdir }}/actions-runner-linux-{{ runner_version }}.tar.gz"
6263
force: no
6364
run_once: yes
@@ -77,10 +78,22 @@
7778
tags:
7879
- install
7980

81+
- name: Configure custom env file if required
82+
blockinfile:
83+
path: "{{ runner_dir }}/.env"
84+
block: "{{ custom_env }}"
85+
owner: "{{ runner_user }}"
86+
create: yes
87+
mode: 0755
88+
marker_begin: "# BEGIN ANSIBLE MANAGED BLOCK"
89+
marker_end: "# END ANSIBLE MANAGED BLOCK"
90+
when: custom_env is defined
91+
8092
- name: Register runner (if new installation) for repo
81-
command: "{{ runner_dir }}/./config.sh --url {{ github_url }}/{{ github_owner | default(github_account) }}/{{ github_repo }} \
82-
--token {{ registration.json.token }} --name {{ runner_name }} --labels {{ runner_labels | join(',') }} --unattended \
83-
{{ runner_extra_config_args }}"
93+
command:
94+
"{{ runner_dir }}/./config.sh --url {{ github_url }}/{{ github_owner | default(github_account) }}/{{ github_repo }} \
95+
--token {{ registration.json.token }} --name {{ runner_name }} --labels {{ runner_labels | join(',') }} --unattended \
96+
{{ runner_extra_config_args }}"
8497
args:
8598
chdir: "{{ runner_dir }}"
8699
become: yes
@@ -91,8 +104,9 @@
91104
- install
92105

93106
- name: Register runner (if new installation) for organization
94-
command: "{{ runner_dir }}/./config.sh --url {{ github_url }}/{{ github_owner | default(github_account) }} \
95-
--token {{ registration.json.token }} --name {{ runner_name }} --labels {{ runner_labels | join(',') }} --unattended"
107+
command:
108+
"{{ runner_dir }}/./config.sh --url {{ github_url }}/{{ github_owner | default(github_account) }} \
109+
--token {{ registration.json.token }} --name {{ runner_name }} --labels {{ runner_labels | join(',') }} --unattended"
96110
args:
97111
chdir: "{{ runner_dir }}"
98112
become: yes
@@ -103,8 +117,9 @@
103117
- install
104118

105119
- name: Replace registered runner for repo
106-
command: "{{ runner_dir }}/config.sh --url {{ github_url }}/{{ github_owner | default(github_account) }}/{{ github_repo }} \
107-
--token {{ registration.json.token }} --name {{ runner_name }} --labels {{ runner_labels | join(',') }} --unattended --replace"
120+
command:
121+
"{{ runner_dir }}/config.sh --url {{ github_url }}/{{ github_owner | default(github_account) }}/{{ github_repo }} \
122+
--token {{ registration.json.token }} --name {{ runner_name }} --labels {{ runner_labels | join(',') }} --unattended --replace"
108123
args:
109124
chdir: "{{ runner_dir }}"
110125
become: yes
@@ -115,8 +130,9 @@
115130
- install
116131

117132
- name: Replace registered runner for organization
118-
command: "{{ runner_dir }}/config.sh --url {{ github_url }}/{{ github_owner | default(github_account) }} \
119-
--token {{ registration.json.token }} --name {{ runner_name }} --labels {{ runner_labels | join(',') }} --unattended --replace"
133+
command:
134+
"{{ runner_dir }}/config.sh --url {{ github_url }}/{{ github_owner | default(github_account) }} \
135+
--token {{ registration.json.token }} --name {{ runner_name }} --labels {{ runner_labels | join(',') }} --unattended --replace"
120136
args:
121137
chdir: "{{ runner_dir }}"
122138
become: yes

0 commit comments

Comments
 (0)