Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added the AMI to machine setup info to runner workflows. #2451

Merged
merged 7 commits into from
Oct 7, 2022
Merged
16 changes: 13 additions & 3 deletions modules/runners/templates/start-runner.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
Write-Host "Retrieving TOKEN from AWS API"
$token=Invoke-RestMethod -Method PUT -Uri "http://169.254.169.254/latest/api/token" -Headers @{"X-aws-ec2-metadata-token-ttl-seconds" = "180"}

$ami_id=Invoke-RestMethod -Uri "http://169.254.169.254/latest/meta-data/ami-id" -Headers @{"X-aws-ec2-metadata-token" = $token}

$metadata=Invoke-RestMethod -Uri "http://169.254.169.254/latest/dynamic/instance-identity/document" -Headers @{"X-aws-ec2-metadata-token" = $token}

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

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

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

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

$jsonBody = @(
@{
group='Runner Image'
details="AMI id: $ami_id"
}
)
ConvertTo-Json -InputObject $jsonBody > "$pwd\.setup_info"
GuptaNavdeep1983 marked this conversation as resolved.
Show resolved Hide resolved

Write-Host "Installing the runner as a service"

$action = New-ScheduledTaskAction -WorkingDirectory "$pwd" -Execute "run.cmd"
$trigger = Get-CimClass "MSFT_TaskRegistrationTrigger" -Namespace "Root/Microsoft/Windows/TaskScheduler"
Register-ScheduledTask -TaskName "runnertask" -Action $action -Trigger $trigger -User $username -Password $password -RunLevel Highest -Force
Write-Host "Starting the runner in persistent mode"
Write-Host "Starting runner after $(((get-date) - (gcim Win32_OperatingSystem).LastBootUpTime).tostring("hh':'mm':'ss''"))"
Write-Host "Starting runner after $(((get-date) - (gcim Win32_OperatingSystem).LastBootUpTime).tostring("hh':'mm':'ss''"))"
19 changes: 19 additions & 0 deletions modules/runners/templates/start-runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
echo "Retrieving TOKEN from AWS API"
token=$(curl -f -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 180")

ami_id=$(curl -f -H "X-aws-ec2-metadata-token: $token" -v http://169.254.169.254/latest/meta-data/ami-id)

region=$(curl -f -H "X-aws-ec2-metadata-token: $token" -v http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region)
echo "Retrieved REGION from AWS API ($region)"

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

info_arch=$(uname -p)
info_os=$(( lsb_release -ds || cat /etc/*release || uname -om ) 2>/dev/null | head -n1 | cut -d "=" -f2- | tr -d '"')

tee /opt/actions-runner/.setup_info <<EOL
GuptaNavdeep1983 marked this conversation as resolved.
Show resolved Hide resolved
[
{
"group": "Operating System",
"detail": "Distribution: $info_os\nArchitecture: $info_arch"
},
{
"group": "Runner Image",
"detail": "AMI id: $ami_id"
}
]
EOL


## Start the runner
echo "Starting runner after $(awk '{print int($1/3600)":"int(($1%3600)/60)":"int($1%60)}' /proc/uptime)"
echo "Starting the runner as user $run_as"
Expand Down