Skip to content

Commit ea4e042

Browse files
authored
feat(runners): add option to prefix registered runners in GitHub (#3043)
* feat: add support to prefix the runner update start scripts fix scripts refactor names and manage fixed tags via terraform * sync with main, and clenup * update docs * Only tag ec2 * set default prefix to empty string in case of an error * Add separator in example for prefix
1 parent dfd693f commit ea4e042

File tree

22 files changed

+68
-8
lines changed

22 files changed

+68
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ We welcome any improvement to the standard module to make the default as secure
524524
| <a name="input_runner_iam_role_managed_policy_arns"></a> [runner\_iam\_role\_managed\_policy\_arns](#input\_runner\_iam\_role\_managed\_policy\_arns) | Attach AWS or customer-managed IAM policies (by ARN) to the runner IAM role | `list(string)` | `[]` | no |
525525
| <a name="input_runner_log_files"></a> [runner\_log\_files](#input\_runner\_log\_files) | (optional) Replaces the module default cloudwatch log config. See https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html for details. | <pre>list(object({<br> log_group_name = string<br> prefix_log_group = bool<br> file_path = string<br> log_stream_name = string<br> }))</pre> | `null` | no |
526526
| <a name="input_runner_metadata_options"></a> [runner\_metadata\_options](#input\_runner\_metadata\_options) | Metadata options for the ec2 runner instances. By default, the module uses metadata tags for bootstrapping the runner, only disable `instance_metadata_tags` when using custom scripts for starting the runner. | `map(any)` | <pre>{<br> "http_endpoint": "enabled",<br> "http_put_response_hop_limit": 1,<br> "http_tokens": "optional",<br> "instance_metadata_tags": "enabled"<br>}</pre> | no |
527+
| <a name="input_runner_name_prefix"></a> [runner\_name\_prefix](#input\_runner\_name\_prefix) | The prefix used for the GitHub runner name. The prefix will be used in the default start script to prefix the instance name when register the runner in GitHub. The value is availabe via an EC2 tag 'ghr:runner\_name\_prefix'. | `string` | `""` | no |
527528
| <a name="input_runner_os"></a> [runner\_os](#input\_runner\_os) | The EC2 Operating System type to use for action runner instances (linux,windows). | `string` | `"linux"` | no |
528529
| <a name="input_runner_run_as"></a> [runner\_run\_as](#input\_runner\_run\_as) | Run the GitHub actions agent as user. | `string` | `"ec2-user"` | no |
529530
| <a name="input_runners_lambda_s3_key"></a> [runners\_lambda\_s3\_key](#input\_runners\_lambda\_s3\_key) | S3 key for runners lambda function. Required if using S3 bucket to specify lambdas. | `string` | `null` | no |

examples/default/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,7 @@ module "runners" {
8787
# enable_workflow_job_events_queue = true
8888

8989
enable_user_data_debug_logging_runner = true
90+
91+
# prefix GitHub runners with the environment name
92+
runner_name_prefix = "${local.environment}_"
9093
}

examples/multi-runner/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module "multi-runner" {
2525
runner_os = "linux"
2626
runner_architecture = "arm64"
2727
runner_extra_labels = "amazon"
28+
runner_name_prefix = "amazon-arm64_"
2829
enable_ssm_on_runners = true
2930
instance_types = ["t4g.large", "c6g.large"]
3031
runners_maximum_count = 1
@@ -47,6 +48,7 @@ module "multi-runner" {
4748
runner_architecture = "x64"
4849
runner_extra_labels = "ubuntu-latest,ubuntu-2204"
4950
runner_run_as = "ubuntu"
51+
runner_name_prefix = "ubuntu-2204-x64_"
5052
enable_ssm_on_runners = true
5153
instance_types = ["m5ad.large", "m5a.large"]
5254
runners_maximum_count = 1
@@ -101,6 +103,7 @@ module "multi-runner" {
101103
runner_config = {
102104
runner_os = "windows"
103105
runner_architecture = "x64"
106+
runner_name_prefix = "servercore-2022-x64_"
104107
enable_ssm_on_runners = true
105108
instance_types = ["m5.large", "c5.large"]
106109
runner_extra_labels = "servercore-2022"
@@ -129,6 +132,7 @@ module "multi-runner" {
129132
}
130133
runner_os = "linux"
131134
runner_architecture = "x64"
135+
runner_name_prefix = "amazon-x64_"
132136
create_service_linked_role_spot = true
133137
enable_ssm_on_runners = true
134138
instance_types = ["m5ad.large", "m5a.large"]

main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ module "runners" {
242242
cloudwatch_config = var.cloudwatch_config
243243
runner_log_files = var.runner_log_files
244244
runner_group_name = var.runner_group_name
245+
runner_name_prefix = var.runner_name_prefix
245246

246247
scale_up_reserved_concurrent_executions = var.scale_up_reserved_concurrent_executions
247248

modules/multi-runner/README.md

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

modules/multi-runner/runners.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ module "runners" {
6969
cloudwatch_config = var.cloudwatch_config
7070
runner_log_files = each.value.runner_config.runner_log_files
7171
runner_group_name = each.value.runner_config.runner_group_name
72+
runner_name_prefix = each.value.runner_config.runner_name_prefix
7273

7374
scale_up_reserved_concurrent_executions = each.value.runner_config.scale_up_reserved_concurrent_executions
7475

modules/multi-runner/variables.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ variable "multi_runner_config" {
6060
runner_boot_time_in_minutes = optional(number, 5)
6161
runner_extra_labels = string
6262
runner_group_name = optional(string, "Default")
63+
runner_name_prefix = optional(string, "")
6364
runner_run_as = optional(string, "ec2-user")
6465
runners_maximum_count = number
6566
scale_down_schedule_expression = optional(string, "cron(*/5 * * * ? *)")
@@ -150,6 +151,7 @@ variable "multi_runner_config" {
150151
runner_boot_time_in_minutes: "The minimum time for an EC2 runner to boot and register as a runner."
151152
runner_extra_labels: "Extra (custom) labels for the runners (GitHub). Separate each label by a comma. Labels checks on the webhook can be enforced by setting `enable_workflow_job_labels_check`. GitHub read-only labels should not be provided."
152153
runner_group_name: "Name of the runner group."
154+
runner_name_prefix: "Prefix for the GitHub runner name."
153155
runner_run_as: "Run the GitHub actions agent as user."
154156
runners_maximum_count: "The maximum number of runners that will be created."
155157
scale_down_schedule_expression: "Scheduler expression to check every x for scale down."

modules/runners/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ yarn run dist
185185
| <a name="input_runner_group_name"></a> [runner\_group\_name](#input\_runner\_group\_name) | Name of the runner group. | `string` | `"Default"` | no |
186186
| <a name="input_runner_iam_role_managed_policy_arns"></a> [runner\_iam\_role\_managed\_policy\_arns](#input\_runner\_iam\_role\_managed\_policy\_arns) | Attach AWS or customer-managed IAM policies (by ARN) to the runner IAM role | `list(string)` | `[]` | no |
187187
| <a name="input_runner_log_files"></a> [runner\_log\_files](#input\_runner\_log\_files) | (optional) List of logfiles to send to CloudWatch, will only be used if `enable_cloudwatch_agent` is set to true. Object description: `log_group_name`: Name of the log group, `prefix_log_group`: If true, the log group name will be prefixed with `/github-self-hosted-runners/<var.prefix>`, `file_path`: path to the log file, `log_stream_name`: name of the log stream. | <pre>list(object({<br> log_group_name = string<br> prefix_log_group = bool<br> file_path = string<br> log_stream_name = string<br> }))</pre> | `null` | no |
188+
| <a name="input_runner_name_prefix"></a> [runner\_name\_prefix](#input\_runner\_name\_prefix) | The prefix used for the GitHub runner name. The prefix will be used in the default start script to prefix the instance name when register the runner in GitHub. The value is availabe via an EC2 tag 'ghr:runner\_name\_prefix'. | `string` | `""` | no |
188189
| <a name="input_runner_os"></a> [runner\_os](#input\_runner\_os) | The EC2 Operating System type to use for action runner instances (linux,windows). | `string` | `"linux"` | no |
189190
| <a name="input_runner_run_as"></a> [runner\_run\_as](#input\_runner\_run\_as) | Run the GitHub actions agent as user. | `string` | `"ec2-user"` | no |
190191
| <a name="input_runners_lambda_s3_key"></a> [runners\_lambda\_s3\_key](#input\_runners\_lambda\_s3\_key) | S3 key for runners lambda function. Required if using S3 bucket to specify lambdas. | `string` | `null` | no |
@@ -218,4 +219,5 @@ yarn run dist
218219
| <a name="output_role_runner"></a> [role\_runner](#output\_role\_runner) | n/a |
219220
| <a name="output_role_scale_down"></a> [role\_scale\_down](#output\_role\_scale\_down) | n/a |
220221
| <a name="output_role_scale_up"></a> [role\_scale\_up](#output\_role\_scale\_up) | n/a |
222+
| <a name="output_runners_log_groups"></a> [runners\_log\_groups](#output\_runners\_log\_groups) | List of log groups from different log files of runner machine. |
221223
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

modules/runners/lambdas/runners/src/aws/runners.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const ORG_NAME = 'SomeAwesomeCoder';
1616
const REPO_NAME = `${ORG_NAME}/some-amazing-library`;
1717
const ENVIRONMENT = 'unit-test-environment';
1818
const SSM_TOKEN_PATH = '/github-action-runners/default/runners/tokens';
19+
const RUNNER_NAME_PREFIX = '';
1920

2021
const mockDescribeInstances = { promise: jest.fn() };
2122
mockEC2.describeInstances.mockImplementation(() => mockDescribeInstances);
@@ -28,6 +29,8 @@ const mockRunningInstances: AWS.EC2.DescribeInstancesResult = {
2829
InstanceId: 'i-1234',
2930
Tags: [
3031
{ Key: 'ghr:Application', Value: 'github-action-runner' },
32+
{ Key: 'ghr:runner_name_prefix', Value: RUNNER_NAME_PREFIX },
33+
{ Key: 'ghr:created_by', Value: 'scale-up-lambda' },
3134
{ Key: 'Type', Value: 'Org' },
3235
{ Key: 'Owner', Value: 'CoderToCat' },
3336
],
@@ -523,6 +526,7 @@ function expectedCreateFleetRequest(expectedValues: ExpectedFleetRequestValues):
523526
ResourceType: 'instance',
524527
Tags: [
525528
{ Key: 'ghr:Application', Value: 'github-action-runner' },
529+
{ Key: 'ghr:created_by', Value: expectedValues.totalTargetCapacity > 1 ? 'pool-lambda' : 'scale-up-lambda' },
526530
{ Key: 'Type', Value: expectedValues.type },
527531
{ Key: 'Owner', Value: REPO_NAME },
528532
],

modules/runners/lambdas/runners/src/aws/runners.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ export async function createRunner(runnerParameters: RunnerInputParameters): Pro
214214
ResourceType: 'instance',
215215
Tags: [
216216
{ Key: 'ghr:Application', Value: 'github-action-runner' },
217+
{ Key: 'ghr:created_by', Value: numberOfRunners === 1 ? 'scale-up-lambda' : 'pool-lambda' },
217218
{ Key: 'Type', Value: runnerParameters.runnerType },
218219
{ Key: 'Owner', Value: runnerParameters.runnerOwner },
219220
],

0 commit comments

Comments
 (0)