github_agent.linux.pkr.hcl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 = "m3.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-amzn2-x86_64-${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 = "amzn2-ami-kernel-5.*-hvm-*-x86_64-gp2"
  78. root-device-type = "ebs"
  79. virtualization-type = "hvm"
  80. }
  81. most_recent = true
  82. owners = ["137112412989"]
  83. }
  84. ssh_username = "ec2-user"
  85. tags = merge(
  86. var.global_tags,
  87. var.ami_tags,
  88. {
  89. OS_Version = "amzn2"
  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/xvda"
  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. inline = concat([
  112. "sudo yum update -y",
  113. "sudo yum install -y amazon-cloudwatch-agent curl jq git",
  114. "sudo amazon-linux-extras install docker",
  115. "sudo systemctl enable docker.service",
  116. "sudo systemctl enable containerd.service",
  117. "sudo service docker start",
  118. "sudo usermod -a -G docker ec2-user",
  119. ], var.custom_shell_commands)
  120. }
  121. provisioner "file" {
  122. content = templatefile("../install-runner.sh", {
  123. install_runner = templatefile("../../modules/runners/templates/install-runner.sh", {
  124. ARM_PATCH = ""
  125. S3_LOCATION_RUNNER_DISTRIBUTION = ""
  126. RUNNER_ARCHITECTURE = "x64"
  127. })
  128. })
  129. destination = "/tmp/install-runner.sh"
  130. }
  131. provisioner "shell" {
  132. environment_vars = [
  133. "RUNNER_TARBALL_URL=https://github.com/actions/runner/releases/download/v${var.runner_version}/actions-runner-linux-x64-${var.runner_version}.tar.gz"
  134. ]
  135. inline = [
  136. "sudo chmod +x /tmp/install-runner.sh",
  137. "echo ec2-user > /tmp/install-user.txt",
  138. "sudo RUNNER_ARCHITECTURE=x64 RUNNER_TARBALL_URL=$RUNNER_TARBALL_URL /tmp/install-runner.sh"
  139. ]
  140. }
  141. provisioner "file" {
  142. content = templatefile("../start-runner.sh", {
  143. start_runner = templatefile("../../modules/runners/templates/start-runner.sh", {})
  144. })
  145. destination = "/tmp/start-runner.sh"
  146. }
  147. provisioner "shell" {
  148. inline = [
  149. "sudo mv /tmp/start-runner.sh /var/lib/cloud/scripts/per-boot/start-runner.sh",
  150. "sudo chmod +x /var/lib/cloud/scripts/per-boot/start-runner.sh",
  151. ]
  152. }
  153. }