|
1 | 1 |
|
2 | 2 | ## Retrieve instance metadata |
3 | 3 |
|
| 4 | +function Tag-InstanceWithRunnerId { |
| 5 | + Write-Host "Checking for .runner file to extract agent ID" |
| 6 | + |
| 7 | + $runnerFilePath = "$pwd\.runner" |
| 8 | + if (-not (Test-Path $runnerFilePath)) { |
| 9 | + Write-Host "Warning: .runner file not found" |
| 10 | + return $true |
| 11 | + } |
| 12 | + |
| 13 | + Write-Host "Found .runner file, extracting agent ID" |
| 14 | + try { |
| 15 | + $runnerConfig = Get-Content $runnerFilePath | ConvertFrom-Json |
| 16 | + $agentId = $runnerConfig.agentId |
| 17 | + |
| 18 | + if (-not $agentId -or $agentId -eq $null) { |
| 19 | + Write-Host "Warning: Could not extract agent ID from .runner file" |
| 20 | + return $true |
| 21 | + } |
| 22 | + |
| 23 | + Write-Host "Tagging instance with GitHub runner agent ID: $agentId" |
| 24 | + $tagResult = aws ec2 create-tags --region "$Region" --resources "$InstanceId" --tags "Key=ghr:github_runner_id,Value=$agentId" 2>&1 |
| 25 | + |
| 26 | + if ($LASTEXITCODE -eq 0) { |
| 27 | + Write-Host "Successfully tagged instance with agent ID: $agentId" |
| 28 | + return $true |
| 29 | + } else { |
| 30 | + Write-Host "Warning: Failed to tag instance with agent ID - $tagResult" |
| 31 | + return $true |
| 32 | + } |
| 33 | + } |
| 34 | + catch { |
| 35 | + Write-Host "Warning: Error processing .runner file - $($_.Exception.Message)" |
| 36 | + return $true |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +## Retrieve instance metadata |
| 41 | + |
4 | 42 | Write-Host "Retrieving TOKEN from AWS API" |
5 | 43 | $token=Invoke-RestMethod -Method PUT -Uri "http://169.254.169.254/latest/api/token" -Headers @{"X-aws-ec2-metadata-token-ttl-seconds" = "180"} |
6 | 44 | if ( ! $token ) { |
@@ -122,6 +160,9 @@ if ($enable_jit_config -eq "false" -or $agent_mode -ne "ephemeral") { |
122 | 160 | $configCmd = ".\config.cmd --unattended --name $runner_name_prefix$InstanceId --work `"_work`" $runnerExtraOptions $config" |
123 | 161 | Write-Host "Configure GH Runner (non ephmeral / no JIT) as user $run_as" |
124 | 162 | Invoke-Expression $configCmd |
| 163 | + |
| 164 | + # Tag instance with GitHub runner agent ID for non-JIT runners |
| 165 | + Tag-InstanceWithRunnerId |
125 | 166 | } |
126 | 167 |
|
127 | 168 | $jsonBody = @( |
|
0 commit comments