Skip to content

Commit

Permalink
feat: Added the AMI to machine setup info to runner workflows. (phili…
Browse files Browse the repository at this point in the history
…ps-labs#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]>
  • Loading branch information
4 people authored Oct 7, 2022
1 parent 97707c2 commit e197cbd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
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 | Set-Content -Path "$pwd\.setup_info"

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
[
{
"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

0 comments on commit e197cbd

Please sign in to comment.