Skip to content

Commit 1444165

Browse files
committed
fix: start-runner.ps1 set username
1 parent 92710de commit 1444165

File tree

4 files changed

+57
-25
lines changed

4 files changed

+57
-25
lines changed

images/windows-core-2019/windows-provisioner.ps1

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,20 @@ Set-Content -Path "$PsHome\Microsoft.PowerShell_profile.ps1" -Value $ChocoProfil
2222

2323
refreshenv
2424

25-
Write-Host "Installing cloudwatch agent..."
25+
Write-Host "Installing cloudwatch agent, part 1"
2626
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
3027

3128
# Install dependent tools
3229
Write-Host "Installing additional development tools"
3330
choco install git awscli -y
3431
refreshenv
3532

33+
Write-Host "Installing cloudwatch agent, part 2"
34+
# Delayed part 2 to ensure the download from part 1 completed and was written to disk
35+
$cloudwatchParams = '/i', 'C:\amazon-cloudwatch-agent.msi', '/qn', '/L*v', 'C:\CloudwatchInstall.log'
36+
Start-Process "msiexec.exe" $cloudwatchParams -Wait -NoNewWindow
37+
Remove-Item C:\amazon-cloudwatch-agent.msi
38+
3639
Write-Host "Creating actions-runner directory for the GH Action installtion"
3740
New-Item -ItemType Directory -Path C:\actions-runner ; Set-Location C:\actions-runner
3841

@@ -49,4 +52,4 @@ $action = New-ScheduledTaskAction -WorkingDirectory "C:\actions-runner" -Execute
4952
$trigger = New-ScheduledTaskTrigger -AtStartup
5053
Register-ScheduledTask -TaskName "runnerinit" -Action $action -Trigger $trigger -User System -RunLevel Highest -Force
5154

52-
C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 -Schedule
55+
C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 -Schedule

images/windows-core-2022/windows-provisioner.ps1

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,20 @@ Set-Content -Path "$PsHome\Microsoft.PowerShell_profile.ps1" -Value $ChocoProfil
2222

2323
refreshenv
2424

25-
Write-Host "Installing cloudwatch agent..."
25+
Write-Host "Installing cloudwatch agent, part 1"
2626
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
3027

3128
# Install dependent tools
3229
Write-Host "Installing additional development tools"
3330
choco install git awscli -y
3431
refreshenv
3532

33+
Write-Host "Installing cloudwatch agent, part 2"
34+
# Delayed part 2 to ensure the download from part 1 completed and was written to disk
35+
$cloudwatchParams = '/i', 'C:\amazon-cloudwatch-agent.msi', '/qn', '/L*v', 'C:\CloudwatchInstall.log'
36+
Start-Process "msiexec.exe" $cloudwatchParams -Wait -NoNewWindow
37+
Remove-Item C:\amazon-cloudwatch-agent.msi
38+
3639
Write-Host "Creating actions-runner directory for the GH Action installtion"
3740
New-Item -ItemType Directory -Path C:\actions-runner ; Set-Location C:\actions-runner
3841

@@ -49,4 +52,4 @@ $action = New-ScheduledTaskAction -WorkingDirectory "C:\actions-runner" -Execute
4952
$trigger = New-ScheduledTaskTrigger -AtStartup
5053
Register-ScheduledTask -TaskName "runnerinit" -Action $action -Trigger $trigger -User System -RunLevel Highest -Force
5154

52-
C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 -Schedule
55+
C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 -Schedule

