Skip to content

Instantly share code, notes, and snippets.

@Antebios
Created October 22, 2021 16:11
Show Gist options
  • Save Antebios/735c7c344945b5fe24cbe1ad6394ed84 to your computer and use it in GitHub Desktop.
Save Antebios/735c7c344945b5fe24cbe1ad6394ed84 to your computer and use it in GitHub Desktop.
Install GitHub Runner Silently
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[string]$AgentDirectory="C:\GH-Agent1",
[Parameter(Mandatory=$true)]
[string]$OrganizationName,
[Parameter(Mandatory=$true)]
[string]$RepoName,
[Parameter(Mandatory=$true)]
[string]$PAT,
[Parameter(Mandatory=$true)]
[string]$AgentName,
[Parameter(Mandatory=$true)]
[string]$ServiceAccountLogin,
[string]$Labels
)
$GitHubRunnerUrl='https://github.com/actions/runner/releases/download/v2.283.2/actions-runner-win-x64-2.283.2.zip'
[securestring]$ServiceAccountPassword = (
Read-Host -prompt "Enter password for service account $ServiceAccountLogin" -AsSecureString
)
# create directory
Write-Host "Creating Agent directory at: $AgentDirectory"
if (Test-Path $AgentDirectory) { remove-item $AgentDirectory }
New-Item -ItemType Directory $AgentDirectory -Force | Write-Verbose
# download zip package
$RunnerArchiveLocalPath=Join-Path $PSScriptRoot "actions-runner-win-x64.zip"
if (-Not(Test-Path $RunnerArchiveLocalPath)) {
Invoke-WebRequest $GitHubRunnerUrl -OutFile $RunnerArchiveLocalPath
} else {
Write-Host "GitHub Runner already exists at: $RunnerArchiveLocalPath"
Write-Host "Downloading of GitHub Runner is skipped."
}
# Unzip package
Write-Host "Expanding GitHub Runner to: $AgentDirectory"
Expand-Archive -Path $RunnerArchiveLocalPath -DestinationPath $AgentDirectory
Set-Location "$AgentDirectory"
$svcPassword = [System.Net.NetworkCredential]::new('', $ServiceAccountPassword).Password
# Configure GitHub Runner silently
& .\config.cmd --unattended --url https://github.com/$OrganizationName/$RepoName `
--token $PAT --name $AgentName --labels $Labels `
--replace --runAsService --windowsLogonAccount $ServiceAccountLogin --windowsLogonPassword $svcPassword
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment