Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Commit 997b171

Browse files
authored
feat(images): Added ubuntu-focual example packer configuration (#1644)
* feat: create Ubuntu image * feat: add aws v2 cli * feat: add unzip * feat: port tag changes to amazon linux image * chore(images): use same default for aws region * fix: use apt-get you melon * feat: add cloudwatch agent * fix: use new install location in example * feat: set env vars in service and align * fix: setup action tool cache * fix: add missing arch var * fix: use Ubuntu's root device name * chore: set apt to be noninteractive * chore: align tag values with amzn2 image * chore: better docs for tool cache location * chore: include a warning Co-authored-by: toast-gear <[email protected]>
1 parent a7f9eb1 commit 997b171

File tree

6 files changed

+200
-9
lines changed

6 files changed

+200
-9
lines changed

Diff for: .github/workflows/packer-build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
image: hashicorp/packer:1.7.8
2222
strategy:
2323
matrix:
24-
image: ["linux-amzn2", "windows-core-2019"]
24+
image: ["linux-amzn2", "windows-core-2019", "ubuntu-focal"]
2525
defaults:
2626
run:
2727
working-directory: images/${{ matrix.image }}

Diff for: examples/ubuntu/main.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module "runners" {
3535
# enable access to the runners via SSM
3636
enable_ssm_on_runners = true
3737

38-
runner_run_as = "runners"
38+
runner_run_as = "ubuntu"
3939
userdata_template = "./templates/user-data.sh"
4040
ami_owners = ["099720109477"] # Canonical's Amazon account ID
4141

@@ -64,7 +64,7 @@ module "runners" {
6464
{
6565
"log_group_name" : "runner",
6666
"prefix_log_group" : true,
67-
"file_path" : "/home/runners/actions-runner/_diag/Runner_**.log",
67+
"file_path" : "/opt/actions-runner/_diag/Runner_**.log",
6868
"log_stream_name" : "{instance_id}/runner"
6969
}
7070
]

Diff for: images/install-runner.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash -e
22

3-
user_name=ec2-user
3+
user_name=$(cat /tmp/install-user.txt)
44

55
## This wrapper file re-uses scripts in the /modules/runners/templates directory
66
## of this repo. These are the same that are used by the user_data functionality

Diff for: images/linux-amzn2/github_agent.linux.pkr.hcl

+23-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ packer {
1010
variable "runner_version" {
1111
description = "The version (no v prefix) of the runner software to install https://github.com/actions/runner/releases"
1212
type = string
13-
default = "2.286.0"
13+
default = "2.286.1"
1414
}
1515

1616
variable "region" {
@@ -42,8 +42,20 @@ variable "root_volume_size_gb" {
4242
default = 8
4343
}
4444

45-
variable "tags" {
46-
description = "Additional tags to add globally"
45+
variable "global_tags" {
46+
description = "Tags to apply to everything"
47+
type = map(string)
48+
default = {}
49+
}
50+
51+
variable "ami_tags" {
52+
description = "Tags to apply to the AMI"
53+
type = map(string)
54+
default = {}
55+
}
56+
57+
variable "snapshot_tags" {
58+
description = "Tags to apply to the snapshot"
4759
type = map(string)
4860
default = {}
4961
}
@@ -65,12 +77,18 @@ source "amazon-ebs" "githubrunner" {
6577
}
6678
ssh_username = "ec2-user"
6779
tags = merge(
68-
var.tags,
80+
var.global_tags,
81+
var.ami_tags,
6982
{
7083
OS_Version = "amzn2"
7184
Release = "Latest"
7285
Base_AMI_Name = "{{ .SourceAMIName }}"
7386
})
87+
snapshot_tags = merge(
88+
var.global_tags,
89+
var.snapshot_tags,
90+
)
91+
7492

7593
launch_block_device_mappings {
7694
device_name = "/dev/xvda"
@@ -114,6 +132,7 @@ build {
114132
]
115133
inline = [
116134
"sudo chmod +x /tmp/install-runner.sh",
135+
"echo ec2-user > /tmp/install-user.txt",
117136
"sudo RUNNER_ARCHITECTURE=x64 RUNNER_TARBALL_URL=$RUNNER_TARBALL_URL /tmp/install-runner.sh"
118137
]
119138
}

Diff for: images/ubuntu-focal/github_agent.ubuntu.pkr.hcl

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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.286.1"
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 "instance_type" {
35+
description = "The instance type Packer will use for the builder"
36+
type = string
37+
default = "t3.medium"
38+
}
39+
40+
variable "root_volume_size_gb" {
41+
type = number
42+
default = 8
43+
}
44+
45+
variable "global_tags" {
46+
description = "Tags to apply to everything"
47+
type = map(string)
48+
default = {}
49+
}
50+
51+
variable "ami_tags" {
52+
description = "Tags to apply to the AMI"
53+
type = map(string)
54+
default = {}
55+
}
56+
57+
variable "snapshot_tags" {
58+
description = "Tags to apply to the snapshot"
59+
type = map(string)
60+
default = {}
61+
}
62+
63+
source "amazon-ebs" "githubrunner" {
64+
ami_name = "github-runner-ubuntu-focal-amd64-${formatdate("YYYYMMDDhhmm", timestamp())}"
65+
instance_type = var.instance_type
66+
region = var.region
67+
security_group_id = var.security_group_id
68+
subnet_id = var.subnet_id
69+
source_ami_filter {
70+
filters = {
71+
name = "*/ubuntu-focal-20.04-amd64-server-*"
72+
root-device-type = "ebs"
73+
virtualization-type = "hvm"
74+
}
75+
most_recent = true
76+
owners = ["099720109477"]
77+
}
78+
ssh_username = "ubuntu"
79+
tags = merge(
80+
var.global_tags,
81+
var.ami_tags,
82+
{
83+
OS_Version = "ubuntu-focal"
84+
Release = "Latest"
85+
Base_AMI_Name = "{{ .SourceAMIName }}"
86+
})
87+
snapshot_tags = merge(
88+
var.global_tags,
89+
var.snapshot_tags,
90+
)
91+
92+
launch_block_device_mappings {
93+
device_name = "/dev/sda1"
94+
volume_size = "${var.root_volume_size_gb}"
95+
volume_type = "gp3"
96+
}
97+
}
98+
99+
build {
100+
name = "githubactions-runner"
101+
sources = [
102+
"source.amazon-ebs.githubrunner"
103+
]
104+
provisioner "shell" {
105+
environment_vars = [
106+
"DEBIAN_FRONTEND=noninteractive"
107+
]
108+
inline = [
109+
"sudo apt-get -y update",
110+
"sudo apt-get -y install ca-certificates curl gnupg lsb-release",
111+
"sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg",
112+
"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",
113+
"sudo apt-get -y update",
114+
"sudo apt-get -y install docker-ce docker-ce-cli containerd.io jq git unzip",
115+
"sudo systemctl enable containerd.service",
116+
"sudo service docker start",
117+
"sudo usermod -a -G docker ubuntu",
118+
"sudo curl -f https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb -o amazon-cloudwatch-agent.deb",
119+
"sudo dpkg -i amazon-cloudwatch-agent.deb",
120+
"sudo systemctl restart amazon-cloudwatch-agent",
121+
"sudo curl -f https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip -o awscliv2.zip",
122+
"unzip awscliv2.zip",
123+
"sudo ./aws/install",
124+
]
125+
}
126+
127+
provisioner "file" {
128+
content = templatefile("../install-runner.sh", {
129+
install_runner = templatefile("../../modules/runners/templates/install-runner.sh", {
130+
ARM_PATCH = ""
131+
S3_LOCATION_RUNNER_DISTRIBUTION = ""
132+
RUNNER_ARCHITECTURE = "x64"
133+
})
134+
})
135+
destination = "/tmp/install-runner.sh"
136+
}
137+
138+
provisioner "shell" {
139+
environment_vars = [
140+
"RUNNER_TARBALL_URL=https://github.com/actions/runner/releases/download/v${var.runner_version}/actions-runner-linux-x64-${var.runner_version}.tar.gz"
141+
]
142+
inline = [
143+
"sudo chmod +x /tmp/install-runner.sh",
144+
"echo ubuntu | tee -a /tmp/install-user.txt",
145+
"sudo RUNNER_ARCHITECTURE=x64 RUNNER_TARBALL_URL=$RUNNER_TARBALL_URL /tmp/install-runner.sh",
146+
"echo ImageOS=ubuntu20 | tee -a /opt/actions-runner/.env"
147+
]
148+
}
149+
150+
provisioner "file" {
151+
content = templatefile("../start-runner.sh", {
152+
start_runner = templatefile("../../modules/runners/templates/start-runner.sh", {})
153+
})
154+
destination = "/tmp/start-runner.sh"
155+
}
156+
157+
provisioner "shell" {
158+
inline = [
159+
"sudo mv /tmp/start-runner.sh /var/lib/cloud/scripts/per-boot/start-runner.sh",
160+
"sudo chmod +x /var/lib/cloud/scripts/per-boot/start-runner.sh",
161+
]
162+
}
163+
164+
}

Diff for: modules/runners/templates/install-runner.sh

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ fi
1212

1313
file_name="actions-runner.tar.gz"
1414

15-
echo "Creating actions-runner directory for the GH Action installtion"
15+
echo "Setting up GH Actions runner tool cache"
16+
# Required for various */setup-* actions to work, location is also know by various environment
17+
# variable names in the actions/runner software : RUNNER_TOOL_CACHE / RUNNER_TOOLSDIRECTORY / AGENT_TOOLSDIRECTORY
18+
# Warning, not all setup actions support the env vars and so this specific path must be created regardless
19+
mkdir -p /opt/hostedtoolcache
20+
21+
echo "Creating actions-runner directory for the GH Action installation"
1622
cd /opt/
1723
mkdir -p actions-runner && cd actions-runner
1824

25+
1926
if [[ -n "$RUNNER_TARBALL_URL" ]]; then
2027
echo "Downloading the GH Action runner from $RUNNER_TARBALL_URL to $file_name"
2128
curl -o $file_name -L "$RUNNER_TARBALL_URL"
@@ -47,3 +54,4 @@ fi
4754

4855
echo "Set file ownership of action runner"
4956
chown -R "$user_name":"$user_name" .
57+
chown -R "$user_name":"$user_name" /opt/hostedtoolcache

0 commit comments

Comments
 (0)