Skip to content

Commit

Permalink
Finish github runner service installation:
Browse files Browse the repository at this point in the history
- Install a separate runner for each operations_github_runners.
- Set default values for runner path name, labels, repo and user.
- Download one copy of the runner, then copy to all other runners.
  • Loading branch information
jonpugh committed May 21, 2024
1 parent dc8a318 commit 6659d00
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
3 changes: 3 additions & 0 deletions roles/operations.runner/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@

# Global runner path. github.yml will copy this dir instead of downloading.
operations_github_runner_path: /usr/share/github-runner
operations_github_api_token: DefaultApiToken
operations_github_runners:
- repo_name: operations-project/site-runner
# Set repo_host if using enterprise github.
# repo_host: https://github.com
runner_name: "{{ inventory_hostname }}"
runner_path: "runner"
# Set a runner-specific api_token, if desired.
# api_token: "{{ operations_github_api_token }}"
runner_labels: "example,local,operations,{{ inventory_hostname }}"
Expand Down
61 changes: 41 additions & 20 deletions roles/operations.runner/tasks/github.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
---
- name: Prepare runner
user:
name: "{{ item.runner_user | default(operations_platform_user) | default('platform')}}"
state: present
register: user_facts

- name: Prepare variables
become: true
become_user: "{{ item.runner_user | default(operations_platform_user) | default('platform')}}"
set_fact:
# @TODO: Is there a better way to set defaults in a list of maps?
item_runner_path: "{{ user_facts.home }}/{{ item.runner_path | default('runner')}}"
item_runner_user: "{{ item.runner_user | default(operations_platform_user) | default('platform')}}"
item_runner_name: "{{ item.runner_user | default(operations_platform_user) }}@{{ item.runner_name | default(inventory_hostname) }}"
item_runner_repo: "{{ item.runner_repo | default(operations_runner_repo_default) }}"
item_runner_labels: "{{ inventory_hostname }},user/{{ item.runner_user | default(operations_platform_user) | default('platform') }},{{ item.runner_labels | default('') }}"
item_runner_api_token: "{{ item.api_token | default(operations_github_api_token) }}"

- name: Copy GitHub Runner
copy:
src: "{{ operations_github_runner_path }}/"
dest: "{{ item_runner_path }}"
owner: "{{ item_runner_user }}"
group: "{{ item_runner_user }}"
remote_src: yes

# See https://docs.github.com/en/rest/actions/self-hosted-runners?apiVersion=2022-11-28#create-configuration-for-a-just-in-time-runner-for-an-organization--fine-grained-access-tokens
- name: "{{ item.repo_name }} | Get GitHub Runner Token"
- name: "{{ item_runner_repo }} | Get GitHub Runner Token"
shell: |
curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer {{ item.api_token | default(operations_github_api_token) }}" \
-H "Authorization: Bearer {{ item_runner_api_token }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/{{item.repo_name}}/actions/runners/registration-token \
https://api.github.com/repos/{{ item_runner_repo }}/actions/runners/registration-token \
register: result
failed_when:
- "'token' not in result.stdout"
Expand All @@ -23,29 +48,25 @@
failed_when:
- "'token' not in github_runner_registration_token"

- name: "{{ item.repo_name }} | Remove existing GitHub Runner"
- name: "{{ item_runner_repo }} | Remove existing GitHub Runner"
shell:
cmd: "./svc.sh uninstall"
chdir: "{{ operations_github_runner_path }}"
chdir: "{{ item_runner_path }}"
become: true
ignore_errors: true

- name: "{{ item.repo_name }} | Setup GitHub Runner"
- name: "{{ item_runner_repo }} | Setup GitHub Runner"
shell:
cmd: "cd {{ operations_github_runner_path }} && ./config.sh remove --token {{ github_runner_registration_token.token }} && ./config.sh --replace --unattended --url {{ item.repo_host | default('https://github.com') }}/{{ item.repo_name }} --token {{ github_runner_registration_token.token }} --name {{ item.runner_name|default(inventory_hostname) }} --labels {{ item.runner_labels | default('{{ inventory_hostname }},operations-project/site-runner') }}"
chdir: "{{ operations_github_runner_path }}"
cmd: "./config.sh remove --token {{ github_runner_registration_token.token }} && ./config.sh --replace --unattended --url {{ item.repo_host | default('https://github.com') }}/{{ item_runner_repo }} --token {{ github_runner_registration_token.token }} --name {{ item_runner_name }} --labels {{ item_runner_labels }}"
chdir: "{{ item_runner_path }}"
become: true
become_user: "{{ item.runner_user | default(operations_platform_user) | default('platform')}}"
become_user: "{{ item_runner_user }}"

- name: "{{ item.repo_name }} | Setup GitHub Runner Service"
- name: "Setup GitHub Runner Service"
shell:
cmd: "./svc.sh install && ./svc start"
chdir: "{{ operations_github_runner_path }}"
cmd: |
whoami
./svc.sh install {{ item_runner_user }}
./svc.sh start
chdir: "{{ item_runner_path }}"
become: true

#- name: Add GitHub Runner Service
# shell:
# cmd: "cd {{ operations_github_runner_path }} && ./config.sh remove --token {{ item.runner_token }} && ./config.sh --unattended --url {{ item.repo_url }} --token {{ item.runner_token }} --name {{ item.runner_name }} --labels {{ item.runner_labels }}"
# chdir: "{{ operations_github_runner_path }}"
# become: true
# become_user: "{{ item.user }}"
# with_items: "{{ operations_github_runners }}"
12 changes: 5 additions & 7 deletions roles/operations.runner/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
---

# See Your repo > Settings > Actions > Runner > New Runner
- name: Install GitHub Runner Software
- name: Download the Runner
shell:
cmd: |
cd {{ operations_github_runner_path }}
mkdir {{ operations_github_runner_path }} -p
curl -o {{ operations_github_runner_path }}/actions-runner-linux-x64-2.314.1.tar.gz -L https://github.com/actions/runner/releases/download/v2.314.1/actions-runner-linux-x64-2.314.1.tar.gz
cd {{ operations_github_runner_path }} && tar xzf {{ operations_github_runner_path }}/actions-runner-linux-x64-2.314.1.tar.gz
cd {{ operations_github_runner_path }} && ./bin/installdependencies.sh
chown {{ operations_platform_user }}:{{ operations_platform_user }} {{ operations_github_runner_path }} -R
cd {{ operations_github_runner_path }} && ./bin/installdependencies.sh
ls -la {{ operations_github_runner_path }}
creates: "{{ operations_github_runner_path }}"
tar xzf {{ operations_github_runner_path }}/actions-runner-linux-x64-2.314.1.tar.gz
./bin/installdependencies.sh
creates: "{{ operations_github_runner_path }}/config.sh"

- name: Configure GitHub Runners
include_tasks: github.yml
Expand Down

0 comments on commit 6659d00

Please sign in to comment.