Skip to content

Commit 9fa50e5

Browse files
committed
build(lint): Adding YAML and Ansible lint
Execute YAML and Ansible lint using GitHub Actions. Instructions on the README about this role, purpose and goals.
1 parent 4cc3727 commit 9fa50e5

File tree

9 files changed

+247
-18
lines changed

9 files changed

+247
-18
lines changed

.github/actions/config/ansible.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
skip_list:
3+
# YAML lint is executed apart from Ansible lint to support custom
4+
# configurations
5+
- yaml
6+
- role-name

.github/actions/config/yamllint.yaml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
rules:
3+
braces:
4+
min-spaces-inside: 0
5+
max-spaces-inside: 0
6+
min-spaces-inside-empty: -1
7+
max-spaces-inside-empty: -1
8+
brackets:
9+
min-spaces-inside: 0
10+
max-spaces-inside: 0
11+
min-spaces-inside-empty: -1
12+
max-spaces-inside-empty: -1
13+
colons:
14+
max-spaces-before: 0
15+
max-spaces-after: 1
16+
commas:
17+
max-spaces-before: 0
18+
min-spaces-after: 1
19+
max-spaces-after: 1
20+
comments:
21+
require-starting-space: yes
22+
min-spaces-from-content: 1
23+
document-end: disable
24+
document-start: enable
25+
empty-lines:
26+
max: 1
27+
max-start: 0
28+
max-end: 0
29+
hyphens:
30+
max-spaces-after: 1
31+
indentation:
32+
spaces: consistent
33+
indent-sequences: consistent
34+
check-multi-line-strings: no
35+
# key-duplicates: enable
36+
line-length:
37+
max: 120
38+
allow-non-breakable-words: yes
39+
new-line-at-end-of-file: enable
40+
new-lines:
41+
type: unix
42+
# trailing-spaces: disable
43+
truthy:
44+
allowed-values:
45+
- !!str yes
46+
- !!str true
47+
- !!str no
48+
- !!str false
49+
level: warning

.github/workflows/ci.yaml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
name: Continuous Integration
3+
4+
on:
5+
push:
6+
branches: ["**"]
7+
pull_request:
8+
branches: [master]
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
python-version:
17+
- "3.8"
18+
19+
steps:
20+
- uses: actions/checkout@v2
21+
22+
- name: Set up Python ${{ matrix.python-version }}
23+
uses: actions/setup-python@v2
24+
with:
25+
python-version: ${{ matrix.python-version }}
26+
27+
- name: Enable cache for (pip) dependencies
28+
uses: actions/cache@v2
29+
with:
30+
path: ~/.cache/pip
31+
key: ${{ runner.os }}-pip
32+
restore-keys: |
33+
${{ runner.os }}-pip-
34+
35+
- name: Lint | Check Ansible and YAML
36+
run: |
37+
python -m pip install --upgrade pip
38+
python -m pip install yamllint ansible ansible-lint
39+
40+
yamllint -c .github/actions/config/yamllint.yaml .
41+
ansible-lint -c .github/actions/config/ansible.yaml .
42+
43+
- name: Galaxy | Import
44+
if: ${{ github.ref == 'refs/heads/master' }}
45+
run: |
46+
ansible-galaxy role import \
47+
--api-key ${ANSIBLE_GALAXY_API_KEY} \
48+
--branch master \
49+
macunha1 github_actions_runner
50+
env:
51+
ANSIBLE_GALAXY_API_KEY: ${{ secrets.ANSIBLE_GALAXY_API_KEY }}

README.md

+81-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,86 @@
11
<h1 align="center">GitHub Actions self-hosted Runner Ansible role</h1>
22

3-
Work In Progress.
3+
An Ansible role that installs and configures GitHub Actions self-hosted Runners
4+
inside one or multiple hosts, you can re-use it for many different URLs
5+
(repositories or organizations) inside the same host in order to re-use it as
6+
much as possible.
47

5-
All-in-all the features of this Ansible role are implemented just not
6-
documented and well-presented yet. Ansible Galaxy repository import and
7-
documentation about the variables are on its way.
8+
Main goals of this role:
89

