Skip to content

Commit fff6e1f

Browse files
enhancement: custom GitHub runner name (#1)
* enhancement: improved readability * enhancement: added export for custom machine host This hostname is later on used to as github runner name * enhancement: removed machine os from machine name and added a naming scheme rule * fix: local variable reference to fill_char_length
1 parent 327aee7 commit fff6e1f

3 files changed

Lines changed: 27 additions & 8 deletions

File tree

main.tf

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,3 @@
22
provider "hcloud" {
33
token = var.hcloud_token
44
}
5-
6-
resource "random_uuid" "hetzner_machine" {
7-
}

scripts/remote/setup-runner.sh

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@ cd /srv/actions-runner
55
case "$GH_RUNNER_TYPE" in
66
"repo")
77
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;;
8+
./config.sh \
9+
--unattended \
10+
--url https://github.com/$GH_OWNER/$GH_NAME \
11+
--token $registration_token \
12+
--name $CUSTOM_HOSTNAME \
13+
--labels $GH_LABELS \
14+
--replace $GH_REPLACE_RUNNERS \
15+
--work _work;;
916
"org")
1017
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;;
18+
./config.sh \
19+
--unattended \
20+
--url https://github.com/$GH_OWNER \
21+
--token $registration_token \
22+
--name $CUSTOM_HOSTNAME \
23+
--labels $GH_LABELS \
24+
--replace $GH_REPLACE_RUNNERS \
25+
--work _work;;
1226
esac
1327

1428
sudo ./svc.sh install

servers.tf

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
locals {
2+
fill_char_length = 64-length("github-runner")-4
3+
}
4+
5+
resource "random_string" "hetzner_machine" {
6+
length = local.fill_char_length > 0 ? local.fill_char_length : 0
7+
special = false
8+
}
9+
110
resource "hcloud_server" "github_runner" {
211
count = var.github_actions_runner_count
3-
name = format("%s-%s-%s-%d", "github-runner", var.hetzner_machine_os, random_uuid.hetzner_machine.result, count.index + 1)
12+
name = format("%s-%s-%d", "github-runner", random_string.hetzner_machine.result, count.index + 1)
413
server_type = var.hetzner_machine_type
514
image = var.hetzner_machine_os
615
location = var.hetzner_machine_location
@@ -15,7 +24,6 @@ resource "hcloud_server" "github_runner" {
1524
provisioner "file" {
1625
source = "scripts/remote/setup-runner.sh"
1726
destination = "/srv/setup-runner.sh"
18-
1927
}
2028

2129
provisioner "file" {
@@ -44,7 +52,7 @@ resource "hcloud_server" "github_runner" {
4452
"chown -R github-runner /srv",
4553
"chmod +x /srv/setup-runner.sh /srv/gh-runner-cli",
4654
"mv /srv/actions-runner/run.sh /srv/actions-runner/run.sh.old",
47-
"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'"
55+
"su github-runner -c 'export CUSTOM_HOSTNAME=${self.name}; 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'"
4856
]
4957
}
5058
}

0 commit comments

Comments
 (0)