|
| 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" |
| 12 | + type = string |
| 13 | + default = "2.294.0" |
| 14 | +} |
| 15 | + |
| 16 | +variable "region" { |
| 17 | + description = "The region to build the image in" |
| 18 | + type = string |
| 19 | + default = "eu-west-1" |
| 20 | +} |
| 21 | + |
| 22 | +variable "security_group_id" { |
| 23 | + description = "The ID of the security group Packer will associate with the builder to enable access" |
| 24 | + type = string |
| 25 | + default = null |
| 26 | +} |
| 27 | + |
| 28 | +variable "subnet_id" { |
| 29 | + 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" |
| 30 | + type = string |
| 31 | + default = null |
| 32 | +} |
| 33 | + |
| 34 | +variable "associate_public_ip_address" { |
| 35 | + 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" |
| 36 | + type = string |
| 37 | + default = null |
| 38 | +} |
| 39 | + |
| 40 | +variable "instance_type" { |
| 41 | + description = "The instance type Packer will use for the builder" |
| 42 | + type = string |
| 43 | + default = "t3.medium" |
| 44 | +} |
| 45 | + |
| 46 | +variable "root_volume_size_gb" { |
| 47 | + type = number |
| 48 | + default = 8 |
| 49 | +} |
| 50 | + |
| 51 | +variable "ebs_delete_on_termination" { |
| 52 | + description = "Indicates whether the EBS volume is deleted on instance termination." |
| 53 | + type = bool |
| 54 | + default = true |
| 55 | +} |
| 56 | + |
| 57 | +variable "global_tags" { |
| 58 | + description = "Tags to apply to everything" |
| 59 | + type = map(string) |
| 60 | + default = {} |
| 61 | +} |
| 62 | + |
| 63 | +variable "ami_tags" { |
| 64 | + description = "Tags to apply to the AMI" |
| 65 | + type = map(string) |
| 66 | + default = {} |
| 67 | +} |
| 68 | + |
| 69 | +variable "snapshot_tags" { |
| 70 | + description = "Tags to apply to the snapshot" |
| 71 | + type = map(string) |
| 72 | + default = {} |
| 73 | +} |
| 74 | + |
| 75 | +variable "custom_shell_commands" { |
| 76 | + description = "Additional commands to run on the EC2 instance, to customize the instance, like installing packages" |
| 77 | + type = list(string) |
| 78 | + default = [] |
| 79 | +} |
| 80 | + |
| 81 | +source "amazon-ebs" "githubrunner" { |
| 82 | + ami_name = "github-runner-ubuntu-jammy-amd64-${formatdate("YYYYMMDDhhmm", timestamp())}" |
| 83 | + instance_type = var.instance_type |
| 84 | + region = var.region |
| 85 | + security_group_id = var.security_group_id |
| 86 | + subnet_id = var.subnet_id |
| 87 | + associate_public_ip_address = var.associate_public_ip_address |
| 88 | + |
| 89 | + source_ami_filter { |
| 90 | + filters = { |
| 91 | + name = "*/ubuntu-jammy-22.04-amd64-server-*" |
| 92 | + root-device-type = "ebs" |
| 93 | + virtualization-type = "hvm" |
| 94 | + } |
| 95 | + most_recent = true |
| 96 | + owners = ["099720109477"] |
| 97 | + } |
| 98 | + ssh_username = "ubuntu" |
| 99 | + tags = merge( |
| 100 | + var.global_tags, |
| 101 | + var.ami_tags, |
| 102 | + { |
| 103 | + OS_Version = "ubuntu-jammy" |
| 104 | + Release = "Latest" |
| 105 | + Base_AMI_Name = "{{ .SourceAMIName }}" |
| 106 | + }) |
| 107 | + snapshot_tags = merge( |
| 108 | + var.global_tags, |
| 109 | + var.snapshot_tags, |
| 110 | + ) |
| 111 | + |
| 112 | + launch_block_device_mappings { |
| 113 | + device_name = "/dev/sda1" |
| 114 | + volume_size = "${var.root_volume_size_gb}" |
| 115 | + volume_type = "gp3" |
| 116 | + delete_on_termination = "${var.ebs_delete_on_termination}" |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +build { |
| 121 | + name = "githubactions-runner" |
| 122 | + sources = [ |
| 123 | + "source.amazon-ebs.githubrunner" |
| 124 | + ] |
| 125 | + provisioner "shell" { |
| 126 | + environment_vars = [ |
| 127 | + "DEBIAN_FRONTEND=noninteractive" |
| 128 | + ] |
| 129 | + inline = concat([ |
| 130 | + "sudo apt-get -y update", |
| 131 | + "sudo apt-get -y install ca-certificates curl gnupg lsb-release", |
| 132 | + "sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg", |
| 133 | + "echo deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null", |
| 134 | + "sudo apt-get -y update", |
| 135 | + "sudo apt-get -y install docker-ce docker-ce-cli containerd.io jq git unzip", |
| 136 | + "sudo systemctl enable containerd.service", |
| 137 | + "sudo service docker start", |
| 138 | + "sudo usermod -a -G docker ubuntu", |
| 139 | + "sudo curl -f https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb -o amazon-cloudwatch-agent.deb", |
| 140 | + "sudo dpkg -i amazon-cloudwatch-agent.deb", |
| 141 | + "sudo systemctl restart amazon-cloudwatch-agent", |
| 142 | + "sudo curl -f https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip -o awscliv2.zip", |
| 143 | + "unzip awscliv2.zip", |
| 144 | + "sudo ./aws/install", |
| 145 | + ], var.custom_shell_commands) |
| 146 | + } |
| 147 | + |
| 148 | + provisioner "file" { |
| 149 | + content = templatefile("../install-runner.sh", { |
| 150 | + install_runner = templatefile("../../modules/runners/templates/install-runner.sh", { |
| 151 | + ARM_PATCH = "" |
| 152 | + S3_LOCATION_RUNNER_DISTRIBUTION = "" |
| 153 | + RUNNER_ARCHITECTURE = "x64" |
| 154 | + }) |
| 155 | + }) |
| 156 | + destination = "/tmp/install-runner.sh" |
| 157 | + } |
| 158 | + |
| 159 | + provisioner "shell" { |
| 160 | + environment_vars = [ |
| 161 | + "RUNNER_TARBALL_URL=https://github.com/actions/runner/releases/download/v${var.runner_version}/actions-runner-linux-x64-${var.runner_version}.tar.gz" |
| 162 | + ] |
| 163 | + inline = [ |
| 164 | + "sudo chmod +x /tmp/install-runner.sh", |
| 165 | + "echo ubuntu | tee -a /tmp/install-user.txt", |
| 166 | + "sudo RUNNER_ARCHITECTURE=x64 RUNNER_TARBALL_URL=$RUNNER_TARBALL_URL /tmp/install-runner.sh", |
| 167 | + "echo ImageOS=ubuntu22 | tee -a /opt/actions-runner/.env" |
| 168 | + ] |
| 169 | + } |
| 170 | + |
| 171 | + provisioner "file" { |
| 172 | + content = templatefile("../start-runner.sh", { |
| 173 | + start_runner = templatefile("../../modules/runners/templates/start-runner.sh", {}) |
| 174 | + }) |
| 175 | + destination = "/tmp/start-runner.sh" |
| 176 | + } |
| 177 | + |
| 178 | + provisioner "shell" { |
| 179 | + inline = [ |
| 180 | + "sudo mv /tmp/start-runner.sh /var/lib/cloud/scripts/per-boot/start-runner.sh", |
| 181 | + "sudo chmod +x /var/lib/cloud/scripts/per-boot/start-runner.sh", |
| 182 | + ] |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments