Skip to content

Reproduce missing Git executable for SDK #2

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

Closed
wants to merge 10 commits into from
51 changes: 51 additions & 0 deletions .github/workflows/sdk-missing-git-executable.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
on: push

jobs:
externals-dir:
runs-on: dennis-windows-arm64
steps:
- name: list env variables and externals
run: |
Get-ChildItem -Path Env:
Write-Host "Workspace path: $env:GITHUB_WORKSPACE"
Write-Host "Temp path: $env:RUNNER_TEMP"
Write-Host "Tool cache path: $env:RUNNER_TOOL_CACHE"
Get-ChildItem -Path C:\a\externals -Recurse | Select-String -Pattern 'git'
Get-ChildItem -Path C:\a\externals -Recurse
missing-git-executable:
runs-on: dennis-windows-arm64
steps:
# This will use the GitHub API since Git isn't installed on the runner
- name: clone git-for-windows-automation
uses: actions/checkout@v4
# This will fail because Git isn't installed on the runner
- uses: dscho/setup-git-for-windows-sdk@fix-on-arm64
continue-on-error: true
with:
flavor: build-installers
architecture: aarch64
cache: false
- name: install GfW
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
uses: dscho/setup-git-for-windows-sdk@fix-on-arm64
with:
flavor: build-installers
architecture: aarch64
cache: false
should-work-on-hosted-runner:
runs-on: windows-latest
steps:
# This should work because the hosted runner has Git installed. Let's see what it logs as its PATH.
- uses: dscho/setup-git-for-windows-sdk@fix-on-arm64
continue-on-error: true
with:
flavor: build-installers
architecture: aarch64
cache: false
143 changes: 0 additions & 143 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 @@ -159,14 +135,6 @@ Write-Output "Enabling Windows Developer Mode..."
Start-Process -Wait "reg" 'add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock" /t REG_DWORD /f /v "AllowDevelopmentWithoutDevLicense" /d "1"'
Write-Output "Enabled Windows developer mode."

# =============================
# MICROSOFT DEFENDER EXCLUSIONS
# =============================

Write-Output "Adding Microsoft Defender Exclusions..."
Add-MpPreference -ExclusionPath "C:\"
Write-Output "Finished adding Microsoft Defender Exclusions."

# ======================
# GIT FOR WINDOWS
# ======================
Expand Down Expand Up @@ -211,114 +179,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."