Skip to content

Commit

Permalink
feat(images): automatically find latest GitHub Runner version when bu…
Browse files Browse the repository at this point in the history
…ilding images (#3129)

* feat: Automatically find latest GitHub Runner version when building images

* update the rest of the packer configs

* Update .github/workflows/packer-build.yml

Co-authored-by: Niek Palm <npalm@users.noreply.github.com>

---------

Co-authored-by: Niek Palm <npalm@users.noreply.github.com>
  • Loading branch information
gautamg795 and npalm authored Apr 19, 2023
1 parent 569cff8 commit da49078
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/packer-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
name: Verify packer
runs-on: ubuntu-latest
container:
image: index.docker.io/hashicorp/packer@sha256:f795aace438ef92e738228c21d5ceb7d5dd73ceb7e0b1efab5b0e90cbc4d4dcd # ratchet:hashicorp/packer:1.7.8
image: index.docker.io/hashicorp/packer@sha256:297bbbbbbf3ce9e0431ac1e8f02934b20e1197613f877b55dfdb1ebfd94eb748 # ratchet:index.docker.io/hashicorp/packer:1.8.6
strategy:
matrix:
image: ["linux-amzn2", "windows-core-2019", "windows-core-2022", "ubuntu-focal", "ubuntu-jammy", "ubuntu-jammy-arm64"]
Expand All @@ -30,4 +30,4 @@ jobs:
- name: check packer formatting
run: packer fmt -recursive -check=true .
- name: packer validate
run: packer validate .
run: packer validate -evaluate-datasources .
19 changes: 15 additions & 4 deletions images/linux-amzn2/github_agent.linux.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ packer {
}

variable "runner_version" {
description = "The version (no v prefix) of the runner software to install https://github.com/actions/runner/releases"
type = string
default = "2.295.0"
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."
default = null
}

variable "region" {
Expand Down Expand Up @@ -78,6 +77,18 @@ variable "custom_shell_commands" {
default = []
}

data "http" github_runner_release_json {
url = "https://api.github.com/repos/actions/runner/releases/latest"
request_headers = {
Accept = "application/vnd.github+json"
X-GitHub-Api-Version : "2022-11-28"
}
}

locals {
runner_version = coalesce(var.runner_version, trimprefix(jsondecode(data.http.github_runner_release_json.body).tag_name, "v"))
}

source "amazon-ebs" "githubrunner" {
ami_name = "github-runner-amzn2-x86_64-${formatdate("YYYYMMDDhhmm", timestamp())}"
instance_type = var.instance_type
Expand Down Expand Up @@ -148,7 +159,7 @@ build {

provisioner "shell" {
environment_vars = [
"RUNNER_TARBALL_URL=https://github.com/actions/runner/releases/download/v${var.runner_version}/actions-runner-linux-x64-${var.runner_version}.tar.gz"
"RUNNER_TARBALL_URL=https://github.com/actions/runner/releases/download/v${local.runner_version}/actions-runner-linux-x64-${local.runner_version}.tar.gz"
]
inline = [
"sudo chmod +x /tmp/install-runner.sh",
Expand Down
19 changes: 15 additions & 4 deletions images/ubuntu-focal/github_agent.ubuntu.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ packer {
}

variable "runner_version" {
description = "The version (no v prefix) of the runner software to install https://github.com/actions/runner/releases"
type = string
default = "2.286.1"
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."
default = null
}

variable "region" {
Expand Down Expand Up @@ -78,6 +77,18 @@ variable "custom_shell_commands" {
default = []
}

data "http" github_runner_release_json {
url = "https://api.github.com/repos/actions/runner/releases/latest"
request_headers = {
Accept = "application/vnd.github+json"
X-GitHub-Api-Version : "2022-11-28"
}
}

locals {
runner_version = coalesce(var.runner_version, trimprefix(jsondecode(data.http.github_runner_release_json.body).tag_name, "v"))
}

source "amazon-ebs" "githubrunner" {
ami_name = "github-runner-ubuntu-focal-amd64-${formatdate("YYYYMMDDhhmm", timestamp())}"
instance_type = var.instance_type
Expand Down Expand Up @@ -159,7 +170,7 @@ build {

provisioner "shell" {
environment_vars = [
"RUNNER_TARBALL_URL=https://github.com/actions/runner/releases/download/v${var.runner_version}/actions-runner-linux-x64-${var.runner_version}.tar.gz"
"RUNNER_TARBALL_URL=https://github.com/actions/runner/releases/download/v${local.runner_version}/actions-runner-linux-x64-${local.runner_version}.tar.gz"
]
inline = [
"sudo chmod +x /tmp/install-runner.sh",
Expand Down
19 changes: 15 additions & 4 deletions images/ubuntu-jammy-arm64/github_agent.ubuntu.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ packer {
}

variable "runner_version" {
description = "The version (no v prefix) of the runner software to install https://github.com/actions/runner/releases"
type = string
default = "2.302.1"
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."
default = null
}

variable "region" {
Expand Down Expand Up @@ -78,6 +77,18 @@ variable "custom_shell_commands" {
default = []
}

data "http" github_runner_release_json {
url = "https://api.github.com/repos/actions/runner/releases/latest"
request_headers = {
Accept = "application/vnd.github+json"
X-GitHub-Api-Version : "2022-11-28"
}
}

locals {
runner_version = coalesce(var.runner_version, trimprefix(jsondecode(data.http.github_runner_release_json.body).tag_name, "v"))
}

source "amazon-ebs" "githubrunner" {
ami_name = "github-runner-ubuntu-jammy-arm64-${formatdate("YYYYMMDDhhmm", timestamp())}"
instance_type = var.instance_type
Expand Down Expand Up @@ -159,7 +170,7 @@ build {

provisioner "shell" {
environment_vars = [
"RUNNER_TARBALL_URL=https://github.com/actions/runner/releases/download/v${var.runner_version}/actions-runner-linux-arm64-${var.runner_version}.tar.gz"
"RUNNER_TARBALL_URL=https://github.com/actions/runner/releases/download/v${local.runner_version}/actions-runner-linux-arm64-${local.runner_version}.tar.gz"
]
inline = [
"sudo chmod +x /tmp/install-runner.sh",
Expand Down
19 changes: 15 additions & 4 deletions images/ubuntu-jammy/github_agent.ubuntu.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ packer {
}

variable "runner_version" {
description = "The version (no v prefix) of the runner software to install https://github.com/actions/runner/releases"
type = string
default = "2.294.0"
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."
default = null
}

variable "region" {
Expand Down Expand Up @@ -78,6 +77,18 @@ variable "custom_shell_commands" {
default = []
}

data "http" github_runner_release_json {
url = "https://api.github.com/repos/actions/runner/releases/latest"
request_headers = {
Accept = "application/vnd.github+json"
X-GitHub-Api-Version : "2022-11-28"
}
}

locals {
runner_version = coalesce(var.runner_version, trimprefix(jsondecode(data.http.github_runner_release_json.body).tag_name, "v"))
}

source "amazon-ebs" "githubrunner" {
ami_name = "github-runner-ubuntu-jammy-amd64-${formatdate("YYYYMMDDhhmm", timestamp())}"
instance_type = var.instance_type
Expand Down Expand Up @@ -159,7 +170,7 @@ build {

provisioner "shell" {
environment_vars = [
"RUNNER_TARBALL_URL=https://github.com/actions/runner/releases/download/v${var.runner_version}/actions-runner-linux-x64-${var.runner_version}.tar.gz"
"RUNNER_TARBALL_URL=https://github.com/actions/runner/releases/download/v${local.runner_version}/actions-runner-linux-x64-${local.runner_version}.tar.gz"
]
inline = [
"sudo chmod +x /tmp/install-runner.sh",
Expand Down
21 changes: 16 additions & 5 deletions images/windows-core-2019/github_agent.windows.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ packer {
}
}

variable "action_runner_url" {
description = "The URL to the tarball of the action runner"
type = string
default = "https://github.com/actions/runner/releases/download/v2.285.1/actions-runner-win-x64-2.285.1.zip"
variable "runner_version" {
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."
default = null
}

variable "region" {
Expand Down Expand Up @@ -43,6 +42,18 @@ variable "custom_shell_commands" {
default = []
}

data "http" github_runner_release_json {
url = "https://api.github.com/repos/actions/runner/releases/latest"
request_headers = {
Accept = "application/vnd.github+json"
X-GitHub-Api-Version : "2022-11-28"
}
}

locals {
runner_version = coalesce(var.runner_version, trimprefix(jsondecode(data.http.github_runner_release_json.body).tag_name, "v"))
}

source "amazon-ebs" "githubrunner" {
ami_name = "github-runner-windows-core-2019-${formatdate("YYYYMMDDhhmm", timestamp())}"
communicator = "winrm"
Expand Down Expand Up @@ -92,7 +103,7 @@ build {
provisioner "powershell" {
inline = concat([
templatefile("./windows-provisioner.ps1", {
action_runner_url = var.action_runner_url
action_runner_url = "https://github.com/actions/runner/releases/download/v${local.runner_version}/actions-runner-win-x64-${local.runner_version}.zip"
})
], var.custom_shell_commands)
}
Expand Down
19 changes: 15 additions & 4 deletions images/windows-core-2022/github_agent.windows.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ packer {
}

variable "runner_version" {
description = "The version (no v prefix) of the runner software to install https://github.com/actions/runner/releases"
type = string
default = "2.286.1"
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."
default = null
}

variable "region" {
Expand Down Expand Up @@ -54,6 +53,18 @@ variable "custom_shell_commands" {
default = []
}

data "http" github_runner_release_json {
url = "https://api.github.com/repos/actions/runner/releases/latest"
request_headers = {
Accept = "application/vnd.github+json"
X-GitHub-Api-Version : "2022-11-28"
}
}

locals {
runner_version = coalesce(var.runner_version, trimprefix(jsondecode(data.http.github_runner_release_json.body).tag_name, "v"))
}

source "amazon-ebs" "githubrunner" {
ami_name = "github-runner-windows-core-2022-${formatdate("YYYYMMDDhhmm", timestamp())}"
communicator = "winrm"
Expand Down Expand Up @@ -106,7 +117,7 @@ build {
provisioner "powershell" {
inline = concat([
templatefile("./windows-provisioner.ps1", {
action_runner_url = "https://github.com/actions/runner/releases/download/v${var.runner_version}/actions-runner-win-x64-${var.runner_version}.zip"
action_runner_url = "https://github.com/actions/runner/releases/download/v${local.runner_version}/actions-runner-win-x64-${local.runner_version}.zip"
})
], var.custom_shell_commands)
}
Expand Down

0 comments on commit da49078

Please sign in to comment.