Skip to content

Commit

Permalink
Install GfW if the first attempt fails
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisameling committed Sep 9, 2024
1 parent 95d3d65 commit 34bf76d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 135 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/sdk-missing-git-executable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,20 @@ jobs:
with:
flavor: build-installers
architecture: aarch64
- name: install GfW
id: install-gfw
if: failure()
shell: powershell
run: |
& ./azure-self-hosted-runners/post-deployment-script.ps1
Add-Content $env:GITHUB_PATH "C:\Program Files\Git\bin"
$currentPath = [System.Environment]::GetEnvironmentVariable("PATH", "Machine")
$newPath = 'C:\Program Files\Git\bin;' + $currentPath
[Environment]::SetEnvironmentVariable("PATH", $newPath, "Machine")
- name: try installing the SDK again
if: failure()
needs: install-gfw
uses: dennisameling/setup-git-for-windows-sdk@e95ed5e0269958503b1ffbd56a5859c8f26d8a2b
with:
flavor: build-installers
architecture: aarch64
135 changes: 0 additions & 135 deletions azure-self-hosted-runners/post-deployment-script.ps1
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
param (
# https://docs.github.com/en/actions/hosting-your-own-runners/adding-self-hosted-runners
[Parameter(Mandatory = $true, HelpMessage = "GitHub Actions Runner registration token. Note that these tokens are only valid for one hour after creation, so we always expect the user to provide one.")]
[string]$GitHubActionsRunnerToken,

# GitHub Actions Runner repository. E.g. "https://github.com/MY_ORG" (org-level) or "https://github.com/MY_ORG/MY_REPO" (repo-level)
# https://docs.github.com/en/actions/hosting-your-own-runners/adding-self-hosted-runners
[Parameter(Mandatory = $true)]
[ValidateScript({ $_ -like "https://*" })]
[string]$GithubActionsRunnerRegistrationUrl,

[Parameter(Mandatory = $true, HelpMessage = "Name of the runner. Needs to be unique in the org/repo")]
[ValidateNotNullOrEmpty()]
[string]$GithubActionsRunnerName,

[Parameter(Mandatory = $false, HelpMessage = "Stop Service immediately (useful for spinning up runners preemptively)")]
[ValidateSet('true', 'false')]
[string]$StopService = 'true',

[Parameter(Mandatory = $true, HelpMessage = "Path to the Actions Runner. Keep this path short to prevent Long Path issues, e.g. D:\a")]
[ValidateNotNullOrEmpty()]
[string]$GitHubActionsRunnerPath
)

Write-Output "Starting post-deployment script."

# =================================
Expand Down Expand Up @@ -211,114 +187,3 @@ EnableFSMonitor=Disabled
Start-Process -Wait $GitHubGit.OutFile '/VERYSILENT /NORESTART /NOCANCEL /SP- /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS /LOADINF="./git-installer-config.inf"'

Write-Output "Finished installing Git for Windows."

# ======================
# PWSH (PowerShell)
# ======================

Write-Output "Downloading pwsh..."

$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -UseBasicParsing -Uri $GitHubPwsh.DownloadUrl -OutFile $GitHubPwsh.OutFile
$ProgressPreference = 'Continue'

if ((Get-FileHash -Path $GitHubPwsh.OutFile -Algorithm SHA256).Hash.ToUpper() -ne $GitHubPwsh.Hash) {
Write-Error "Computed checksum for $($GitHubPwsh.OutFile) did not match $($GitHubPwsh.Hash)"
exit 1
}

Write-Output "Installing pwsh..."

# Get the full path to the MSI in the current working directory
$MsiPath = Resolve-Path $GitHubPwsh.OutFile

