Skip to content

Commit

Permalink
feat: Natively support runner job started/completed hooks (philips-la…
Browse files Browse the repository at this point in the history
…bs#4260)

Pre and post job hooks were added to github actions to help
administrators run custom scripts at the beginning and end of every job.
As of today the module doesn't support these options out of the box.

Add variables to accept these optional scripts and register the hook in
user-data.

Related to: philips-labs#3854
  • Loading branch information
winwinashwin committed Nov 16, 2024
1 parent cf6dd34 commit 232e1a7
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ module "runners" {
userdata_content = var.userdata_content
userdata_pre_install = var.userdata_pre_install
userdata_post_install = var.userdata_post_install
runner_hook_job_started = var.runner_hook_job_started
runner_hook_job_completed = var.runner_hook_job_completed
key_name = var.key_name
runner_ec2_tags = var.runner_ec2_tags

Expand Down
16 changes: 9 additions & 7 deletions modules/multi-runner/runners.tf
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ module "runners" {
role_path = var.role_path
role_permissions_boundary = var.role_permissions_boundary
enable_userdata = each.value.runner_config.enable_userdata
userdata_template = each.value.runner_config.userdata_template
userdata_content = each.value.runner_config.userdata_content
userdata_pre_install = each.value.runner_config.userdata_pre_install
userdata_post_install = each.value.runner_config.userdata_post_install
key_name = var.key_name
runner_ec2_tags = each.value.runner_config.runner_ec2_tags
enable_userdata = each.value.runner_config.enable_userdata
userdata_template = each.value.runner_config.userdata_template
userdata_content = each.value.runner_config.userdata_content
userdata_pre_install = each.value.runner_config.userdata_pre_install
userdata_post_install = each.value.runner_config.userdata_post_install
runner_hook_job_started = each.value.runner_config.runner_hook_job_started
runner_hook_job_completed = each.value.runner_config.runner_hook_job_completed
key_name = var.key_name
runner_ec2_tags = each.value.runner_config.runner_ec2_tags
create_service_linked_role_spot = each.value.runner_config.create_service_linked_role_spot
Expand Down
2 changes: 2 additions & 0 deletions modules/multi-runner/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ variable "multi_runner_config" {
cloudwatch_config = optional(string, null)
userdata_pre_install = optional(string, "")
userdata_post_install = optional(string, "")
runner_hook_job_started = optional(string, "")
runner_hook_job_completed = optional(string, "")
runner_ec2_tags = optional(map(string), {})
runner_iam_role_managed_policy_arns = optional(list(string), [])
vpc_id = optional(string, null)
Expand Down
4 changes: 3 additions & 1 deletion modules/runners/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ locals {
S3_LOCATION_RUNNER_DISTRIBUTION = local.s3_location_runner_distribution
RUNNER_ARCHITECTURE = var.runner_architecture
})
post_install = var.userdata_post_install
post_install = var.userdata_post_install
hook_job_started = var.runner_hook_job_started
hook_job_completed = var.runner_hook_job_completed
start_runner = templatefile(local.userdata_start_runner[var.runner_os], {
metadata_tags = var.metadata_options != null ? var.metadata_options.instance_metadata_tags : "enabled"
})
Expand Down
16 changes: 16 additions & 0 deletions modules/runners/templates/user-data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,20 @@ ${install_runner}

${post_install}

# Register runner job hooks
# Ref: https://github.com/actions/runner/blob/main/docs/adrs/1751-runner-job-hooks.md
%{ if hook_job_started != "" }
cat > /opt/actions-runner/hook_job_started.sh << EOF
${hook_job_started}
EOF
echo ACTIONS_RUNNER_HOOK_JOB_STARTED=/opt/actions-runner/hook_job_started.sh | tee -a /opt/actions-runner/.env
%{ endif }

%{ if hook_job_completed != "" }
cat > /opt/actions-runner/hook_job_completed.sh << EOF
${hook_job_completed}
EOF
echo ACTIONS_RUNNER_HOOK_JOB_COMPLETED=/opt/actions-runner/hook_job_completed.sh | tee -a /opt/actions-runner/.env
%{ endif }

${start_runner}
12 changes: 12 additions & 0 deletions modules/runners/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ variable "userdata_post_install" {
default = ""
}

variable "runner_hook_job_started" {
description = "Script to be ran in the runner environment at the beginning of every job"
type = string
default = ""
}

variable "runner_hook_job_completed" {
description = "Script to be ran in the runner environment at the end of every job"
type = string
default = ""
}

variable "sqs_build_queue" {
description = "SQS queue to consume accepted build events."
type = object({
Expand Down
12 changes: 12 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,18 @@ variable "userdata_post_install" {
description = "Script to be ran after the GitHub Actions runner is installed on the EC2 instances"
}

variable "runner_hook_job_started" {
type = string
default = ""
description = "Script to be ran in the runner environment at the beginning of every job"
}

variable "runner_hook_job_completed" {
type = string
default = ""
description = "Script to be ran in the runner environment at the end of every job"
}

variable "idle_config" {
description = "List of time periods, defined as a cron expression, to keep a minimum amount of runners active instead of scaling down to 0. By defining this list you can ensure that in time periods that match the cron expression within 5 seconds a runner is kept idle."
type = list(object({
Expand Down

0 comments on commit 232e1a7

Please sign in to comment.