Skip to content

Commit e397b3a

Browse files
Improved handling for organisation runners
Added option to switch between organisation and repository runners Some variables have been renamed to correspond to the task for which they are intended.
1 parent f865a50 commit e397b3a

4 files changed

Lines changed: 58 additions & 29 deletions

File tree

scripts/local/destroy_runner.sh

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,25 @@ machine_names=$1
44
github_user=$2
55
github_user_token=$3
66
github_repo_name=$4
7-
github_repo_owner=$5
7+
github_owner=$5
8+
github_runner_type=$6
89

910
array=(`echo $machine_names | sed 's/,/\n/g'`)
1011
for i in "${array[@]}"
1112
do
12-
./scripts/remote/gh-runner-cli repo runner destroy-by-name --username="$github_user" --token="$github_user_token" --owner="$github_repo_owner" --name="$github_repo_name" --runner-name "$i"
13+
case "$github_runner_type" in
14+
"repo")
15+
./scripts/remote/gh-runner-cli repo runner destroy-by-name \
16+
--username="$github_user" \
17+
--token="$github_user_token" \
18+
--owner="$github_owner" \
19+
--name="$github_repo_name" \
20+
--runner-name "$i";;
21+
"org")
22+
./scripts/remote/gh-runner-cli org runner destroy-by-name \
23+
--username "$github_user" \
24+
--token "$github_user_token" \
25+
--name "$github_owner" \
26+
--runner-name "$i";;
27+
esac
1328
done
14-
15-
#runner_id=$(../gh-runner-cli repo runner id-by-name --username=$1 --token=$2 --owner=$3 --name=$4 --runner-name=$5)
16-
17-
#../gh-runner-cli repo runner destroy --username=$1 --token=$2 --owner=$3 --name=$4

scripts/remote/setup-runner.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
cd /srv/actions-runner
44

5-
registration_token=$(../gh-runner-cli repo runner provision-token --username=$1 --token=$2 --owner=$3 --name=$4)
6-
7-
./config.sh --unattended --url https://github.com/$3/$4 --token $registration_token --name $(hostname) --labels $5 --replace $6 --work _work
5+
case "$GH_RUNNER_TYPE" in
6+
"repo")
7+
registration_token=$(../gh-runner-cli repo runner provision-token --username=$GH_USERNAME --token=$GH_TOKEN --owner=$GH_OWNER --name=$GH_NAME)
8+
./config.sh --unattended --url https://github.com/$GH_OWNER/$GH_NAME --token $registration_token --name $(hostname) --labels $GH_LABELS --replace $GH_REPLACE_RUNNERS --work _work;;
9+
"org")
10+
registration_token=$(../gh-runner-cli org runner provision-token --username=$GH_USERNAME --token=$GH_TOKEN --name=$GH_OWNER)
11+
./config.sh --unattended --url https://github.com/$GH_OWNER --token $registration_token --name $(hostname) --labels $GH_LABELS --replace $GH_REPLACE_RUNNERS --work _work;;
12+
esac
813

914
sudo ./svc.sh install
1015
sudo ./svc.sh start