# Define arguments for silent installation
$MsiArguments = "/qn /i `"$MsiPath`" ADD_PATH=1"

# Install pwsh using msiexec
Start-Process msiexec.exe -Wait -ArgumentList $MsiArguments

# TODO remove once 7.5.0 is out
Write-Output "Copying pwsh-preview.cmd to pwsh.cmd as a temporary measure until 7.5.0 is out..."
Copy-Item "C:\Program Files\PowerShell\7-preview\preview\pwsh-preview.cmd" "C:\Program Files\PowerShell\7-preview\preview\pwsh.cmd"

Write-Output "Finished installing pwsh."

# ======================
# GITHUB ACTIONS RUNNER
# ======================

Write-Output "Downloading GitHub Actions runner..."

mkdir $GitHubActionsRunnerPath | Out-Null
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -UseBasicParsing -Uri $GitHubAction.DownloadUrl -OutFile $GitHubAction.OutFile
$ProgressPreference = 'Continue'

if ((Get-FileHash -Path $GitHubAction.OutFile -Algorithm SHA256).Hash.ToUpper() -ne $GitHubAction.hash) {
Write-Error "Computed checksum for $($GitHubAction.OutFile) did not match $($GitHubAction.hash)"
exit 1
}

Write-Output "Installing GitHub Actions runner $($GitHubAction.Tag) as a Windows service with labels $($GitHubAction.RunnerLabels)..."

Add-Type -AssemblyName System.IO.Compression.FileSystem ; [System.IO.Compression.ZipFile]::ExtractToDirectory($GitHubAction.OutFile, $GitHubActionsRunnerPath)

Write-Output "Configuring the runner to shut down automatically after running"
Set-Content -Path "${GitHubActionsRunnerPath}\shut-down.ps1" -Value "shutdown -s -t 60 -d p:4:0 -c `"workflow job is done`""
[System.Environment]::SetEnvironmentVariable("ACTIONS_RUNNER_HOOK_JOB_COMPLETED", "${GitHubActionsRunnerPath}\shut-down.ps1", [System.EnvironmentVariableTarget]::Machine)

Write-Output "Configuring the runner"
cmd.exe /c "${GitHubActionsRunnerPath}\config.cmd" --unattended --ephemeral --name ${GithubActionsRunnerName} --runasservice --labels $($GitHubAction.RunnerLabels) --url ${GithubActionsRunnerRegistrationUrl} --token ${GitHubActionsRunnerToken}

# Ensure that the service was created. If not, exit with error code.
if ($null -eq (Get-Service -Name "actions.runner.*")) {
Write-Output "Could not find service actions.runner.*, making three more attempts with a 3 second delay in between each attempt..."

[int]$RetryCountService = 0
do {
Write-Output "Attempt $($RetryCountService) of 3: Looking for service actions.runner.*..."
$RetryCountService++
Start-Sleep -Seconds 3
}
while ($null -eq (Get-Service -Name "actions.runner.*") -or $RetryCountService -gt 3)

if ($RetryCountService -gt 3) {
Write-Error "GitHub Actions service not found (should start with actions.runner). Check the logs in ${GitHubActionsRunnerPath}\_diag for more details."
exit 1
}
else {
Write-Output "Found service actions.runner.*"
}
}

# Immediately stop the service as we want to leave the VM in a deallocated state for later use. The service will automatically be started when Windows starts.
if ($StopService -eq 'true') {
#Collects all running services named actions.runner.*
$GetActionRunnerServices = Get-Service -Name "actions.runner.*" | Where-Object { $_.Status -eq 'Running' } | Select-Object -ExpandProperty Name

# Loops trough all services and stopping them one by one
foreach ($Service in $GetActionRunnerServices) {
Write-Output "Stopping service $Service"
Stop-Service -Name $Service

# Making sure that all of the services has been stopped before moving forward
[int]$RetryCount = 0
do {
Write-Output "Attempt: $($RetryCount) of 5: Waiting for service $Service to stop..."
$RetryCount++
Start-Sleep -Seconds 5
}
while ((Get-Service -Name $Service).Status -eq 'running' -or $RetryCount -gt 5)

if ($RetryCount -gt 5) {
Write-Error "Service $Service failed to stop"
exit 1
}
else {
Write-Output "Service $Service has been stopped"
}
}
}

Write-Output "Finished installing GitHub Actions runner."

0 comments on commit 34bf76d

Please sign in to comment.