Skip to content

Commit 7907984

Browse files
authored
fix: Add config for windows ami (#1525)
* Add config for windows ami * Update packer build to validate windows
1 parent 4ec44df commit 7907984

File tree

12 files changed

+208
-12
lines changed

12 files changed

+208
-12
lines changed

.github/workflows/packer-build.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@ jobs:
1818
runs-on: ubuntu-latest
1919
container:
2020
image: hashicorp/packer:1.7.8
21+
strategy:
22+
matrix:
23+
image: ["linux-amzn2", "windows-core-2019"]
2124
defaults:
2225
run:
23-
working-directory: images/linux-amzn2
26+
working-directory: images/${{ matrix.image }}
2427
steps:
2528
- name: "Checkout"
2629
uses: actions/checkout@v2
2730

2831
- name: packer init
2932
run: packer init .
3033

31-
- name: check terraform formatting
34+
- name: check packer formatting
3235
run: packer fmt -recursive -check=true .
3336

3437
- name: packer validate

examples/prebuilt/README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@ This module shows how to create GitHub action runners using a prebuilt AMI for t
44

55
## Usages
66

7-
Steps for the full setup, such as creating a GitHub app can be found in the root module's [README](../../README.md).
7+
Steps for the full setup, such as creating a GitHub app can be found in the root module's [README](../../README.md).
8+
9+
## Variables
10+
11+
| Name | Description | Type | Default | Required |
12+
|------|-------------|------|---------|:--------:|
13+
| <a name="input_ami_filter"></a> [ami\_filter](#input\_ami\_filter) | The amis to search. Use the default for the provided amazon linux image, `github-runner-windows-core-2019-*` for the provided widnows image | `string` | `github-runner-amzn2-x86_64-2021*` | no |
14+
| <a name="input_github_app_key_base64"></a> [github\_app\_key\_base64](#input\_github\_app\_key\_base64) | The base64 encoded private key you downloaded from GitHub when creating the app | `string` | | yes |
15+
| <a name="input_github_app_id"></a> [github\_app\_id](#input\_github\_app\_id) | The id of the app you created on GitHub | `string` | | yes |
16+
| <a name="input_region"></a> [region](#input\_region) | The target aws region | `string` | `eu-west-1` | no |
17+
| <a name="input_runner_os"></a> [runner\_os](#input\_runner\_os) | The os of the image, either `linux` or `windows` | `string` | `linux` | no |
818

919
### Lambdas
1020

examples/prebuilt/main.tf

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
locals {
22
environment = "prebuilt"
3-
aws_region = "eu-west-1"
43
}
54

65
resource "random_id" "random" {
@@ -12,7 +11,7 @@ data "aws_caller_identity" "current" {}
1211
module "runners" {
1312
source = "../../"
1413
create_service_linked_role_spot = true
15-
aws_region = local.aws_region
14+
aws_region = var.aws_region
1615
vpc_id = module.vpc.vpc_id
1716
subnet_ids = module.vpc.private_subnets
1817

@@ -24,15 +23,17 @@ module "runners" {
2423
webhook_secret = random_id.random.hex
2524
}
2625

27-
webhook_lambda_zip = "../../lambda_output/webhook.zip"
28-
runner_binaries_syncer_lambda_zip = "../../lambda_output/runner-binaries-syncer.zip"
29-
runners_lambda_zip = "../../lambda_output/runners.zip"
26+
webhook_lambda_zip = "lambdas-download/webhook.zip"
27+
runner_binaries_syncer_lambda_zip = "lambdas-download/runner-binaries-syncer.zip"
28+
runners_lambda_zip = "lambdas-download/runners.zip"
3029

3130
runner_extra_labels = "default,example"
3231

32+
runner_os = var.runner_os
33+
3334
# configure your pre-built AMI
3435
enabled_userdata = false
35-
ami_filter = { name = ["github-runner-amzn2-x86_64-2021*"] }
36+
ami_filter = { name = [var.ami_name_filter] }
3637
ami_owners = [data.aws_caller_identity.current.account_id]
3738

3839
# enable access to the runners via SSM

examples/prebuilt/providers.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
provider "aws" {
2-
region = local.aws_region
2+
region = var.aws_region
33
}

examples/prebuilt/variables.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,18 @@
22
variable "github_app_key_base64" {}
33

44
variable "github_app_id" {}
5+
6+
variable "runner_os" {
7+
type = string
8+
default = "linux"
9+
}
10+
11+
variable "ami_name_filter" {
12+
type = string
13+
default = "github-runner-amzn2-x86_64-2021*"
14+
}
15+
16+
variable "aws_region" {
17+
type = string
18+
default = "eu-west-1"
19+
}

examples/prebuilt/vpc.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ module "vpc" {
22
source = "git::https://github.com/philips-software/terraform-aws-vpc.git?ref=2.2.0"
33

44
environment = local.environment
5-
aws_region = local.aws_region
5+
aws_region = var.aws_region
66
create_private_hosted_zone = false
77
}

images/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The images inside this folder are pre-built images designed to shorten the boot
44

55
These images share the same scripting as used in the user-data mechanism in `/modules/runners/templates/`. We use a `tempaltefile` mechanism to insert the relevant script fragments into the scripts used for provisioning the images.
66

7-
The example in `linux-amzn2` also uploads a `start-runner.sh` script that uses the exact same startup process as used in the user-data mechanism. This means that the image created here does not need any extra scripts injected or changes to boot up and connect to GH.
7+
The examples in `linux-amzn2` and `windows-core-2019` also upload a `start-runner` script that uses the exact same startup process as used in the user-data mechanism. This means that the image created here does not need any extra scripts injected or changes to boot up and connect to GH.
88

99
## Building your own
1010

images/install-runner.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash -e
2+
3+
user_name=ec2-user
4+
5+
## This wrapper file re-uses scripts in the /modules/runners/templates directory
6+
## of this repo. These are the same that are used by the user_data functionality
7+
## to bootstrap the instance if it is started from an existing AMI.
8+
${install_runner}

images/start-runner.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Start-Transcript -Path "C:\runner-startup.log" -Append
2+
${start_runner}
3+
Stop-Transcript
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<powershell>
2+
3+
Write-Output "Running User Data Script"
4+
Write-Host "(host) Running User Data Script"
5+
6+
Set-ExecutionPolicy Unrestricted -Scope LocalMachine -Force -ErrorAction Ignore
7+
8+
# Don't set this before Set-ExecutionPolicy as it throws an error
9+
$ErrorActionPreference = "stop"
10+
11+
# Remove HTTP listener
12+
Remove-Item -Path WSMan:\Localhost\listener\listener* -Recurse
13+
14+
# Create a self-signed certificate to let ssl work
15+
$Cert = New-SelfSignedCertificate -CertstoreLocation Cert:\LocalMachine\My -DnsName "packer"
16+
New-Item -Path WSMan:\LocalHost\Listener -Transport HTTPS -Address * -CertificateThumbPrint $Cert.Thumbprint -Force
17+
18+
# WinRM
19+
Write-Output "Setting up WinRM"
20+
Write-Host "(host) setting up WinRM"
21+
22+
# I'm not really sure why we need the cmd.exe wrapper, but it works with it and doesn't work without it
23+
cmd.exe /c winrm quickconfig -q
24+
cmd.exe /c winrm set "winrm/config" '@{MaxTimeoutms="1800000"}'
25+
cmd.exe /c winrm set "winrm/config/winrs" '@{MaxMemoryPerShellMB="1024"}'
26+
cmd.exe /c winrm set "winrm/config/service" '@{AllowUnencrypted="true"}'
27+
cmd.exe /c winrm set "winrm/config/client" '@{AllowUnencrypted="true"}'
28+
cmd.exe /c winrm set "winrm/config/service/auth" '@{Basic="true"}'
29+
cmd.exe /c winrm set "winrm/config/client/auth" '@{Basic="true"}'
30+
cmd.exe /c winrm set "winrm/config/service/auth" '@{CredSSP="true"}'
31+
cmd.exe /c winrm set "winrm/config/listener?Address=*+Transport=HTTPS" "@{Port=`"5986`";Hostname=`"packer`";CertificateThumbprint=`"$($Cert.Thumbprint)`"}"
32+
cmd.exe /c netsh advfirewall firewall set rule group="remote administration" new enable=yes
33+
cmd.exe /c netsh firewall add portopening TCP 5986 "Port 5986"
34+
cmd.exe /c net stop winrm
35+
cmd.exe /c sc config winrm start= auto
36+
cmd.exe /c net start winrm
37+
38+
</powershell>

0 commit comments

Comments
 (0)