servers.tf

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ resource "hcloud_server" "github_runner" {
33
name = format("%s-%s-%s-%d", "github-runner", var.hetzner_machine_os, random_uuid.hetzner_machine.result, count.index + 1)
44
server_type = var.hetzner_machine_type
55
image = var.hetzner_machine_os
6-
ssh_keys = concat([hcloud_ssh_key.admin_ssh_key.id], var.additional_public_key_ids)
6+
ssh_keys = concat([hcloud_ssh_key.admin_ssh_key.id], var.hetzner_additional_public_key_ids)
77

88
connection {
99
host = self.ipv4_address
@@ -26,7 +26,7 @@ resource "hcloud_server" "github_runner" {
2626
inline = [
2727
"apt-get update -y",
2828
"DEBIAN_FRONTEND=noninteractive apt-get upgrade -y",
29-
"DEBIAN_FRONTEND=noninteractive apt-get install sudo git vim tmux apt-transport-https ca-certificates curl gnupg lsb-release gcc build-essential ffmpeg imagemagick sqlite3 libopenjp2-tools libopenjp2-7 libopenjp2-7-dev rsync make pkg-config exiftool ghostscript xsltproc gnupg2 pass -y",
29+
"DEBIAN_FRONTEND=noninteractive apt-get install sudo git vim tmux apt-transport-https ca-certificates curl gnupg lsb-release pass ${var.hetzner_machine_additional_packages} -y",
3030
"echo '127.0.0.1 fylr-server-postgres fylr-server-sqlite execserver minio2 postgres2 elasticsearch2' >> /etc/hosts",
3131
"curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg",
3232
"echo 'deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian buster stable' | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null",
@@ -43,7 +43,7 @@ resource "hcloud_server" "github_runner" {
4343
"chown -R github-runner /srv",
4444
"chmod +x /srv/setup-runner.sh /srv/gh-runner-cli",
4545
"mv /srv/actions-runner/run.sh /srv/actions-runner/run.sh.old",
46-
"su github-runner -c '/srv/setup-runner.sh ${var.github_authentication_user} ${var.github_authentication_token} ${var.github_repository_owner} ${var.github_repository_name} ${var.github_actions_runner_labels} ${var.github_actions_runner_replace_existing}'"
46+
"su github-runner -c 'export GH_USERNAME=${var.github_authentication_user}; export GH_TOKEN=${var.github_authentication_token}; export GH_OWNER=${var.github_owner}; export GH_NAME=${var.github_repository_name}; export GH_LABELS=${var.github_actions_runner_labels}; export GH_REPLACE_RUNNERS=${var.github_actions_runner_replace_existing}; export GH_RUNNER_TYPE=${var.github_runner_type}; /srv/setup-runner.sh'"
4747
]
4848
}
4949
}
@@ -54,11 +54,12 @@ resource "null_resource" "deprovision" {
5454
github_user = var.github_authentication_user
5555
github_user_token = var.github_authentication_token
5656
github_repo_name = var.github_repository_name
57-
github_repo_owner = var.github_repository_owner
57+
github_repo_owner = var.github_owner
58+
github_runner_type = var.github_runner_type
5859
}
5960

6061
provisioner "local-exec" {
6162
when = destroy
62-
command = "./scripts/local/destroy_runner.sh ${self.triggers.machine_names} ${self.triggers.github_user} ${self.triggers.github_user_token} ${self.triggers.github_repo_name} ${self.triggers.github_repo_owner}"
63+
command = "./scripts/local/destroy_runner.sh ${self.triggers.machine_names} ${self.triggers.github_user} ${self.triggers.github_user_token} ${self.triggers.github_repo_name} ${self.triggers.github_repo_owner} ${self.triggers.github_runner_type}"
6364
}
6465
}

variables.tf

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,48 +46,60 @@ variable "hetzner_machine_os" {
4646
type = string
4747
}
4848

49+
variable hetzner_additional_public_key_ids {
50+
description = "Adds public keys to the server that are already registered at hetzner"
51+
default = []
52+
type = list(string)
53+
}
54+
55+
variable hetzner_machine_additional_packages {
56+
description = "Defines additional packages that must be installed on the machine."
57+
default = ""
58+
type = string
59+
}
60+
61+
//
62+
4963
variable "github_actions_runner_count" {
50-
description = "Defines the amount of runners that should be provisioned"
64+
description = "Defines the number of runners to be provided. This option is equal to Machines at hetzner."
5165
default = 1
5266
type = number
5367
}
5468

55-
//
56-
5769
variable "github_actions_runner_labels" {
58-
description = "Defines a list of labels used to identify the runner. The list is a simple string seperated by ','"
70+
description = "Defines a list of labels used to identify the runners. The list is divided by separating the individual entries with `,`."
5971
default = ""
6072
type = string
6173
}
6274

6375
variable "github_actions_runner_replace_existing" {
64-
description = "Defines if existing runners should be destroyed"
76+
description = "Specifies whether to replace existing Github action runners with the same name."
6577
default = false
6678
type = bool
6779
}
6880

69-
variable "github_repository_owner" {
70-
description = "Defines the repository owner"
81+
variable "github_owner" {
82+
description = "Defines the organisation name or repository owner."
7183
type = string
7284
}
7385

7486
variable "github_repository_name" {
75-
description = "Defines the repository name"
87+
description = "Sets the name of the repository. This option is only used if you use self-hosted Github runners at the repository level."
7688
type = string
7789
}
7890

7991
variable "github_authentication_user" {
80-
description = "Defines the authentication username"
92+
description = "Sets the user used for issuing new registration tokens. Ensure that the user has the appropriate permissions. "
8193
type = string
8294
}
8395

8496
variable "github_authentication_token" {
85-
description = "Defines the authentication personal access token"
97+
description = " Sets the personal access token for the configured user in the variable github_authentication_user."
8698
type = string
8799
}
88100

89-
variable additional_public_key_ids {
90-
description = "Adds public keys to the server that are already registered at hetzner"
91-
default = []
92-
type = list(string)
93-
}
101+
variable "github_runner_type" {
102+
description = "Defines the github runner type. Available values are: repo, org"
103+
default = "repo"
104+
type = string
105+
}

0 commit comments

Comments
 (0)