modules/runners/templates/start-runner.ps1

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
function Tag-InstanceWithRunnerId {
55
Write-Host "Checking for .runner file to extract agent ID"
66

7+
# Note: $pwd is usually C:\actions-runner since the Scheduled Task that calls
8+
# start-runner.ps1 sets that as the working directory.
79
$runnerFilePath = "$pwd\.runner"
810
if (-not (Test-Path $runnerFilePath)) {
911
Write-Host "Warning: .runner file not found"
@@ -177,30 +179,52 @@ ConvertTo-Json -InputObject $jsonBody | Set-Content -Path "$pwd\.setup_info"
177179
Write-Host "Starting the runner in $agent_mode mode"
178180
Write-Host "Starting runner after $(((get-date) - (gcim Win32_OperatingSystem).LastBootUpTime).tostring("hh':'mm':'ss''"))"
179181

182+
$taskExecutable = "run.cmd"
183+
$taskArgument = $null
184+
180185
if ($agent_mode -eq "ephemeral") {
186+
$startRunnerService = "start-runner-service.ps1"
187+
$taskExecutable = "PowerShell.exe"
188+
$taskArgument = "-File $startRunnerService"
189+
if (Test-Path $startRunnerService) {
190+
Remove-Item "$startRunnerService"
191+
}
181192
if ($enable_jit_config -eq "true") {
182-
Write-Host "Starting with jit config"
183-
Invoke-Expression ".\run.cmd --jitconfig $${config}"
193+
Write-Output 'Write-Host "Starting with jit config"' | Out-File -Append -FilePath "$startRunnerService"
194+
# Note: the double dollar signs are an artifact of Terraform since a single dollar sign and bracket would be
195+
# interpreted by the template.
196+
Write-Output ".\run.cmd --jitconfig $${config}" | Out-File -Append -FilePath "$startRunnerService"
184197
}
185198
else {
186-
Write-Host "Starting without jit config"
187-
Invoke-Expression ".\run.cmd"
199+
Write-Output 'Write-Host "Starting without jit config"' | Out-File -Append -FilePath "$startRunnerService"
200+
Write-Output ".\run.cmd" | Out-File -Append -FilePath "$startRunnerService"
188201
}
189-
Write-Host "Runner has finished"
190202

191203
if ($enable_cloudwatch_agent)
192204
{
193-
Write-Host "Stopping CloudWatch Agent"
194-
& 'C:\Program Files\Amazon\AmazonCloudWatchAgent\amazon-cloudwatch-agent-ctl.ps1' -a stop
205+
$outputstring = @"
206+
Write-Host `"Stopping CloudWatch Agent`"
207+
& 'C:\Program Files\Amazon\AmazonCloudWatchAgent\amazon-cloudwatch-agent-ctl.ps1' -a stop
208+
"@
209+
210+
$outputstring | Out-File -Append -FilePath "$startRunnerService"
195211
}
196212

197-
Write-Host "Terminating instance"
198-
aws ec2 terminate-instances --instance-ids "$InstanceId" --region "$Region"
199-
} else {
200-
Write-Host "Installing the runner as a service"
213+
$outputstring = @"
214+
Write-Host `"Terminating instance`"
215+
aws ec2 terminate-instances --instance-ids `"$InstanceId`" --region `"$Region`"
216+
"@
217+
$outputstring | Out-File -Append -FilePath "$startRunnerService"
218+
}
201219

202-
$action = New-ScheduledTaskAction -WorkingDirectory "$pwd" -Execute "run.cmd"
203-
$trigger = Get-CimClass "MSFT_TaskRegistrationTrigger" -Namespace "Root/Microsoft/Windows/TaskScheduler"
204-
Register-ScheduledTask -TaskName "runnertask" -Action $action -Trigger $trigger -User $username -Password $password -RunLevel Highest -Force
205-
Write-Host "Starting runner after $(((get-date) - (gcim Win32_OperatingSystem).LastBootUpTime).tostring("hh':'mm':'ss''"))"
220+
Write-Host "Installing the runner as a service"
221+
222+
if ( $taskArgument ) {
223+
$action = New-ScheduledTaskAction -WorkingDirectory "$pwd" -Execute "$taskExecutable" -Argument "$taskArgument"
224+
}
225+
else {
226+
$action = New-ScheduledTaskAction -WorkingDirectory "$pwd" -Execute "$taskExecutable"
206227
}
228+
$trigger = Get-CimClass "MSFT_TaskRegistrationTrigger" -Namespace "Root/Microsoft/Windows/TaskScheduler"
229+
Register-ScheduledTask -TaskName "runnertask" -Action $action -Trigger $trigger -User $username -Password $password -RunLevel Highest -Force
230+
Write-Host "Starting runner after $(((get-date) - (gcim Win32_OperatingSystem).LastBootUpTime).tostring("hh':'mm':'ss''"))"

modules/runners/templates/start-runner.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ if [[ "$enable_jit_config" == "false" || $agent_mode != "ephemeral" ]]; then
254254
else
255255
extra_flags=""
256256
fi
257+
# Note: the double dollar signs are an artifact of Terraform since a single dollar sign and bracket would be
258+
# interpreted by the template
257259
sudo --preserve-env=RUNNER_ALLOW_RUNASROOT -u "$run_as" -- ./config.sh $${extra_flags} --unattended --name "$runner_name_prefix$instance_id" --work "_work" $${config}
258260

259261
# Tag instance with GitHub runner agent ID for non-JIT runners

0 commit comments

Comments
 (0)