Skip to content

Commit 7704ec3

Browse files
feat: Adding runner_enable_default_labels variable
1 parent 26ad5c1 commit 7704ec3

File tree

7 files changed

+43
-3
lines changed

7 files changed

+43
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ Talk to the forestkeepers in the `runners-channel` on Slack.
209209
| <a name="input_runner_credit_specification"></a> [runner\_credit\_specification](#input\_runner\_credit\_specification) | The credit option for CPU usage of a T instance. Can be unset, "standard" or "unlimited". | `string` | `null` | no |
210210
| <a name="input_runner_ec2_tags"></a> [runner\_ec2\_tags](#input\_runner\_ec2\_tags) | Map of tags that will be added to the launch template instance tag specifications. | `map(string)` | `{}` | no |
211211
| <a name="input_runner_egress_rules"></a> [runner\_egress\_rules](#input\_runner\_egress\_rules) | List of egress rules for the GitHub runner instances. | <pre>list(object({<br/> cidr_blocks = list(string)<br/> ipv6_cidr_blocks = list(string)<br/> prefix_list_ids = list(string)<br/> from_port = number<br/> protocol = string<br/> security_groups = list(string)<br/> self = bool<br/> to_port = number<br/> description = string<br/> }))</pre> | <pre>[<br/> {<br/> "cidr_blocks": [<br/> "0.0.0.0/0"<br/> ],<br/> "description": null,<br/> "from_port": 0,<br/> "ipv6_cidr_blocks": [<br/> "::/0"<br/> ],<br/> "prefix_list_ids": null,<br/> "protocol": "-1",<br/> "security_groups": null,<br/> "self": null,<br/> "to_port": 0<br/> }<br/>]</pre> | no |
212+
| <a name="input_runner_enable_default_labels"></a> [runner\_enable\_default\_labels](#input\_runner\_enable\_default\_labels) | Enable default labels for the runners (os, architecture and `self-hosted`). If disabled, the runner will only have the extra labels provided in `runner_extra_labels`. | `bool` | `true` | no |
212213
| <a name="input_runner_extra_labels"></a> [runner\_extra\_labels](#input\_runner\_extra\_labels) | Extra (custom) labels for the runners (GitHub). Labels checks on the webhook can be enforced by setting `enable_runner_workflow_job_labels_check_all`. GitHub read-only labels should not be provided. | `list(string)` | `[]` | no |
213214
| <a name="input_runner_group_name"></a> [runner\_group\_name](#input\_runner\_group\_name) | Name of the runner group. | `string` | `"Default"` | no |
214215
| <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 |

main.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ locals {
88
key_base64 = module.ssm.parameters.github_app_key_base64
99
}
1010

11-
runner_labels = sort(distinct(concat(["self-hosted", var.runner_os, var.runner_architecture], var.runner_extra_labels)))
11+
default_runner_labels = distinct(concat(["self-hosted", var.runner_os, var.runner_architecture]))
12+
runner_labels = (var.runner_enable_default_labels == true) ? concat(local.default_runner_labels, var.runner_extra_labels) : var.runner_extra_labels
1213

1314
ssm_root_path = var.ssm_paths.use_prefix ? "/${var.ssm_paths.root}/${var.prefix}" : "/${var.ssm_paths.root}"
1415
}
@@ -199,6 +200,7 @@ module "runners" {
199200
scale_down_schedule_expression = var.scale_down_schedule_expression
200201
minimum_running_time_in_minutes = var.minimum_running_time_in_minutes
201202
runner_boot_time_in_minutes = var.runner_boot_time_in_minutes
203+
runner_enable_default_labels = var.runner_enable_default_labels
202204
runner_labels = local.runner_labels
203205
runner_as_root = var.runner_as_root
204206
runner_run_as = var.runner_run_as

modules/runners/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ yarn run dist
202202
| <a name="input_runner_ec2_tags"></a> [runner\_ec2\_tags](#input\_runner\_ec2\_tags) | Map of tags that will be added to the launch template instance tag specifications. | `map(string)` | `{}` | no |
203203
| <a name="input_runner_group_name"></a> [runner\_group\_name](#input\_runner\_group\_name) | Name of the runner group. | `string` | `"Default"` | no |
204204
| <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 |
205+
| <a name="input_runner_enable_default_labels"></a> [runner\_enable\_default\_labels](#input\_runner\_enable\_default\_labels) | Enable default labels for the runners (os, architecture and `self-hosted`). If disabled, the runner will only have the extra labels provided in `runner_extra_labels`. | `bool` | `true` | no |
205206
| <a name="input_runner_labels"></a> [runner\_labels](#input\_runner\_labels) | All the labels for the runners (GitHub) including the default one's(e.g: self-hosted, linux, x64, label1, label2). Separate each label by a comma | `list(string)` | n/a | yes |
206207
| <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 |
207208
| <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 |

modules/runners/runner-config.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ resource "aws_ssm_parameter" "runner_agent_mode" {
1212
tags = local.tags
1313
}
1414

15+
resource "aws_ssm_parameter" "default_labels" {
16+
name = "${var.ssm_paths.root}/${var.ssm_paths.config}/default_labels"
17+
type = "String"
18+
value = var.runner_enable_default_labels
19+
tags = local.tags
20+
}
21+
1522
resource "aws_ssm_parameter" "jit_config_enabled" {
1623
name = "${var.ssm_paths.root}/${var.ssm_paths.config}/enable_jit_config"
1724
type = "String"

modules/runners/templates/start-runner.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ echo "Retrieved /$ssm_config_path/enable_cloudwatch parameter - ($enable_cloudwa
141141
agent_mode=$(echo "$parameters" | jq --arg ssm_config_path "$ssm_config_path" -r '.[] | select(.Name == "'$ssm_config_path'/agent_mode") | .Value')
142142
echo "Retrieved /$ssm_config_path/agent_mode parameter - ($agent_mode)"
143143

144+
default_labels=$(echo "$parameters" | jq --arg ssm_config_path "$ssm_config_path" -r '.[] | select(.Name == "'$ssm_config_path'/default_labels") | .Value')
145+
echo "Retrieved /$ssm_config_path/default_labels parameter - ($default_labels)"
146+
144147
enable_jit_config=$(echo "$parameters" | jq --arg ssm_config_path "$ssm_config_path" -r '.[] | select(.Name == "'$ssm_config_path'/enable_jit_config") | .Value')
145148
echo "Retrieved /$ssm_config_path/enable_jit_config parameter - ($enable_jit_config)"
146149

@@ -216,7 +219,11 @@ echo "Starting the runner as user $run_as"
216219
# configure the runner if the runner is non ephemeral or jit config is disabled
217220
if [[ "$enable_jit_config" == "false" || $agent_mode != "ephemeral" ]]; then
218221
echo "Configure GH Runner as user $run_as"
219-
sudo --preserve-env=RUNNER_ALLOW_RUNASROOT -u "$run_as" -- ./config.sh --unattended --name "$runner_name_prefix$instance_id" --work "_work" $${config}
222+
if [[ "$default_labels" == "true" ]]; then
223+
sudo --preserve-env=RUNNER_ALLOW_RUNASROOT -u "$run_as" -- ./config.sh --unattended --name "$runner_name_prefix$instance_id" --work "_work" $${config}
224+
else
225+
sudo --preserve-env=RUNNER_ALLOW_RUNASROOT -u "$run_as" -- ./config.sh --unattended --no-default-labels --name "$runner_name_prefix$instance_id" --work "_work" $${config}
226+
fi
220227
fi
221228

222229
create_xray_success_segment "$SEGMENT"

modules/runners/variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,20 @@ variable "runner_boot_time_in_minutes" {
217217
default = 5
218218
}
219219

220+
variable "runner_enable_default_labels" {
221+
description = "Enable default labels for the runners (os, architecture and `self-hosted`). If disabled, the runner will only have the extra labels provided in `runner_extra_labels`."
222+
type = bool
223+
default = true
224+
}
225+
220226
variable "runner_labels" {
221227
description = "All the labels for the runners (GitHub) including the default one's(e.g: self-hosted, linux, x64, label1, label2). Separate each label by a comma"
222228
type = list(string)
229+
230+
validation {
231+
condition = var.runner_labels != null && var.runner_labels != []
232+
error_message = "The runner_labels variable must be set."
233+
}
223234
}
224235

225236
variable "runner_group_name" {

variables.tf

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,21 @@ variable "runner_boot_time_in_minutes" {
5858
default = 5
5959
}
6060

61+
variable "runner_enable_default_labels" {
62+
description = "Enable default labels for the runners (os, architecture and `self-hosted`). If disabled, the runner will only have the extra labels provided in `runner_extra_labels`. In order set runner_enable_default_labels = false, enable_jit_config and enable_ephemeral_runners must be set to true; otherwise the value of this variable will be considered as true"
63+
type = bool
64+
default = true
65+
}
66+
6167
variable "runner_extra_labels" {
62-
description = "Extra (custom) labels for the runners (GitHub). Labels checks on the webhook can be enforced by setting `enable_runner_workflow_job_labels_check_all`. GitHub read-only labels should not be provided."
68+
description = "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."
6369
type = list(string)
6470
default = []
71+
72+
validation {
73+
condition = var.runner_extra_labels != null
74+
error_message = "Extra labels should not be null."
75+
}
6576
}
6677

6778
variable "runner_group_name" {

0 commit comments

Comments
 (0)