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

Commit 97707c2

Browse files
authored
feat(images): add ami for windows core 2022 (#2390)
1 parent 69578e0 commit 97707c2

File tree

3 files changed

+203
-0
lines changed

3 files changed

+203
-0
lines changed
+38
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 "root_volume_size_gb" {
35+
type = number
36+
default = 30
37+
}
38+
39+
variable "ebs_delete_on_termination" {
40+
description = "Indicates whether the EBS volume is deleted on instance termination."
41+
type = bool
42+
default = true
43+
}
44+
45+
variable "associate_public_ip_address" {
46+
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"
47+
type = string
48+
default = null
49+
}
50+
51+
variable "custom_shell_commands" {
52+
description = "Additional commands to run on the EC2 instance, to customize the instance, like installing packages"
53+
type = list(string)
54+
default = []
55+
}
56+
57+
source "amazon-ebs" "githubrunner" {
58+
ami_name = "github-runner-windows-core-2022-${formatdate("YYYYMMDDhhmm", timestamp())}"
59+
communicator = "winrm"
60+
instance_type = "m4.xlarge"
61+
region = var.region
62+
security_group_id = var.security_group_id
63+
subnet_id = var.subnet_id
64+
associate_public_ip_address = var.associate_public_ip_address
65+
66+
source_ami_filter {
67+
filters = {
68+
name = "Windows_Server-2022-English-Core-ContainersLatest-**"
69+
root-device-type = "ebs"
70+
virtualization-type = "hvm"
71+
}
72+
most_recent = true
73+
owners = ["amazon"]
74+
}
75+
tags = {
76+
OS_Version = "windows-core-2022"
77+
Release = "Latest"
78+
Base_AMI_Name = "{{ .SourceAMIName }}"
79+
}
80+
user_data_file = "./bootstrap_win.ps1"
81+
winrm_insecure = true
82+
winrm_port = 5986
83+
winrm_use_ssl = true
84+
winrm_username = "Administrator"
85+
86+
launch_block_device_mappings {
87+
device_name = "/dev/sda1"
88+
volume_size = "${var.root_volume_size_gb}"
89+
delete_on_termination = "${var.ebs_delete_on_termination}"
90+
}
91+
}
92+
93+
build {
94+
name = "githubactions-runner"
95+
sources = [
96+
"source.amazon-ebs.githubrunner"
97+
]
98+
99+
provisioner "file" {
100+
content = templatefile("../start-runner.ps1", {
101+
start_runner = templatefile("../../modules/runners/templates/start-runner.ps1", {})
102+
})
103+
destination = "C:\\start-runner.ps1"
104+
}
105+
106+
provisioner "powershell" {
107+
inline = concat([
108+
templatefile("./windows-provisioner.ps1", {
109+
action_runner_url = "https://github.com/actions/runner/releases/download/v${var.runner_version}/actions-runner-win-x64-${var.runner_version}.zip"
110+
})
111+
], var.custom_shell_commands)
112+
}
113+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
$ErrorActionPreference = "Continue"
2+
$VerbosePreference = "Continue"
3+
4+
# Install Chocolatey
5+
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
6+
$env:chocolateyUseWindowsCompression = 'true'
7+
Invoke-WebRequest https://chocolatey.org/install.ps1 -UseBasicParsing | Invoke-Expression
8+
9+
# Add Chocolatey to powershell profile
10+
$ChocoProfileValue = @'
11+
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
12+
if (Test-Path($ChocolateyProfile)) {
13+
Import-Module "$ChocolateyProfile"
14+
}
15+
16+
refreshenv
17+
'@
18+
# Write it to the $profile location
19+
Set-Content -Path "$PsHome\Microsoft.PowerShell_profile.ps1" -Value $ChocoProfileValue -Force
20+
# Source it
21+
. "$PsHome\Microsoft.PowerShell_profile.ps1"
22+
23+
refreshenv
24+
25+
Write-Host "Installing cloudwatch agent..."
26+
Invoke-WebRequest -Uri https://s3.amazonaws.com/amazoncloudwatch-agent/windows/amd64/latest/amazon-cloudwatch-agent.msi -OutFile C:\amazon-cloudwatch-agent.msi
27+
$cloudwatchParams = '/i', 'C:\amazon-cloudwatch-agent.msi', '/qn', '/L*v', 'C:\CloudwatchInstall.log'
28+
Start-Process "msiexec.exe" $cloudwatchParams -Wait -NoNewWindow
29+
Remove-Item C:\amazon-cloudwatch-agent.msi
30+
31+
# Install dependent tools
32+
Write-Host "Installing additional development tools"
33+
choco install git awscli -y
34+
refreshenv
35+
36+
Write-Host "Creating actions-runner directory for the GH Action installtion"
37+
New-Item -ItemType Directory -Path C:\actions-runner ; Set-Location C:\actions-runner
38+
39+
Write-Host "Downloading the GH Action runner from ${action_runner_url}"
40+
Invoke-WebRequest -Uri ${action_runner_url} -OutFile actions-runner.zip
41+
42+
Write-Host "Un-zip action runner"
43+
Expand-Archive -Path actions-runner.zip -DestinationPath .
44+
45+
Write-Host "Delete zip file"
46+
Remove-Item actions-runner.zip
47+
48+
$action = New-ScheduledTaskAction -WorkingDirectory "C:\actions-runner" -Execute "PowerShell.exe" -Argument "-File C:\start-runner.ps1"
49+
$trigger = New-ScheduledTaskTrigger -AtStartup
50+
Register-ScheduledTask -TaskName "runnerinit" -Action $action -Trigger $trigger -User System -RunLevel Highest -Force
51+
52+
C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 -Schedule

0 commit comments

Comments
 (0)