Skip to content

Commit

Permalink
Merge pull request #26 from MonolithProjects/develop
Browse files Browse the repository at this point in the history
Change GitHub API authentication method
  • Loading branch information
MonolithProjects authored Aug 13, 2020
2 parents 8f14b1d + 7bcc7cd commit 97b20e7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches:
- master
- develop
- feature/*
schedule:
- cron: '0 6 * * 0'
jobs:
Expand Down
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This role will deploy/redeploy/uninstall and register/unregister local GitHub Ac

* System must have access to the GitHub.

* The role require Personal Access Token for the GitHub user. The token has to be a value of `PERSONAL_ACCESS_TOKEN` variable.
* The role require Personal Access Token to access the GitHub. The token has to be a value of `PERSONAL_ACCESS_TOKEN` variable.
Export the token to the local host environment. The token has to have admin rights for the repo.
Personal Access Token for GitHub account can be created [here](https://github.com/settings/tokens).
**Note:** Never store you personal access token in the GitHub repository. Use [GitHub Secrets](https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets) or some different secrets service.
Expand Down Expand Up @@ -51,13 +51,13 @@ replace_runner: yes
# Do not show Ansible logs which may contain sensitive data (registration token)
hide_sensitive_logs: yes

# Personal Access Token for your GitHub account
access_token: "{{ lookup('env', 'PERSONAL_ACCESS_TOKEN') }}"

# GitHub address
github_server: "https://github.com"

# GitHub account name
# Personal Access Token
access_token: "{{ lookup('env', 'PERSONAL_ACCESS_TOKEN') }}"

# Account used for Runner registration (GitHub Repository user with admin rights or Organization owner)
# github_account: "youruser"

# Github repository name
Expand All @@ -66,7 +66,7 @@ github_server: "https://github.com"

## Example Playbook

In this example the role will deploy (or redeploy) the GitHub Actions runner service (latest available version) and register the runner for the GitHub repo.
In this example the Ansible role will deploy (or redeploy) the GitHub Actions runner service (latest available version) and register the runner for the GitHub repo.
Runner service will run under the same user as the Ansible is using for ssh connection (*ansible*).

```yaml
Expand All @@ -76,13 +76,13 @@ Runner service will run under the same user as the Ansible is using for ssh conn
user: ansible
become: yes
vars:
- github_account: my-github-user
- github_account: github-access-user
- github_repo: my_awesome_repo
roles:
- role: monolithprojects.github_actions_runner
```
In this example the 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 used `runner-user`.
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`.

```yaml
---
Expand All @@ -92,7 +92,7 @@ In this example the role will deploy (or redeploy) the GitHub Actions runner ser
vars:
- runner_version: "2.165.2"
- runner_user: runner-user
- github_account: my-github-user
- github_account: github-access-user
- github_repo: my_awesome_repo
roles:
- role: monolithprojects.github_actions_runner
Expand All @@ -104,12 +104,10 @@ By using tag `uninstall`, GitHub Actions runner will be removed from the host an
ansible-playbook playbook.yml --tags uninstall
```

License
-------
## License

MIT

Author Information
------------------
## Author Information

Created in 2020 by Michal Muransky
8 changes: 4 additions & 4 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ replace_runner: yes
# Do not show Ansible logs which may contain sensitive data (registration token)
hide_sensitive_logs: yes

# Personal Access Token for your GitHub account
access_token: "{{ lookup('env', 'PERSONAL_ACCESS_TOKEN') }}"

# GitHub address
github_server: "https://github.com"

# GitHub account name
# Personal Access Token for your GitHub account
access_token: "{{ lookup('env', 'PERSONAL_ACCESS_TOKEN') }}"

# GitHub Repository user or Organization owner used for Runner registration
# github_account: "youruser"

# Github repository name
Expand Down
12 changes: 7 additions & 5 deletions tasks/collect_info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
- name: Get registration token (RUN ONCE)
uri:
url: "https://api.github.com/repos/{{ github_account }}/{{ github_repo }}/actions/runners/registration-token"
user: "{{ github_account }}"
password: "{{ access_token }}"
headers:
Authorization: "token {{ access_token }}"
Accept: "application/vnd.github.v3+json"
method: POST
status_code: 201
force_basic_auth: yes
Expand All @@ -16,8 +17,9 @@
- name: Check currently registered runners (RUN ONCE)
uri:
url: "https://api.github.com/repos/{{ github_account }}/{{ github_repo }}/actions/runners"
user: "{{ github_account }}"
password: "{{ access_token }}"
headers:
Authorization: "token {{ access_token }}"
Accept: "application/vnd.github.v3+json"
method: GET
status_code: 200
force_basic_auth: yes
Expand All @@ -35,7 +37,7 @@

- name: Combine Github account and repo names
set_fact:
svc_name: "{{ github_account }}-{{ github_repo }}"
svc_name: "{{ github_owner | default(github_account) }}-{{ github_repo }}"
tags:
- install
- uninstall
Expand Down
9 changes: 5 additions & 4 deletions tasks/install_runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
- name: Find the latest runner version (RUN ONCE)
uri:
url: "https://api.github.com/repos/actions/runner/releases/latest"
url_username: "{{ github_account }}"
url_password: "{{ access_token }}"
headers:
Authorization: "token {{ access_token }}"
Accept: "application/vnd.github.v3+json"
method: GET
force_basic_auth: yes
return_content: yes
Expand Down Expand Up @@ -51,7 +52,7 @@
- install

- name: Register runner (if new installation)
command: "{{ runner_dir }}/./config.sh --url {{ github_server }}/{{ github_account }}/{{ github_repo }} \
command: "{{ runner_dir }}/./config.sh --url {{ github_server }}/{{ github_owner | default(github_account) }}/{{ github_repo }} \
--token {{ registration.json.token }} --unattended"
args:
chdir: "{{ runner_dir }}"
Expand All @@ -63,7 +64,7 @@
- install

- name: Replace registered runner
command: "{{ runner_dir }}/config.sh --url {{ github_server }}/{{ github_account }}/{{ github_repo }} \
command: "{{ runner_dir }}/config.sh --url {{ github_server }}/{{ github_owner | default(github_account) }}/{{ github_repo }} \
--token {{ registration.json.token }} --unattended --replace"
args:
chdir: "{{ runner_dir }}"
Expand Down

0 comments on commit 97b20e7

Please sign in to comment.