Skip to content

Commit 7ae4223

Browse files
authored
Merge pull request #35 from hexa2k9/feature-add-labels-to-runner-instances
Add Labels to created runner Instances
2 parents db6ddea + c95023f commit 7ae4223

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

app/clients/gcloud_client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ def _get_template_name(self, template_name):
5454
except Exception:
5555
return None
5656

57-
def create_runner_instance(self, registration_token, repo_url, template_name):
57+
def create_runner_instance(self, registration_token, repo_url, template_name, instance_label=None):
5858
"""
5959
Create a new GCE instance for a GitHub Actions runner.
6060
6161
Args:
6262
registration_token (str): The GitHub Actions runner registration token.
6363
repo_url (str): The URL of the repository or organization.
6464
template_name (str): The name of the instance template to use.
65+
instance_label (str): Label to add to the Instance for Cost Tracking.
6566
6667
Returns:
6768
str: The name of the created instance.
@@ -86,6 +87,14 @@ def create_runner_instance(self, registration_token, repo_url, template_name):
8687
instance_resource = compute_v1.Instance() # google.cloud.compute_v1.types.Instance
8788
instance_resource.name = instance_name
8889

90+
if instance_label is not None:
91+
owner, repo = instance_label.split("/")
92+
instance_resource.labels = {
93+
"gha-owner": owner.lower(),
94+
"gha-repo": repo.lower(),
95+
"gha-runner": template_name
96+
}
97+
8998
# Set metadata (startup script) - use shlex.quote to prevent command injection
9099
startup_script = (
91100
f"sudo -u runner /actions-runner/config.sh --url {shlex.quote(repo_url)} "

app/services/webhook_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ def _handle_queued_job(self, template_name, repo_url, repo_owner_url, repo_name,
8282
if org_name:
8383
# Create GitHub Actions runner instance for organization
8484
token = self.github_client.get_registration_token(org_name=org_name)
85-
self.gcloud_client.create_runner_instance(token, repo_owner_url, template_name)
85+
self.gcloud_client.create_runner_instance(token, repo_owner_url, template_name, repo_name)
8686
elif repo_name:
8787
# Create GitHub Actions runner instance for repository
8888
token = self.github_client.get_registration_token(repo_name=repo_name)
89-
self.gcloud_client.create_runner_instance(token, repo_url, template_name)
89+
self.gcloud_client.create_runner_instance(token, repo_url, template_name, repo_name)
9090
else:
9191
logger.error("Neither repository nor organization found in payload. Ignoring job.")
9292
return

tests/unit/test_webhook_service.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ def test_handle_queued_job_with_matching_label(self, mock_gh_client_class, mock_
3434
mock_gc_client.create_runner_instance.assert_called_once_with(
3535
'fake-token',
3636
'https://github.com/owner/repo',
37-
'gcp-ubuntu-24.04'
37+
'gcp-ubuntu-24.04',
38+
'owner/repo'
3839
)
3940

4041
@patch('app.services.webhook_service.GCloudClient')

0 commit comments

Comments
 (0)