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

Commit 44f5d6d

Browse files
authored
fix: support ephemeral runners for windows (#4164)
This PR adds support for ephemeral runners on windows via the provided start script.
1 parent 5573c39 commit 44f5d6d

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

Diff for: modules/runners/templates/start-runner.ps1

+37-10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ Write-Host "Retrieved $ssm_config_path/enable_cloudwatch parameter - ($enable_c
5151
$agent_mode=$parameters.where( {$_.Name -eq "$ssm_config_path/agent_mode"}).value
5252
Write-Host "Retrieved $ssm_config_path/agent_mode parameter - ($agent_mode)"
5353

54+
$enable_jit_config=$parameters.where( {$_.Name -eq "$ssm_config_path/enable_jit_config"}).value
55+
Write-Host "Retrieved $ssm_config_path/enable_jit_config parameter - ($enable_jit_config)"
56+
5457
$token_path=$parameters.where( {$_.Name -eq "$ssm_config_path/token_path"}).value
5558
Write-Host "Retrieved $ssm_config_path/token_path parameter - ($token_path)"
5659

@@ -107,11 +110,11 @@ foreach ($group in @("Administrators", "docker-users")) {
107110
Set-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -Name ConsentPromptBehaviorAdmin -Value 0 -Force
108111
Write-Host "Disabled User Access Control (UAC)"
109112

110-
$configCmd = ".\config.cmd --unattended --name $runner_name_prefix$InstanceId --work `"_work`" $config"
111-
Write-Host "Configure GH Runner as user $run_as"
112-
Invoke-Expression $configCmd
113-
114-
Write-Host "Starting the runner as user $run_as"
113+
if ($enable_jit_config -eq "false" -or $agent_mode -ne "ephemeral") {
114+
$configCmd = ".\config.cmd --unattended --name $runner_name_prefix$InstanceId --work `"_work`" $config"
115+
Write-Host "Configure GH Runner (non ephmeral / no JIT) as user $run_as"
116+
Invoke-Expression $configCmd
117+
}
115118

116119
$jsonBody = @(
117120
@{
@@ -121,10 +124,34 @@ $jsonBody = @(
121124
)
122125
ConvertTo-Json -InputObject $jsonBody | Set-Content -Path "$pwd\.setup_info"
123126

124-
Write-Host "Installing the runner as a service"
125127

126-
$action = New-ScheduledTaskAction -WorkingDirectory "$pwd" -Execute "run.cmd"
127-
$trigger = Get-CimClass "MSFT_TaskRegistrationTrigger" -Namespace "Root/Microsoft/Windows/TaskScheduler"
128-
Register-ScheduledTask -TaskName "runnertask" -Action $action -Trigger $trigger -User $username -Password $password -RunLevel Highest -Force
129-
Write-Host "Starting the runner in persistent mode"
128+
Write-Host "Starting the runner in $agent_mode mode"
130129
Write-Host "Starting runner after $(((get-date) - (gcim Win32_OperatingSystem).LastBootUpTime).tostring("hh':'mm':'ss''"))"
130+
131+
if ($agent_mode -eq "ephemeral") {
132+
if ($enable_jit_config -eq "true") {
133+
Write-Host "Starting with jit config"
134+
Invoke-Expression ".\run.cmd --jitconfig $${config}"
135+
}
136+
else {
137+
Write-Host "Starting without jit config"
138+
Invoke-Expression ".\run.cmd"
139+
}
140+
Write-Host "Runner has finished"
141+
142+
if ($enable_cloudwatch_agent)
143+
{
144+
Write-Host "Stopping CloudWatch Agent"
145+
& 'C:\Program Files\Amazon\AmazonCloudWatchAgent\amazon-cloudwatch-agent-ctl.ps1' -a stop
146+
}
147+
148+
Write-Host "Terminating instance"
149+
aws ec2 terminate-instances --instance-ids "$InstanceId" --region "$Region"
150+
} else {
151+
Write-Host "Installing the runner as a service"
152+
153+
$action = New-ScheduledTaskAction -WorkingDirectory "$pwd" -Execute "run.cmd"
154+
$trigger = Get-CimClass "MSFT_TaskRegistrationTrigger" -Namespace "Root/Microsoft/Windows/TaskScheduler"
155+
Register-ScheduledTask -TaskName "runnertask" -Action $action -Trigger $trigger -User $username -Password $password -RunLevel Highest -Force
156+
Write-Host "Starting runner after $(((get-date) - (gcim Win32_OperatingSystem).LastBootUpTime).tostring("hh':'mm':'ss''"))"
157+
}

0 commit comments

Comments
 (0)