Skip to content

Commit 14ac0db

Browse files
committed
feat: Add hooks for prebuilt images (AMI), including amazon linux packer example (#1444)
* Initial creation of runner image * Refactored startup script and added it to the per-boot folder * Make the runner location a variable So we can pass the runner version in at packer build time if we want to update the runner version. * Retrieve external config setting via tags Retrieve the required config via the instance tags so we dont have to pass in and set environment on the instance in an awkward way. * Enable tag based config Give the instance the permission to query its own tags and set the correct tags on the instance. * Add a CI job * Fix the CI build * Fix the formatting * Retain user_data provisioning and remove duplication refactored to make sure user_data continues to work with minimal breaking changes. Use a single set of scripts shared between image and user_data provisioning. * Fix interpolation issues in template file * fix build * Fix formatting * minor tweaks and fixes * Fixes from testing * Enable docker on boot * Add in output of start time for the runner * Scoop up the runner log * Add a powershell build script for windows users * Fix formatting * Use SSM parameters for configuration Its best practice to use SSM parameters for configuration of the runners. In adding this i have also added parameter path based config so its easy to extend in the future. * Make the SSM policy more specific * Update .github/workflows/packer-build.yml Co-authored-by: Niek Palm <[email protected]> * Added condition to the describe tags policy * Dont use templatefile on the tags policy Because of the use of ${} in the policy terraform is trying to replace it. * Added an option to turn off userdata scripting * Added/updated documentation * Revert policy as it has no effect on the permissions * Add reference to prebuilt images in the main readme * Add an example of deploying with prebuilt images * Update readme * Use current user as ami_owner * Update example to 5 secs * Updated ami name to include the arch * Fixed log file variable * Added explicit info about required settings to the readme * Change userdata_enabled to enabled_userdata Keep within existing naming convention Co-authored-by: Niek Palm <[email protected]>
1 parent b6b4fa3 commit 14ac0db

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ Examples are located in the [examples](./examples) directory. The following exam
299299
- _[Ubuntu](examples/ubuntu/README.md)_: Example usage of creating a runner using Ubuntu AMIs.
300300
- _[Prebuilt Images](examples/prebuilt/README.md)_: Example usages of deploying runners with a custom prebuilt image.
301301
- _[Windows](examples/windows/README.md)_: Example usage of creating a runner using Windows as the OS.
302+
- _[Prebuilt Images](examples/prebuilt/README.md)_: Example usages of deploying runners with a custom prebuilt image.
302303

303304
## Sub modules
304305

@@ -340,34 +341,34 @@ In case the setup does not work as intended follow the trace of events:
340341
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
341342
## Requirements
342343

343-
| Name | Version |
344-
|------|---------|
344+
| Name | Version |
345+
| ------------------------------------------------------------------------- | --------- |
345346
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.14.1 |
346-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.38 |
347+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.38 |
347348

348349
## Providers
349350

350-
| Name | Version |
351-
|------|---------|
352-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.38 |
353-
| <a name="provider_random"></a> [random](#provider\_random) | n/a |
351+
| Name | Version |
352+
| ---------------------------------------------------------- | ------- |
353+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.38 |
354+
| <a name="provider_random"></a> [random](#provider\_random) | n/a |
354355

355356
## Modules
356357

357-
| Name | Source | Version |
358-
|------|--------|---------|
359-
| <a name="module_runner_binaries"></a> [runner\_binaries](#module\_runner\_binaries) | ./modules/runner-binaries-syncer | n/a |
360-
| <a name="module_runners"></a> [runners](#module\_runners) | ./modules/runners | n/a |
361-
| <a name="module_ssm"></a> [ssm](#module\_ssm) | ./modules/ssm | n/a |
362-
| <a name="module_webhook"></a> [webhook](#module\_webhook) | ./modules/webhook | n/a |
358+
| Name | Source | Version |
359+
| ----------------------------------------------------------------------------------- | -------------------------------- | ------- |
360+
| <a name="module_runner_binaries"></a> [runner\_binaries](#module\_runner\_binaries) | ./modules/runner-binaries-syncer | n/a |
361+
| <a name="module_runners"></a> [runners](#module\_runners) | ./modules/runners | n/a |
362+
| <a name="module_ssm"></a> [ssm](#module\_ssm) | ./modules/ssm | n/a |
363+
| <a name="module_webhook"></a> [webhook](#module\_webhook) | ./modules/webhook | n/a |
363364

364365
## Resources
365366

366-
| Name | Type |
367-
|------|------|
367+
| Name | Type |
368+
| ------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
368369
| [aws_resourcegroups_group.resourcegroups_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/resourcegroups_group) | resource |
369-
| [aws_sqs_queue.queued_builds](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue) | resource |
370-
| [random_string.random](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) | resource |
370+
| [aws_sqs_queue.queued_builds](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue) | resource |
371+
| [random_string.random](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) | resource |
371372

372373
## Inputs
373374

@@ -448,12 +449,12 @@ In case the setup does not work as intended follow the trace of events:
448449

449450
## Outputs
450451

451-
| Name | Description |
452-
|------|-------------|
453-
| <a name="output_binaries_syncer"></a> [binaries\_syncer](#output\_binaries\_syncer) | n/a |
454-
| <a name="output_runners"></a> [runners](#output\_runners) | n/a |
455-
| <a name="output_ssm_parameters"></a> [ssm\_parameters](#output\_ssm\_parameters) | n/a |
456-
| <a name="output_webhook"></a> [webhook](#output\_webhook) | n/a |
452+
| Name | Description |
453+
| ----------------------------------------------------------------------------------- | ----------- |
454+
| <a name="output_binaries_syncer"></a> [binaries\_syncer](#output\_binaries\_syncer) | n/a |
455+
| <a name="output_runners"></a> [runners](#output\_runners) | n/a |
456+
| <a name="output_ssm_parameters"></a> [ssm\_parameters](#output\_ssm\_parameters) | n/a |
457+
| <a name="output_webhook"></a> [webhook](#output\_webhook) | n/a |
457458
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
458459

459460
## Contribution

modules/runners/templates/start-runner.sh

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ echo "Retrieved /$environment/runner/enable-cloudwatch parameter - ($enable_clou
2929
agent_mode=$(echo "$parameters" | jq --arg environment "$environment" -r '.[] | select(.Name == "/\($environment)/runner/agent-mode") | .Value')
3030
echo "Retrieved /$environment/runner/agent-mode parameter - ($agent_mode)"
3131

32-
if [[ -n "$enable_cloudwatch_agent" ]]; then
33-
echo "Cloudwatch is enabled"
32+
if [[ -n "$enable_cloudwatch_agent" ]]; then
33+
echo "Cloudwatch is enabled"
3434
amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c "ssm:$environment-cloudwatch_agent_config_runner"
3535
fi
3636

37-
3837
## Configure the runner
3938

4039
echo "Get GH Runner config from AWS SSM"
@@ -66,18 +65,18 @@ sudo --preserve-env=RUNNER_ALLOW_RUNASROOT -u "$run_as" -- ./config.sh --unatten
6665
echo "Starting runner after $(awk '{print int($1/3600)":"int(($1%3600)/60)":"int($1%60)}' /proc/uptime)"
6766
echo "Starting the runner as user $run_as"
6867

69-
if [[ $agent_mode = "ephemeral" ]]; then
68+
if [[ $agent_mode = "ephemeral" ]]; then
7069
echo "Starting the runner in ephemeral mode"
7170
sudo --preserve-env=RUNNER_ALLOW_RUNASROOT -u "$run_as" -- ./run.sh
7271
echo "Runner has finished"
73-
72+
7473
echo "Stopping cloudwatch service"
7574
service awslogsd stop
7675
echo "Terminating instance"
7776
aws ec2 terminate-instances --instance-ids "$instance_id" --region "$region"
78-
else
77+
else
7978
echo "Installing the runner as a service"
8079
./svc.sh install "$run_as"
8180
echo "Starting the runner in persistent mode"
8281
./svc.sh start
83-
fi
82+
fi

0 commit comments

Comments
 (0)