9-
To-dos:
10+
- _avoid waste_: re-use the same host to provide a build environment for
11+
multiple repositories or organizations;
12+
- _idempotence_: executing the role many times won't make anything break, steps
13+
have checks that validate whether or not they should be executed;
1014

11-
- [ ] Improve this README with variables + instructions;
12-
- [ ] Import to Ansible Galaxy;
13-
- [ ] Add CI using molecule;
14-
- [ ] Integrate GitHub actions to run the CI;
15-
- [ ] Add Code lint (YAML lint + ansible lint) on CI;
16-
- [ ] Include documentation
15+
## Variables
16+
17+
For an exhaustive list of variables check the [defaults](defaults/main.yaml)
18+
file. Ideally, all values will have commentaries describing what are their
19+
purposes and by the default value you can tell the type.
20+
21+
### Required variables
22+
23+
Following values are required since there is no way to register the self-hosted
24+
Runner without them
25+
26+
| Name | Description |
27+
| ---------------------- | -------------------------------------------------- |
28+
| gh_runner_config_url | GitHub Repository or Organization URL |
29+
| gh_runner_config_token | GitHub Registration token to authenticate the host |
30+
31+
## Example Playbook
32+
33+
Simplest use case: Single repository configuration on one host.
34+
35+
```yaml
36+
- hosts: foo
37+
roles:
38+
- role: macunha1.github_actions_runner
39+
vars:
40+
gh_runner_config_labels:
41+
- linux
42+
- self-hosted
43+
44+
gh_runner_config_url: https://github.com/macunha1/ansible-github-actions-runner
45+
gh_runner_config_token: AC5TNLJP9SBAFNEKKLLBLF264J8XO
46+
```
47+
48+
Complex use case to which this role was created for
49+
50+
```yaml
51+
- hosts: foo
52+
roles:
53+
- role: macunha1.github_actions_runner
54+
vars:
55+
gh_runner_config_labels:
56+
- linux
57+
- self-hosted
58+
59+
gh_runner_config_url: https://github.com/macunha1/ansible-github-actions-runner
60+
gh_runner_config_token: AC5TNLJP9SBAFNEKKLLBLF264J8XO
61+
62+
- role: macunha1.github_actions_runner
63+
vars:
64+
gh_runner_config_url: https://github.com/macunha1/another-repository
65+
gh_runner_config_token: AC5CQV3IJRR2OAFGEFCPJ0WJPJQXO
66+
67+
- role: macunha1.github_actions_runner
68+
vars:
69+
gh_runner_config_url: https://github.com/macunha-acme-corp
70+
gh_runner_config_token: ACYWUR9MHGR9U58C34W9ZK00UNBF
71+
```
72+
73+
Note that despite using the same host, each one of these GitHub Actions Runner
74+
configuration will have its own path and credentials. Therefore, they can live
75+
well in harmony without killing each other.
76+
77+
## Contribute
78+
79+
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
80+
81+
Feel free to fill [an issue](https://github.com/macunha1/ansible-github-actions-runner/issues)
82+
containing feature request(s), or (even better) to send me a Pull request, I
83+
would be happy to collaborate with you.
84+
85+
If this role didn't work for you, or if you found some bug during the execution,
86+
let me know.

includes/download.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- block:
1616
- name: GitHub Actions Runner | Download package
1717
get_url:
18+
# yamllint disable-line rule:line-length
1819
url: "{{ gh_runner_download_base_url }}/v{{ gh_runner_version }}/actions-runner-{{ gh_runner_architecture }}-{{ gh_runner_version }}.tar.gz"
1920
dest: "{{ tmp_download_path }}/actions-runner-{{ gh_runner_version }}.tar.gz"
2021
mode: 0400

includes/install.yaml

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
- name: GitHub Actions Runner | Install dependencies
3-
shell:
3+
command:
44
cmd: "{{ gh_runner_path }}/bin/installdependencies.sh"
55
chdir: "{{ gh_runner_path }}"
66
become: true
@@ -36,7 +36,7 @@
3636
when: not is_this_host_registered.stat.exists
3737

3838
- name: GitHub Actions Runner | Remove previous configuration before proceeding
39-
shell:
39+
command:
4040
cmd: >-
4141
{{ gh_runner_path }}/config.sh remove \
4242
--token {{ gh_runner_config_token }}
@@ -55,7 +55,7 @@
5555
- configure
5656

5757
- name: GitHub Actions Runner | Configure Runner
58-
shell:
58+
command:
5959
cmd: >-
6060
{{ gh_runner_path }}/config.sh \
6161
--unattended --replace \
@@ -105,18 +105,29 @@
105105
- configure
106106

107107
- name: GitHub Actions Runner | Install service
108-
shell:
108+
command:
109109
cmd: "{{ gh_runner_path }}/svc.sh install"
110110
chdir: "{{ gh_runner_path }}"
111111
become: true
112112
when: not is_this_host_registered.stat.exists
113113
tags:
114114
- install
115115

116+
- name: GitHub Actions Runner | Check service status
117+
command:
118+
cmd: "{{ gh_runner_path }}/svc.sh status"
119+
chdir: "{{ gh_runner_path }}"
120+
register: gh_runner_service_status
121+
changed_when: false # never changes, this is just a read-only command
122+
become: true
123+
tags:
124+
- configure
125+
116126
- name: GitHub Actions Runner | Start service
117-
shell:
127+
command:
118128
cmd: "{{ gh_runner_path }}/svc.sh start"
119129
chdir: "{{ gh_runner_path }}"
130+
when: not '"active (running)"' in gh_runner_service_status.stdout
120131
become: true
121132
tags:
122133
- configure

includes/uninstall.yaml

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
---
22
- name: GitHub Actions Runner [!!] DESTROY! | De-register Runner
3-
shell:
3+
command:
44
cmd: >-
55
{{ gh_runner_path }}/config.sh \
66
remove --token "{{ gh_runner_config_token }}"
77
chdir: "{{ gh_runner_path }}"
8+
changed_when: gh_runner_remove_host
89
become: true
910

1011
- name: GitHub Actions Runner [!!] DESTROY! | Stop service
11-
shell:
12+
command:
1213
cmd: "{{ gh_runner_path }}/svc.sh uninstall"
1314
chdir: "{{ gh_runner_path }}"
15+
changed_when: gh_runner_remove_host
1416
become: true
1517

1618
- name: GitHub Actions Runner [!!] DESTROY! | Delete workspace and installations
@@ -20,4 +22,5 @@
2022
with_items:
2123
- "{{ gh_runner_installation_path }}/"
2224
- "{{ gh_runner_workspace_path }}/"
25+
changed_when: gh_runner_remove_host
2326
become: true

meta/main.yaml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
galaxy_info:
3+
role_name: github_actions_runner
4+
author: Matheus Cunha
5+
description: >
6+
Idempotent Ansible role that installs and configures self-hosted GitHub
7+
Actions Runners (yeah, plural!)
8+
license: MIT
9+
company: None
10+
min_ansible_version: 2.2
11+
platforms:
12+
- name: Debian
13+
versions:
14+
- jessie
15+
- stretch
16+
- buster
17+
- name: EL
18+
versions:
19+
- 6
20+
- 7
21+
- 8
22+
- name: Fedora
23+
versions:
24+
- 34
25+
- 35
26+
- name: Ubuntu
27+
versions:
28+
- xenial
29+
- trusty
30+
- bionic
31+
- focal
32+
galaxy_tags:
33+
- github
34+
- actions
35+
- workflows
36+
- runner

tasks/main.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#
1818
# Therefore, each GitHub repository or organization URL will be hashed to
1919
# compose the GitHub Actions Runner path.
20+
#
21+
# yamllint disable-line rule:line-length
2022
gh_runner_path: "{{ gh_runner_installation_path }}/{{ gh_runner_version }}/{{ gh_runner_config_url | hash('sha256') }}"
2123

2224
tags:

0 commit comments

Comments
 (0)