|
4 | 4 | function Tag-InstanceWithRunnerId {
|
5 | 5 | Write-Host "Checking for .runner file to extract agent ID"
|
6 | 6 |
|
| 7 | + # Note: $pwd is usually C:\actions-runner since the Scheduled Task that calls |
| 8 | + # start-runner.ps1 sets that as the working directory. |
7 | 9 | $runnerFilePath = "$pwd\.runner"
|
8 | 10 | if (-not (Test-Path $runnerFilePath)) {
|
9 | 11 | Write-Host "Warning: .runner file not found"
|
@@ -177,30 +179,52 @@ ConvertTo-Json -InputObject $jsonBody | Set-Content -Path "$pwd\.setup_info"
|
177 | 179 | Write-Host "Starting the runner in $agent_mode mode"
|
178 | 180 | Write-Host "Starting runner after $(((get-date) - (gcim Win32_OperatingSystem).LastBootUpTime).tostring("hh':'mm':'ss''"))"
|
179 | 181 |
|
| 182 | +$taskExecutable = "run.cmd" |
| 183 | +$taskArgument = $null |
| 184 | + |
180 | 185 | 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 | + } |
181 | 192 | 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" |
184 | 197 | }
|
185 | 198 | 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" |
188 | 201 | }
|
189 |
| - Write-Host "Runner has finished" |
190 | 202 |
|
191 | 203 | if ($enable_cloudwatch_agent)
|
192 | 204 | {
|
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" |
195 | 211 | }
|
196 | 212 |
|
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 | +} |
201 | 219 |
|
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" |
206 | 227 | }
|
| 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''"))" |
0 commit comments