diff --git a/app/clients/gcloud_client.py b/app/clients/gcloud_client.py index 9b88062..43697ec 100644 --- a/app/clients/gcloud_client.py +++ b/app/clients/gcloud_client.py @@ -54,7 +54,7 @@ def _get_template_name(self, template_name): except Exception: return None - def create_runner_instance(self, registration_token, repo_url, template_name): + def create_runner_instance(self, registration_token, repo_url, template_name, instance_label=None): """ Create a new GCE instance for a GitHub Actions runner. @@ -62,6 +62,7 @@ def create_runner_instance(self, registration_token, repo_url, template_name): registration_token (str): The GitHub Actions runner registration token. repo_url (str): The URL of the repository or organization. template_name (str): The name of the instance template to use. + instance_label (str): Label to add to the Instance for Cost Tracking. Returns: str: The name of the created instance. @@ -81,6 +82,14 @@ def create_runner_instance(self, registration_token, repo_url, template_name): instance_resource = compute_v1.Instance() # google.cloud.compute_v1.types.Instance instance_resource.name = instance_name + if instance_label is not None: + owner, repo = instance_label.split("/") + instance_resource.labels = { + "gha-owner": owner.lower(), + "gha-repo": repo.lower(), + "gha-runner": template_name + } + # Set metadata (startup script) - use shlex.quote to prevent command injection startup_script = ( f"sudo -u runner /actions-runner/config.sh --url {shlex.quote(repo_url)} " diff --git a/app/services/webhook_service.py b/app/services/webhook_service.py index 1584c0f..cfe26be 100644 --- a/app/services/webhook_service.py +++ b/app/services/webhook_service.py @@ -82,11 +82,11 @@ def _handle_queued_job(self, template_name, repo_url, repo_owner_url, repo_name, if org_name: # Create GitHub Actions runner instance for organization token = self.github_client.get_registration_token(org_name=org_name) - self.gcloud_client.create_runner_instance(token, repo_owner_url, template_name) + self.gcloud_client.create_runner_instance(token, repo_owner_url, template_name, repo_name) elif repo_name: # Create GitHub Actions runner instance for repository token = self.github_client.get_registration_token(repo_name=repo_name) - self.gcloud_client.create_runner_instance(token, repo_url, template_name) + self.gcloud_client.create_runner_instance(token, repo_url, template_name, repo_name) else: logger.error("Neither repository nor organization found in payload. Ignoring job.") return