Skip to content

Commit b9b227e

Browse files
author
rohit-ng
committed
feat: add support to fetch github token
1 parent b9a7065 commit b9b227e

File tree

4 files changed

+60
-24
lines changed

4 files changed

+60
-24
lines changed

modules/github-runner/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ No modules.
2424
| Name | Description | Type | Default | Required |
2525
|------|-------------|------|---------|:--------:|
2626
| <a name="input_ami_id"></a> [ami\_id](#input\_ami\_id) | AMI ID for the private instances | `string` | `"ami-0f58b397bc5c1f2e8"` | no |
27-
| <a name="input_github_config_token"></a> [github\_config\_token](#input\_github\_config\_token) | Github config token for self-hosted runners | `string` | n/a | yes |
28-
| <a name="input_github_config_url"></a> [github\_config\_url](#input\_github\_config\_url) | Github config url for self-hosted runners | `string` | n/a | yes |
29-
| <a name="input_key_name"></a> [key\_name](#input\_key\_name) | The name of the EC2 key pair | `string` | `"runner"` | no |
27+
| <a name="input_github_org"></a> [github\_org](#input\_github\_org) | The name of github organization | `string` | n/a | yes |
28+
| <a name="input_github_repo"></a> [github\_repo](#input\_github\_repo) | The name of github repository | `string` | n/a | yes |
29+
| <a name="input_github_token"></a> [github\_token](#input\_github\_token) | Personal Access Token for github | `string` | n/a | yes |
30+
| <a name="input_key_name"></a> [key\_name](#input\_key\_name) | The name of the EC2 key pair | `string` | `null` | no |
3031
| <a name="input_private_subnet_id"></a> [private\_subnet\_id](#input\_private\_subnet\_id) | The ID of the private subnet | `string` | n/a | yes |
3132
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | The ID of the VPC | `string` | n/a | yes |
3233

modules/github-runner/main.tf

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ resource "aws_instance" "github_runner" {
55
vpc_security_group_ids = [aws_security_group.github_runner.id]
66
key_name = var.key_name
77
user_data = templatefile("${path.module}/scripts/self-hosted-runner.sh", {
8-
CONFIG_TOKEN = var.github_config_token
9-
CONFIG_URL = var.github_config_url
8+
GITHUB_ORG = var.github_org
9+
GITHUB_REPO = var.github_repo
10+
GITHUB_PAT = var.github_token
1011
})
12+
13+
lifecycle {
14+
create_before_destroy = true
15+
}
16+
1117
tags = {
1218
Name = local.ubuntu_instance_name
1319
}

modules/github-runner/scripts/self-hosted-runner.sh

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
#!/bin/bash
22

3-
# Define multiple runner configurations
4-
# declare -A RUNNERS=(
5-
# ["runner-1"]="https://github.com/actions/runner/releases/download/v2.317.0/actions-runner-linux-x64-2.317.0.tar.gz"
6-
# )
7-
83
DEFAULT_USER="githubrunner"
94
USER_HOME="/home/$DEFAULT_USER"
105
USER_PASSWORD="password"
@@ -73,38 +68,66 @@ download_and_extract_runner() {
7368
sudo chown -R $DEFAULT_USER:$DEFAULT_USER "$USER_HOME/$RUNNER_NAME/actions-runner" || die "Failed to set ownership for $USER_HOME/$RUNNER_NAME/actions-runner."
7469
}
7570

71+
# Function to fetch GitHub Actions runner registration token
72+
fetch_runner_token() {
73+
local response
74+
response=$(curl -s -X POST -H "Authorization: token ${GITHUB_PAT}" "https://api.github.com/repos/${GITHUB_ORG}/${GITHUB_REPO}/actions/runners/registration-token")
75+
echo $(echo "$response" | jq -r .token)
76+
}
77+
7678
# Function to configure and start the runner
7779
configure_and_start_runner() {
7880
local RUNNER_NAME="$1"
81+
local RUNNER_TOKEN
82+
RUNNER_TOKEN=$(fetch_runner_token) || die "Failed to fetch GitHub Actions runner registration token."
7983

8084
sudo -u $DEFAULT_USER -i <<EOF
8185
cd "$USER_HOME/$RUNNER_NAME/actions-runner" || exit 1
8286
83-
./config.sh --url "${CONFIG_URL}" \
84-
--token "${CONFIG_TOKEN}" \
87+
./config.sh --url "https://github.com/${GITHUB_ORG}/${GITHUB_REPO}" \
88+
--token "$RUNNER_TOKEN" \
8589
--name "$RUNNER_NAME" \
8690
--runnergroup "Default" \
8791
--work "_work" \
8892
--labels "self-hosted,Linux,X64,$RUNNER_NAME" \
8993
--unattended \
9094
--replace || { echo "Failed to configure GitHub Actions runner"; exit 1; }
9195
92-
nohup ./run.sh > runner.log 2>&1 &
93-
if [ \$? -ne 0 ]; then
94-
echo "Failed to start GitHub Actions runner $RUNNER_NAME"
95-
exit 1
96-
fi
97-
9896
echo "GitHub Actions runner setup for $RUNNER_NAME completed successfully."
9997
echo "The runner is running in the background. Check runner.log for output."
10098
EOF
99+
100+
# Create systemd service
101+
sudo tee /etc/systemd/system/github-runner.service >/dev/null <<EOL
102+
[Unit]
103+
Description=GitHub Actions Runner
104+
After=network.target
105+
106+
[Service]
107+
User=$DEFAULT_USER
108+
WorkingDirectory=$USER_HOME/$RUNNER_NAME/actions-runner
109+
ExecStart=$USER_HOME/$RUNNER_NAME/actions-runner/run.sh
110+
Restart=always
111+
RestartSec=10
112+
113+
[Install]
114+
WantedBy=multi-user.target
115+
EOL
116+
117+
# Reload systemd and start the service
118+
sudo systemctl daemon-reload
119+
sudo systemctl enable github-runner
120+
sudo systemctl start github-runner
121+
122+
echo "Systemd service for GitHub Actions runner $RUNNER_NAME created and started."
101123
}
102124

103125
# Main script
104126
main() {
105-
local RUNNER_NAME="runner"
127+
local RUNNER_NAME="deck"
106128
# Install required packages if not already installed
107129
command_exists curl || install_packages curl
130+
install_packages curl jq
108131

109132
# Ensure default user exists and has necessary permissions (no longer creating new users)
110133
sudo useradd -m -s /bin/bash $DEFAULT_USER 2>/dev/null || true

modules/github-runner/variables.tf

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,23 @@ variable "private_subnet_id" {
1717
variable "key_name" {
1818
description = "The name of the EC2 key pair"
1919
type = string
20-
default = "runner"
20+
default = null
2121
}
2222

23-
variable "github_config_url" {
24-
description = "Github config url for self-hosted runners"
23+
variable "github_org" {
24+
description = "The name of github organization"
2525
type = string
2626
sensitive = true
2727
}
2828

29-
variable "github_config_token" {
30-
description = "Github config token for self-hosted runners"
29+
variable "github_repo" {
30+
description = "The name of github repository"
31+
type = string
32+
sensitive = true
33+
}
34+
35+
variable "github_token" {
36+
description = "Personal Access Token for github"
3137
type = string
3238
sensitive = true
3339
}

0 commit comments

Comments
 (0)