From 8f9c34236adc74e4ccb46a06bdd4d946a2bee9a7 Mon Sep 17 00:00:00 2001 From: Timo Ulich Date: Thu, 10 Mar 2022 13:55:21 +0100 Subject: [PATCH] feat: Add variable to specify custom commands while building the AMI (#1838) Add ability to specify custom commands while building the AMI This can be used to install additional software or to customize anything else for the final AMI --- images/linux-amzn2/github_agent.linux.pkr.hcl | 10 ++++++++-- images/ubuntu-focal/github_agent.ubuntu.pkr.hcl | 10 ++++++++-- .../windows-core-2019/github_agent.windows.pkr.hcl | 14 +++++++++++--- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/images/linux-amzn2/github_agent.linux.pkr.hcl b/images/linux-amzn2/github_agent.linux.pkr.hcl index f23f3e82c5..2c7a92f04d 100644 --- a/images/linux-amzn2/github_agent.linux.pkr.hcl +++ b/images/linux-amzn2/github_agent.linux.pkr.hcl @@ -72,6 +72,12 @@ variable "snapshot_tags" { default = {} } +variable "custom_shell_commands" { + description = "Additional commands to run on the EC2 instance, to customize the instance, like installing packages" + type = list(string) + default = [] +} + source "amazon-ebs" "githubrunner" { ami_name = "github-runner-amzn2-x86_64-${formatdate("YYYYMMDDhhmm", timestamp())}" instance_type = var.instance_type @@ -118,7 +124,7 @@ build { ] provisioner "shell" { environment_vars = [] - inline = [ + inline = concat([ "sudo yum update -y", "sudo yum install -y amazon-cloudwatch-agent curl jq git", "sudo amazon-linux-extras install docker", @@ -126,7 +132,7 @@ build { "sudo systemctl enable containerd.service", "sudo service docker start", "sudo usermod -a -G docker ec2-user", - ] + ], var.custom_shell_commands) } provisioner "file" { diff --git a/images/ubuntu-focal/github_agent.ubuntu.pkr.hcl b/images/ubuntu-focal/github_agent.ubuntu.pkr.hcl index 112152f680..fccfeba2d7 100644 --- a/images/ubuntu-focal/github_agent.ubuntu.pkr.hcl +++ b/images/ubuntu-focal/github_agent.ubuntu.pkr.hcl @@ -72,6 +72,12 @@ variable "snapshot_tags" { default = {} } +variable "custom_shell_commands" { + description = "Additional commands to run on the EC2 instance, to customize the instance, like installing packages" + type = list(string) + default = [] +} + source "amazon-ebs" "githubrunner" { ami_name = "github-runner-ubuntu-focal-amd64-${formatdate("YYYYMMDDhhmm", timestamp())}" instance_type = var.instance_type @@ -120,7 +126,7 @@ build { environment_vars = [ "DEBIAN_FRONTEND=noninteractive" ] - inline = [ + inline = concat([ "sudo apt-get -y update", "sudo apt-get -y install ca-certificates curl gnupg lsb-release", "sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg", @@ -136,7 +142,7 @@ build { "sudo curl -f https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip -o awscliv2.zip", "unzip awscliv2.zip", "sudo ./aws/install", - ] + ], var.custom_shell_commands) } provisioner "file" { diff --git a/images/windows-core-2019/github_agent.windows.pkr.hcl b/images/windows-core-2019/github_agent.windows.pkr.hcl index 0eb7240e5b..4f8c792534 100644 --- a/images/windows-core-2019/github_agent.windows.pkr.hcl +++ b/images/windows-core-2019/github_agent.windows.pkr.hcl @@ -31,6 +31,12 @@ variable "associate_public_ip_address" { default = null } +variable "custom_shell_commands" { + description = "Additional commands to run on the EC2 instance, to customize the instance, like installing packages" + type = list(string) + default = [] +} + source "amazon-ebs" "githubrunner" { ami_name = "github-runner-windows-core-2019-${formatdate("YYYYMMDDhhmm", timestamp())}" communicator = "winrm" @@ -78,8 +84,10 @@ build { } provisioner "powershell" { - inline = [templatefile("./windows-provisioner.ps1", { - action_runner_url = var.action_runner_url - })] + inline = concat([ + templatefile("./windows-provisioner.ps1", { + action_runner_url = var.action_runner_url + }) + ], var.custom_shell_commands) } }