Skip to content

Commit

Permalink
feat: Add variable to specify custom commands while building the AMI (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
ulich authored Mar 10, 2022
1 parent 3f1a67f commit 8f9c342
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
10 changes: 8 additions & 2 deletions images/linux-amzn2/github_agent.linux.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -118,15 +124,15 @@ 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",
"sudo systemctl enable docker.service",
"sudo systemctl enable containerd.service",
"sudo service docker start",
"sudo usermod -a -G docker ec2-user",
]
], var.custom_shell_commands)
}

provisioner "file" {
Expand Down
10 changes: 8 additions & 2 deletions images/ubuntu-focal/github_agent.ubuntu.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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" {
Expand Down
14 changes: 11 additions & 3 deletions images/windows-core-2019/github_agent.windows.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 8f9c342

Please sign in to comment.