Skip to content

Commit

Permalink
feat(packer): add vars and minor clean up (philips-labs#1611)
Browse files Browse the repository at this point in the history
* feat(packer): add security group id

* fix: /opt requires sudo rights

* feat: add subnet and instance variables

* fix: run script as sudo + add tags

* feat: add configuration for root volume

Co-authored-by: toast-gear <[email protected]>
  • Loading branch information
toast-gear and toast-gear authored Jan 14, 2022
1 parent c9c7c69 commit 1c897a4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
79 changes: 63 additions & 16 deletions images/linux-amzn2/github_agent.linux.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ packer {
}
}

variable "action_runner_url" {
description = "The URL to the tarball of the action runner"
variable "runner_version" {
description = "The version (no v prefix) of the runner software to install https://github.com/actions/runner/releases"
type = string
default = "https://github.com/actions/runner/releases/download/v2.284.0/actions-runner-linux-x64-2.284.0.tar.gz"
default = "2.286.0"
}

variable "region" {
Expand All @@ -19,10 +19,41 @@ variable "region" {
default = "eu-west-1"
}

variable "security_group_id" {
description = "The ID of the security group Packer will associate with the builder to enable access"
type = string
default = null
}

variable "subnet_id" {
description = "If using VPC, the ID of the subnet, such as subnet-12345def, where Packer will launch the EC2 instance. This field is required if you are using an non-default VPC"
type = string
default = null
}

variable "instance_type" {
description = "The instance type Packer will use for the builder"
type = string
default = "m3.medium"
}

variable "root_volume_size_gb" {
type = number
default = 8
}

variable "tags" {
description = "Additional tags to add globally"
type = map(string)
default = {}
}

source "amazon-ebs" "githubrunner" {
ami_name = "github-runner-amzn2-x86_64-${formatdate("YYYYMMDDhhmm", timestamp())}"
instance_type = "m3.medium"
region = var.region
ami_name = "github-runner-amzn2-x86_64-${formatdate("YYYYMMDDhhmm", timestamp())}"
instance_type = var.instance_type
region = var.region
security_group_id = var.security_group_id
subnet_id = var.subnet_id
source_ami_filter {
filters = {
name = "amzn2-ami-hvm-2.*-x86_64-ebs"
Expand All @@ -33,10 +64,18 @@ source "amazon-ebs" "githubrunner" {
owners = ["137112412989"]
}
ssh_username = "ec2-user"
tags = {
OS_Version = "amzn2"
Release = "Latest"
Base_AMI_Name = "{{ .SourceAMIName }}"
tags = merge(
var.tags,
{
OS_Version = "amzn2"
Release = "Latest"
Base_AMI_Name = "{{ .SourceAMIName }}"
})

launch_block_device_mappings {
device_name = "/dev/xvda"
volume_size = "${var.root_volume_size_gb}"
volume_type = "gp3"
}
}

Expand All @@ -58,16 +97,24 @@ build {
]
}

provisioner "shell" {
environment_vars = [
"RUNNER_TARBALL_URL=${var.action_runner_url}"
]
inline = [templatefile("../install-runner.sh", {
provisioner "file" {
content = templatefile("../install-runner.sh", {
install_runner = templatefile("../../modules/runners/templates/install-runner.sh", {
ARM_PATCH = ""
S3_LOCATION_RUNNER_DISTRIBUTION = ""
})
})]
})
destination = "/tmp/install-runner.sh"
}

provisioner "shell" {
environment_vars = [
"RUNNER_TARBALL_URL=https://github.com/actions/runner/releases/download/v${var.runner_version}/actions-runner-linux-x64-${var.runner_version}.tar.gz"
]
inline = [
"sudo chmod +x /tmp/install-runner.sh",
"sudo RUNNER_TARBALL_URL=$RUNNER_TARBALL_URL /tmp/install-runner.sh"
]
}

provisioner "file" {
Expand Down
2 changes: 1 addition & 1 deletion modules/runners/templates/install-runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ file_name="actions-runner.tar.gz"

echo "Creating actions-runner directory for the GH Action installtion"
cd /opt/
mkdir actions-runner && cd actions-runner
mkdir -p actions-runner && cd actions-runner

if [[ -n "$RUNNER_TARBALL_URL" ]]; then
echo "Downloading the GH Action runner from $RUNNER_TARBALL_URL to $file_name"
Expand Down

0 comments on commit 1c897a4

Please sign in to comment.