Skip to content

Commit

Permalink
comment code
Browse files Browse the repository at this point in the history
Signed-off-by: midays <[email protected]>
  • Loading branch information
midays committed Dec 1, 2024
1 parent dd32833 commit 0fd7da7
Showing 1 changed file with 117 additions and 117 deletions.
234 changes: 117 additions & 117 deletions .github/workflows/provision-runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,120 +83,120 @@ jobs:
echo "EC2 Instance ID: ${{ steps.ec2.outputs.ec2-instance-id }}"
echo "Label : ${{ steps.ec2.outputs.label }}"
configure-ec2:
needs: provision-ec2
runs-on: ${{ needs.provision-ec2.outputs.label }}
steps:
- name: Reconfigure Runner Service to Use Non-Admin User
shell: powershell
env:
NONADMIN_PASSWORD: ${{ secrets.nonadmin-password }}
run: |
try {
# Initial delay to allow the GitHub Actions Runner service to start
Write-Output "Waiting for GitHub Actions Runner service to initialize..."
Start-Sleep -Seconds 90
# Print the current user context
Write-Output "Current User Context:"
whoami
# Print all available services after initial wait
Write-Output "Listing all services after initial wait:"
Get-Service | Select-Object -Property Name, Status
# Print the GitHub Actions runner installation directory
Write-Output "Checking runner installation directory..."
if (Test-Path -Path "C:\actions-runner") {
Write-Output "Runner directory found, listing contents..."
Get-ChildItem -Path "C:\actions-runner" -Recurse -ErrorAction SilentlyContinue | Select-Object FullName, Name
} else {
Write-Error "Runner installation directory not found at expected path: C:\actions-runner"
Write-Output "Attempting to reinstall GitHub Actions runner..."
# Attempt to download and install the GitHub Actions runner manually
New-Item -ItemType Directory -Force -Path "C:\actions-runner"
Invoke-WebRequest -Uri "https://github.com/actions/runner/releases/download/v2.292.0/actions-runner-win-x64-2.292.0.zip" -OutFile "C:\actions-runner\actions-runner.zip"
Write-Output "Extracting runner package..."
Expand-Archive -Path "C:\actions-runner\actions-runner.zip" -DestinationPath "C:\actions-runner"
Remove-Item "C:\actions-runner\actions-runner.zip"
Write-Output "Running GitHub Actions runner configuration script..."
Start-Process -FilePath "C:\actions-runner\config.cmd" -ArgumentList "--url https://github.com/YOUR_ORG/YOUR_REPO", "--token YOUR_TOKEN" -Wait
Write-Output "Runner configuration completed."
}
# Print all running processes to check for runner process by keyword
Write-Output "Searching for running processes related to runner..."
Get-Process | Where-Object { $_.ProcessName -like '*runner*' -or $_.ProcessName -like '*actions*' } | Select-Object -Property Id, ProcessName
# Print environment variables to diagnose context issues
Write-Output "Listing environment variables:"
Get-ChildItem Env:
# Inform about stopping the GitHub Actions Runner service
Write-Output "Stopping GitHub Actions Runner service..."
# Initialize retry count and max retry values for attempts to stop and reconfigure the service
$retryCount = 0
$maxRetries = 5
$success = $false
# Loop until the service is successfully reconfigured or maximum retries are reached
while (-not $success -and $retryCount -lt $maxRetries) {
try {
# Get the GitHub Actions Runner service name dynamically using broader wildcard
$serviceName = Get-Service -Name '*runner*' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Name
# If the service is not found, print available services and retry after a delay
if (-not $serviceName) {
Write-Error "GitHub Actions runner service not found. Printing available services..."
Get-Service | Select-Object -Property Name, Status
Write-Error "Retrying in 30 seconds..."
Start-Sleep -Seconds 30
$retryCount++
continue
}
# Stop the runner service forcefully
Stop-Service -Name $serviceName -Force
Write-Output "Service stopped: $serviceName"
# Reconfigure the service to use the non-admin user credentials
Write-Output "Reconfiguring the service to use nonadmin user..."
sc.exe config $serviceName obj= ".\nonadmin" password= "$env:NONADMIN_PASSWORD"
Write-Output "Service reconfiguration command executed."
# Start the GitHub Actions Runner service again
Start-Service -Name $serviceName
Write-Output "Service restarted successfully."
# Verify that the service is running under the non-admin user
$updatedService = Get-Service -Name $serviceName | Select-Object Name, StartName
Write-Output "Service details: $($updatedService.Name) - Running as $($updatedService.StartName)"
# Set success flag if the service is running as non-admin
if ($updatedService.StartName -eq ".\nonadmin") {
$success = $true
} else {
Write-Output "Service is not running as nonadmin, retrying..."
$retryCount++
Start-Sleep -Seconds 30
}
} catch {
# Handle errors during reconfiguration and retry
Write-Output "An error occurred: $_.Exception.Message. Retrying in 30 seconds..."
Start-Sleep -Seconds 30
$retryCount++
}
}
# Throw an error if reconfiguration fails after all retries
if (-not $success) {
throw "Failed to configure the runner service to run as nonadmin user after $maxRetries retries."
}
} catch {
# Handle the case where reconfiguration fails completely
Write-Host "Failed to configure the runner service: $_.Exception.Message"
exit 1
}
# configure-ec2:
# needs: provision-ec2
# runs-on: ${{ needs.provision-ec2.outputs.label }}
# steps:
# - name: Reconfigure Runner Service to Use Non-Admin User
# shell: powershell
# env:
# NONADMIN_PASSWORD: ${{ secrets.nonadmin-password }}
# run: |
# try {
# # Initial delay to allow the GitHub Actions Runner service to start
# Write-Output "Waiting for GitHub Actions Runner service to initialize..."
# Start-Sleep -Seconds 90
#
# # Print the current user context
# Write-Output "Current User Context:"
# whoami
#
# # Print all available services after initial wait
# Write-Output "Listing all services after initial wait:"
# Get-Service | Select-Object -Property Name, Status
#
# # Print the GitHub Actions runner installation directory
# Write-Output "Checking runner installation directory..."
# if (Test-Path -Path "C:\actions-runner") {
# Write-Output "Runner directory found, listing contents..."
# Get-ChildItem -Path "C:\actions-runner" -Recurse -ErrorAction SilentlyContinue | Select-Object FullName, Name
# } else {
# Write-Error "Runner installation directory not found at expected path: C:\actions-runner"
# Write-Output "Attempting to reinstall GitHub Actions runner..."
#
# # Attempt to download and install the GitHub Actions runner manually
# New-Item -ItemType Directory -Force -Path "C:\actions-runner"
# Invoke-WebRequest -Uri "https://github.com/actions/runner/releases/download/v2.292.0/actions-runner-win-x64-2.292.0.zip" -OutFile "C:\actions-runner\actions-runner.zip"
# Write-Output "Extracting runner package..."
# Expand-Archive -Path "C:\actions-runner\actions-runner.zip" -DestinationPath "C:\actions-runner"
# Remove-Item "C:\actions-runner\actions-runner.zip"
#
# Write-Output "Running GitHub Actions runner configuration script..."
# Start-Process -FilePath "C:\actions-runner\config.cmd" -ArgumentList "--url https://github.com/YOUR_ORG/YOUR_REPO", "--token YOUR_TOKEN" -Wait
# Write-Output "Runner configuration completed."
# }
#
# # Print all running processes to check for runner process by keyword
# Write-Output "Searching for running processes related to runner..."
# Get-Process | Where-Object { $_.ProcessName -like '*runner*' -or $_.ProcessName -like '*actions*' } | Select-Object -Property Id, ProcessName
#
# # Print environment variables to diagnose context issues
# Write-Output "Listing environment variables:"
# Get-ChildItem Env:
#
# # Inform about stopping the GitHub Actions Runner service
# Write-Output "Stopping GitHub Actions Runner service..."
#
# # Initialize retry count and max retry values for attempts to stop and reconfigure the service
# $retryCount = 0
# $maxRetries = 5
# $success = $false
#
# # Loop until the service is successfully reconfigured or maximum retries are reached
# while (-not $success -and $retryCount -lt $maxRetries) {
# try {
# # Get the GitHub Actions Runner service name dynamically using broader wildcard
# $serviceName = Get-Service -Name '*runner*' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Name
#
# # If the service is not found, print available services and retry after a delay
# if (-not $serviceName) {
# Write-Error "GitHub Actions runner service not found. Printing available services..."
# Get-Service | Select-Object -Property Name, Status
# Write-Error "Retrying in 30 seconds..."
# Start-Sleep -Seconds 30
# $retryCount++
# continue
# }
#
# # Stop the runner service forcefully
# Stop-Service -Name $serviceName -Force
# Write-Output "Service stopped: $serviceName"
#
# # Reconfigure the service to use the non-admin user credentials
# Write-Output "Reconfiguring the service to use nonadmin user..."
# sc.exe config $serviceName obj= ".\nonadmin" password= "$env:NONADMIN_PASSWORD"
# Write-Output "Service reconfiguration command executed."
#
# # Start the GitHub Actions Runner service again
# Start-Service -Name $serviceName
# Write-Output "Service restarted successfully."
#
# # Verify that the service is running under the non-admin user
# $updatedService = Get-Service -Name $serviceName | Select-Object Name, StartName
# Write-Output "Service details: $($updatedService.Name) - Running as $($updatedService.StartName)"
#
# # Set success flag if the service is running as non-admin
# if ($updatedService.StartName -eq ".\nonadmin") {
# $success = $true
# } else {
# Write-Output "Service is not running as nonadmin, retrying..."
# $retryCount++
# Start-Sleep -Seconds 30
# }
# } catch {
# # Handle errors during reconfiguration and retry
# Write-Output "An error occurred: $_.Exception.Message. Retrying in 30 seconds..."
# Start-Sleep -Seconds 30
# $retryCount++
# }
# }
#
# # Throw an error if reconfiguration fails after all retries
# if (-not $success) {
# throw "Failed to configure the runner service to run as nonadmin user after $maxRetries retries."
# }
# } catch {
# # Handle the case where reconfiguration fails completely
# Write-Host "Failed to configure the runner service: $_.Exception.Message"
# exit 1
# }

0 comments on commit 0fd7da7

Please sign in to comment.