|
| 1 | +packer { |
| 2 | + required_plugins { |
| 3 | + amazon = { |
| 4 | + version = ">= 1.0.0" |
| 5 | + source = "github.com/hashicorp/amazon" |
| 6 | + } |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +variable "runner_version" { |
| 11 | + description = "The version (no v prefix) of the runner software to install https://github.com/actions/runner/releases. The latest release will be fetched from GitHub if not provided." |
| 12 | + default = null |
| 13 | +} |
| 14 | + |
| 15 | +variable "region" { |
| 16 | + description = "The region to build the image in" |
| 17 | + type = string |
| 18 | + default = "eu-west-1" |
| 19 | +} |
| 20 | + |
| 21 | +variable "security_group_id" { |
| 22 | + description = "The ID of the security group Packer will associate with the builder to enable access" |
| 23 | + type = string |
| 24 | + default = null |
| 25 | +} |
| 26 | + |
| 27 | +variable "subnet_id" { |
| 28 | + 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" |
| 29 | + type = string |
| 30 | + default = null |
| 31 | +} |
| 32 | + |
| 33 | +variable "associate_public_ip_address" { |
| 34 | + description = "If using a non-default VPC, there is no public IP address assigned to the EC2 instance. If you specified a public subnet, you probably want to set this to true. Otherwise the EC2 instance won't have access to the internet" |
| 35 | + type = string |
| 36 | + default = null |
| 37 | +} |
| 38 | + |
| 39 | +variable "instance_type" { |
| 40 | + description = "The instance type Packer will use for the builder" |
| 41 | + type = string |
| 42 | + default = "t4g.small" |
| 43 | +} |
| 44 | + |
| 45 | +variable "iam_instance_profile" { |
| 46 | + description = "IAM instance profile Packer will use for the builder. An empty string (default) means no profile will be assigned." |
| 47 | + type = string |
| 48 | + default = "" |
| 49 | +} |
| 50 | + |
| 51 | +variable "root_volume_size_gb" { |
| 52 | + type = number |
| 53 | + default = 8 |
| 54 | +} |
| 55 | + |
| 56 | +variable "ebs_delete_on_termination" { |
| 57 | + description = "Indicates whether the EBS volume is deleted on instance termination." |
| 58 | + type = bool |
| 59 | + default = true |
| 60 | +} |
| 61 | + |
| 62 | +variable "global_tags" { |
| 63 | + description = "Tags to apply to everything" |
| 64 | + type = map(string) |
| 65 | + default = {} |
| 66 | +} |
| 67 | + |
| 68 | +variable "ami_tags" { |
| 69 | + description = "Tags to apply to the AMI" |
| 70 | + type = map(string) |
| 71 | + default = {} |
| 72 | +} |
| 73 | + |
| 74 | +variable "snapshot_tags" { |
| 75 | + description = "Tags to apply to the snapshot" |
| 76 | + type = map(string) |
| 77 | + default = {} |
| 78 | +} |
| 79 | + |
| 80 | +variable "custom_shell_commands" { |
| 81 | + description = "Additional commands to run on the EC2 instance, to customize the instance, like installing packages" |
| 82 | + type = list(string) |
| 83 | + default = [] |
| 84 | +} |
| 85 | + |
| 86 | +variable "temporary_security_group_source_public_ip" { |
| 87 | + description = "When enabled, use public IP of the host (obtained from https://checkip.amazonaws.com) as CIDR block to be authorized access to the instance, when packer is creating a temporary security group. Note: If you specify `security_group_id` then this input is ignored." |
| 88 | + type = bool |
| 89 | + default = false |
| 90 | +} |
| 91 | + |
| 92 | +data "http" github_runner_release_json { |
| 93 | + url = "https://api.github.com/repos/actions/runner/releases/latest" |
| 94 | + request_headers = { |
| 95 | + Accept = "application/vnd.github+json" |
| 96 | + X-GitHub-Api-Version : "2022-11-28" |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +locals { |
| 101 | + runner_version = coalesce(var.runner_version, trimprefix(jsondecode(data.http.github_runner_release_json.body).tag_name, "v")) |
| 102 | +} |
| 103 | + |
| 104 | +source "amazon-ebs" "githubrunner" { |
| 105 | + ami_name = "github-runner-al2023-arm64-${formatdate("YYYYMMDDhhmm", timestamp())}" |
| 106 | + instance_type = var.instance_type |
| 107 | + iam_instance_profile = var.iam_instance_profile |
| 108 | + region = var.region |
| 109 | + security_group_id = var.security_group_id |
| 110 | + subnet_id = var.subnet_id |
| 111 | + associate_public_ip_address = var.associate_public_ip_address |
| 112 | + temporary_security_group_source_public_ip = var.temporary_security_group_source_public_ip |
| 113 | + |
| 114 | + source_ami_filter { |
| 115 | + filters = { |
| 116 | + name = "al2023-ami-2023.*-kernel-6.*-arm64" |
| 117 | + root-device-type = "ebs" |
| 118 | + virtualization-type = "hvm" |
| 119 | + } |
| 120 | + most_recent = true |
| 121 | + owners = ["137112412989"] |
| 122 | + } |
| 123 | + ssh_username = "ec2-user" |
| 124 | + tags = merge( |
| 125 | + var.global_tags, |
| 126 | + var.ami_tags, |
| 127 | + { |
| 128 | + OS_Version = "al2023" |
| 129 | + Release = "Latest" |
| 130 | + Base_AMI_Name = "{{ .SourceAMIName }}" |
| 131 | + }) |
| 132 | + snapshot_tags = merge( |
| 133 | + var.global_tags, |
| 134 | + var.snapshot_tags, |
| 135 | + ) |
| 136 | + |
| 137 | + |
| 138 | + launch_block_device_mappings { |
| 139 | + device_name = "/dev/xvda" |
| 140 | + volume_size = "${var.root_volume_size_gb}" |
| 141 | + volume_type = "gp3" |
| 142 | + delete_on_termination = "${var.ebs_delete_on_termination}" |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +build { |
| 147 | + name = "githubactions-runner" |
| 148 | + sources = [ |
| 149 | + "source.amazon-ebs.githubrunner" |
| 150 | + ] |
| 151 | + provisioner "shell" { |
| 152 | + environment_vars = [] |
| 153 | + inline = concat([ |
| 154 | + "sudo dnf upgrade-minimal -y", |
| 155 | + "sudo dnf install -y amazon-cloudwatch-agent jq git docker", |
| 156 | + "sudo dnf install -y --allowerasing curl", |
| 157 | + "sudo systemctl enable docker.service", |
| 158 | + "sudo systemctl enable containerd.service", |
| 159 | + "sudo service docker start", |
| 160 | + "sudo usermod -a -G docker ec2-user", |
| 161 | + ], var.custom_shell_commands) |
| 162 | + } |
| 163 | + |
| 164 | + provisioner "file" { |
| 165 | + content = templatefile("../install-runner.sh", { |
| 166 | + install_runner = templatefile("../../modules/runners/templates/install-runner.sh", { |
| 167 | + ARM_PATCH = "" |
| 168 | + S3_LOCATION_RUNNER_DISTRIBUTION = "" |
| 169 | + RUNNER_ARCHITECTURE = "arm64" |
| 170 | + }) |
| 171 | + }) |
| 172 | + destination = "/tmp/install-runner.sh" |
| 173 | + } |
| 174 | + |
| 175 | + provisioner "shell" { |
| 176 | + environment_vars = [ |
| 177 | + "RUNNER_TARBALL_URL=https://github.com/actions/runner/releases/download/v${local.runner_version}/actions-runner-linux-arm64-${local.runner_version}.tar.gz" |
| 178 | + ] |
| 179 | + inline = [ |
| 180 | + "sudo chmod +x /tmp/install-runner.sh", |
| 181 | + "echo ec2-user > /tmp/install-user.txt", |
| 182 | + "sudo RUNNER_ARCHITECTURE=arm64 RUNNER_TARBALL_URL=$RUNNER_TARBALL_URL /tmp/install-runner.sh" |
| 183 | + ] |
| 184 | + } |
| 185 | + |
| 186 | + provisioner "file" { |
| 187 | + content = templatefile("../start-runner.sh", { |
| 188 | + start_runner = templatefile("../../modules/runners/templates/start-runner.sh", { metadata_tags = "enabled" }) |
| 189 | + }) |
| 190 | + destination = "/tmp/start-runner.sh" |
| 191 | + } |
| 192 | + |
| 193 | + provisioner "shell" { |
| 194 | + inline = [ |
| 195 | + "sudo mv /tmp/start-runner.sh /var/lib/cloud/scripts/per-boot/start-runner.sh", |
| 196 | + "sudo chmod +x /var/lib/cloud/scripts/per-boot/start-runner.sh", |
| 197 | + ] |
| 198 | + } |
| 199 | + |
| 200 | + post-processor "manifest" { |
| 201 | + output = "manifest.json" |
| 202 | + strip_path = true |
| 203 | + } |
| 204 | +} |
0 commit comments