github_agent.ubuntu.pkr.hcl 5.9 KB

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