Skip to content

Commit 5829023

Browse files
Implemented environment teardown
1 parent e096c0e commit 5829023

5 files changed

Lines changed: 68 additions & 19 deletions

File tree

scripts/local/destroy_runner.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
machine_names=$1
4+
github_user=$2
5+
github_user_token=$3
6+
github_repo_name=$4
7+
github_repo_owner=$5
8+
9+
array=(`echo $machine_names | sed 's/,/\n/g'`)
10+
for i in "${array[@]}"
11+
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+
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/gh-runner-cli

8.48 MB
Binary file not shown.

scripts/remote/setup-runner.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
cd /srv/actions-runner
44

5-
./config.sh --unattended --url $1 --token $2 --name $(hostname) --labels $3 --replace $4 --work _work
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
68

79
sudo ./svc.sh install
8-
sudo ./svc.sh start
10+
sudo ./svc.sh start

servers.tf

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ resource "hcloud_server" "github_runner" {
1111
type = "ssh"
1212
private_key = file(var.ssh_private_key)
1313
}
14-
1514
provisioner "file" {
1615
source = "scripts/remote/setup-runner.sh"
1716
destination = "/srv/setup-runner.sh"
17+
18+
}
19+
20+
provisioner "file" {
21+
source = "scripts/remote/gh-runner-cli"
22+
destination = "/srv/gh-runner-cli"
1823
}
1924

2025
provisioner "remote-exec" {
@@ -33,12 +38,27 @@ resource "hcloud_server" "github_runner" {
3338
"curl -o actions-runner-linux-x64-2.277.1.tar.gz -L https://github.com/actions/runner/releases/download/v2.277.1/actions-runner-linux-x64-2.277.1.tar.gz",
3439
"tar xzf ./actions-runner-linux-x64-2.277.1.tar.gz",
3540
"adduser github-runner --disabled-login --gecos ''",
36-
"chown -R github-runner. /home/github-runner/.ssh",
3741
"usermod -aG docker github-runner",
3842
"echo 'github-runner ALL=(ALL:ALL)NOPASSWD:ALL' > /etc/sudoers.d/github-runner",
3943
"chown -R github-runner /srv",
40-
"chmod +x /srv/setup-runner.sh",
41-
"su github-runner -c '/srv/setup-runner.sh ${var.github_actions_provision_url} ${var.github_actions_provision_token} ${var.github_actions_runner_labels} ${var.github_actions_runner_replace_existing}'"
44+
"chmod +x /srv/setup-runner.sh /srv/gh-runner-cli",
45+
"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}'"
4246
]
4347
}
4448
}
49+
50+
resource "null_resource" "deprovision" {
51+
triggers = {
52+
machine_names = join(",", tolist(hcloud_server.github_runner.*.name))
53+
github_user = var.github_authentication_user
54+
github_user_token = var.github_authentication_token
55+
github_repo_name = var.github_repository_name
56+
github_repo_owner = var.github_repository_owner
57+
}
58+
59+
provisioner "local-exec" {
60+
when = destroy
61+
#command = "./scripts/remote/gh-runner-cli repo runner destroy-by-name --username ${self.triggers.github_user} --token ${self.triggers.github_user_token} --name ${self.triggers.github_repo_name} --owner ${self.triggers.github_repo_owner} --runner-name ${each.value.name}"
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+
}
64+
}

variables.tf

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,14 @@ variable "hetzner_machine_os" {
4040
type = string
4141
}
4242

43-
variable "github_actions_provision_url" {
44-
description = "Defines the github actions target url. Can be a specific repository or company"
45-
default = ""
46-
type = string
47-
}
48-
49-
variable "github_actions_provision_token" {
50-
description = "Defines the github actions self hosted runner registration token"
51-
default = ""
52-
type = string
53-
}
54-
5543
variable "github_actions_runner_count" {
5644
description = "Defines the amount of runners that should be provisioned"
5745
default = 1
5846
type = number
5947
}
6048

49+
//
50+
6151
variable "github_actions_runner_labels" {
6252
description = "Defines a list of labels used to identify the runner. The list is a simple string seperated by ','"
6353
default = ""
@@ -68,4 +58,24 @@ variable "github_actions_runner_replace_existing" {
6858
description = "Defines if existing runners should be destroyed"
6959
default = false
7060
type = bool
71-
}
61+
}
62+
63+
variable "github_repository_owner" {
64+
description = "Defines the repository owner"
65+
type = string
66+
}
67+
68+
variable "github_repository_name" {
69+
description = "Defines the repository name"
70+
type = string
71+
}
72+
73+
variable "github_authentication_user" {
74+
description = "Defines the authentication username"
75+
type = string
76+
}
77+
78+
variable "github_authentication_token" {
79+
description = "Defines the authentication personal access token"
80+
type = string
81+
}

0 commit comments

Comments
 (0)