|
| 1 | +packer { |
| 2 | + required_plugins { |
| 3 | + amazon = { |
| 4 | + version = ">= 0.0.2" |
| 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 "instance_type" { |
| 22 | + description = "The instance type Packer will use for the builder" |
| 23 | + type = string |
| 24 | + default = "m4.xlarge" |
| 25 | +} |
| 26 | + |
| 27 | +variable "iam_instance_profile" { |
| 28 | + description = "IAM instance profile Packer will use for the builder. An empty string (default) means no profile will be assigned." |
| 29 | + type = string |
| 30 | + default = "" |
| 31 | +} |
| 32 | + |
| 33 | +variable "security_group_id" { |
| 34 | + description = "The ID of the security group Packer will associate with the builder to enable access" |
| 35 | + type = string |
| 36 | + default = null |
| 37 | +} |
| 38 | + |
| 39 | +variable "subnet_id" { |
| 40 | + 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" |
| 41 | + type = string |
| 42 | + default = null |
| 43 | +} |
| 44 | + |
| 45 | +variable "root_volume_size_gb" { |
| 46 | + type = number |
| 47 | + default = 30 |
| 48 | +} |
| 49 | + |
| 50 | +variable "ebs_delete_on_termination" { |
| 51 | + description = "Indicates whether the EBS volume is deleted on instance termination." |
| 52 | + type = bool |
| 53 | + default = true |
| 54 | +} |
| 55 | + |
| 56 | +variable "associate_public_ip_address" { |
| 57 | + 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" |
| 58 | + type = string |
| 59 | + default = null |
| 60 | +} |
| 61 | + |
| 62 | +variable "custom_shell_commands" { |
| 63 | + description = "Additional commands to run on the EC2 instance, to customize the instance, like installing packages" |
| 64 | + type = list(string) |
| 65 | + default = [] |
| 66 | +} |
| 67 | + |
| 68 | +variable "temporary_security_group_source_public_ip" { |
| 69 | + 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." |
| 70 | + type = bool |
| 71 | + default = false |
| 72 | +} |
| 73 | + |
| 74 | +data "http" github_runner_release_json { |
| 75 | + url = "https://api.github.com/repos/actions/runner/releases/latest" |
| 76 | + request_headers = { |
| 77 | + Accept = "application/vnd.github+json" |
| 78 | + X-GitHub-Api-Version : "2022-11-28" |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +locals { |
| 83 | + runner_version = coalesce(var.runner_version, trimprefix(jsondecode(data.http.github_runner_release_json.body).tag_name, "v")) |
| 84 | +} |
| 85 | + |
| 86 | +source "amazon-ebs" "githubrunner" { |
| 87 | + ami_name = "github-runner-windows-core-2025-${formatdate("YYYYMMDDhhmm", timestamp())}" |
| 88 | + communicator = "winrm" |
| 89 | + instance_type = var.instance_type |
| 90 | + iam_instance_profile = var.iam_instance_profile |
| 91 | + region = var.region |
| 92 | + security_group_id = var.security_group_id |
| 93 | + subnet_id = var.subnet_id |
| 94 | + associate_public_ip_address = var.associate_public_ip_address |
| 95 | + temporary_security_group_source_public_ip = var.temporary_security_group_source_public_ip |
| 96 | + |
| 97 | + source_ami_filter { |
| 98 | + filters = { |
| 99 | + name = "Windows_Server-2025-English-Full-ECS_Optimized-*" |
| 100 | + root-device-type = "ebs" |
| 101 | + virtualization-type = "hvm" |
| 102 | + } |
| 103 | + most_recent = true |
| 104 | + owners = ["amazon"] |
| 105 | + } |
| 106 | + tags = { |
| 107 | + OS_Version = "windows-core-2025" |
| 108 | + Release = "Latest" |
| 109 | + Base_AMI_Name = "{{ .SourceAMIName }}" |
| 110 | + } |
| 111 | + user_data_file = "./bootstrap_win.ps1" |
| 112 | + winrm_insecure = true |
| 113 | + winrm_port = 5986 |
| 114 | + winrm_use_ssl = true |
| 115 | + winrm_username = "Administrator" |
| 116 | + |
| 117 | + launch_block_device_mappings { |
| 118 | + device_name = "/dev/sda1" |
| 119 | + volume_size = "${var.root_volume_size_gb}" |
| 120 | + delete_on_termination = "${var.ebs_delete_on_termination}" |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +build { |
| 125 | + name = "githubactions-runner" |
| 126 | + sources = [ |
| 127 | + "source.amazon-ebs.githubrunner" |
| 128 | + ] |
| 129 | + |
| 130 | + provisioner "file" { |
| 131 | + content = templatefile("../start-runner.ps1", { |
| 132 | + start_runner = templatefile("../../modules/runners/templates/start-runner.ps1", {}) |
| 133 | + }) |
| 134 | + destination = "C:\\start-runner.ps1" |
| 135 | + } |
| 136 | + |
| 137 | + provisioner "powershell" { |
| 138 | + inline = concat([ |
| 139 | + templatefile("./windows-provisioner.ps1", { |
| 140 | + action_runner_url = "https://github.com/actions/runner/releases/download/v${local.runner_version}/actions-runner-win-x64-${local.runner_version}.zip" |
| 141 | + }) |
| 142 | + ], var.custom_shell_commands) |
| 143 | + } |
| 144 | + post-processor "manifest" { |
| 145 | + output = "manifest.json" |
| 146 | + strip_path = true |
| 147 | + } |
| 148 | +} |
0 commit comments