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

Commit e197cbd

Browse files
GuptaNavdeep1983navdeepg2021npalmScottGuymer
authored
feat: Added the AMI to machine setup info to runner workflows. (#2451)
* feat: Added the Machine setup info to runner workflows. * fix: added to windows image as well. * fix: updated the comments. * Update modules/runners/templates/start-runner.ps1 Co-authored-by: Niek Palm <[email protected]> * Update modules/runners/templates/start-runner.sh Co-authored-by: Niek Palm <[email protected]> * Update modules/runners/templates/start-runner.ps1 Co-authored-by: Scott Guymer <[email protected]> Co-authored-by: navdeepg2021 <[email protected]> Co-authored-by: Niek Palm <[email protected]> Co-authored-by: Scott Guymer <[email protected]>
1 parent 97707c2 commit e197cbd

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
Write-Host "Retrieving TOKEN from AWS API"
55
$token=Invoke-RestMethod -Method PUT -Uri "http://169.254.169.254/latest/api/token" -Headers @{"X-aws-ec2-metadata-token-ttl-seconds" = "180"}
66

7+
$ami_id=Invoke-RestMethod -Uri "http://169.254.169.254/latest/meta-data/ami-id" -Headers @{"X-aws-ec2-metadata-token" = $token}
8+
79
$metadata=Invoke-RestMethod -Uri "http://169.254.169.254/latest/dynamic/instance-identity/document" -Headers @{"X-aws-ec2-metadata-token" = $token}
810

911
$Region = $metadata.region
@@ -32,7 +34,7 @@ Write-Host "Retrieved /$environment/runner/agent-mode parameter - ($agent_mode)
3234

3335
if ($enable_cloudwatch_agent -eq "true")
3436
{
35-
Write-Host "Enabling CloudWatch Agent"
37+
Write-Host "Enabling CloudWatch Agent"
3638
& 'C:\Program Files\Amazon\AmazonCloudWatchAgent\amazon-cloudwatch-agent-ctl.ps1' -a fetch-config -m ec2 -s -c "ssm:$environment-cloudwatch_agent_config_runner"
3739
}
3840

@@ -42,7 +44,7 @@ Write-Host "Get GH Runner config from AWS SSM"
4244
$config = $null
4345
$i = 0
4446
do {
45-
$config = (aws ssm get-parameters --names "$environment-$InstanceId" --with-decryption --region $Region --query "Parameters[*].{Name:Name,Value:Value}" | ConvertFrom-Json)[0].value
47+
$config = (aws ssm get-parameters --names "$environment-$InstanceId" --with-decryption --region $Region --query "Parameters[*].{Name:Name,Value:Value}" | ConvertFrom-Json)[0].value
4648
Write-Host "Waiting for GH Runner config to become available in AWS SSM ($i/30)"
4749
Start-Sleep 1
4850
$i++
@@ -88,10 +90,18 @@ Invoke-Expression $configCmd
8890

8991
Write-Host "Starting the runner as user $run_as"
9092

93+
$jsonBody = @(
94+
@{
95+
group='Runner Image'
96+
details="AMI id: $ami_id"
97+
}
98+
)
99+
ConvertTo-Json -InputObject $jsonBody | Set-Content -Path "$pwd\.setup_info"
100+
91101
Write-Host "Installing the runner as a service"
92102

93103
$action = New-ScheduledTaskAction -WorkingDirectory "$pwd" -Execute "run.cmd"
94104
$trigger = Get-CimClass "MSFT_TaskRegistrationTrigger" -Namespace "Root/Microsoft/Windows/TaskScheduler"
95105
Register-ScheduledTask -TaskName "runnertask" -Action $action -Trigger $trigger -User $username -Password $password -RunLevel Highest -Force
96106
Write-Host "Starting the runner in persistent mode"
97-
Write-Host "Starting runner after $(((get-date) - (gcim Win32_OperatingSystem).LastBootUpTime).tostring("hh':'mm':'ss''"))"
107+
Write-Host "Starting runner after $(((get-date) - (gcim Win32_OperatingSystem).LastBootUpTime).tostring("hh':'mm':'ss''"))"

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

+19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
echo "Retrieving TOKEN from AWS API"
66
token=$(curl -f -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 180")
77

8+
ami_id=$(curl -f -H "X-aws-ec2-metadata-token: $token" -v http://169.254.169.254/latest/meta-data/ami-id)
9+
810
region=$(curl -f -H "X-aws-ec2-metadata-token: $token" -v http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region)
911
echo "Retrieved REGION from AWS API ($region)"
1012

@@ -60,6 +62,23 @@ chown -R $run_as .
6062
echo "Configure GH Runner as user $run_as"
6163
sudo --preserve-env=RUNNER_ALLOW_RUNASROOT -u "$run_as" -- ./config.sh --unattended --name "$instance_id" --work "_work" $${config}
6264

65+
info_arch=$(uname -p)
66+
info_os=$(( lsb_release -ds || cat /etc/*release || uname -om ) 2>/dev/null | head -n1 | cut -d "=" -f2- | tr -d '"')
67+
68+
tee /opt/actions-runner/.setup_info <<EOL
69+
[
70+
{
71+
"group": "Operating System",
72+
"detail": "Distribution: $info_os\nArchitecture: $info_arch"
73+
},
74+
{
75+
"group": "Runner Image",
76+
"detail": "AMI id: $ami_id"
77+
}
78+
]
79+
EOL
80+
81+
6382
## Start the runner
6483
echo "Starting runner after $(awk '{print int($1/3600)":"int(($1%3600)/60)":"int($1%60)}' /proc/uptime)"
6584
echo "Starting the runner as user $run_as"

0 commit comments

Comments
 (0)