Created
June 17, 2020 15:33
-
-
Save voronenko-p/1bee2f8545b9ec62512a51b5eef8bd39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# register default runner with powershell | |
# register-runner -gitRegistrationToken SPECIFY -hostTags "windows" | |
# register oldschool runner with windows cmd | |
# register-runner -gitRegistrationToken SPECIFY -hostTags "windows" -gitlab_executor "shell" -gitlab_shell "cmd" | |
# install runner service as localsystem | |
# gitlab-service-register | |
# install runner service on behalf of specific user | |
# gitlab-service-register -gitlab_runner_username "ieuser" -gitlab_runner_pass "Passw0rd!" | |
new-module -name GitlabRunnerRegister -scriptblock { | |
[Console]::OutputEncoding = New-Object -typename System.Text.ASCIIEncoding | |
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls,Tls11,Tls12' | |
function GitlabRunnerGetSelfSignedCert { | |
param ( | |
[string]$gitlabUrl = "https://gitlab.voronenko.net", | |
[string]$etcHostsEntry = "192.168.3.299`gitlab.voronenko.net" | |
) | |
If (![string]::IsNullOrEmpty($hostTags)) { | |
Add-Content -Value $etcHostsEntry -Path C:\Windows\system32\drivers\etc\hosts | |
} | |
$localCertPath = "$env:temp\$($gitlabUrl.Split('/')[2]).crt" | |
$webRequest = [Net.WebRequest]::Create($gitlabUrl) | |
try { $webRequest.GetResponse() } catch {} # try catch is useful if ssl cert is not valid. ServicePoint is always kept even for invalid ssl cert. | |
$cert = $webRequest.ServicePoint.Certificate | |
$bytes = $cert.Export("Cert") | |
Set-content -value $bytes -encoding byte -path $localCertPath | |
# https://docs.microsoft.com/en-us/windows/desktop/seccertenroll/about-certificate-directory | |
Import-Certificate -FilePath $localCertPath -CertStoreLocation Cert:\LocalMachine\Root | |
} | |
function GitlabRunnerRegister { | |
param ( | |
[ValidateNotNullOrEmpty()] | |
[string]$gitlabUrl = "https://gitlab.com/", | |
[Parameter(Mandatory = $true)] | |
[ValidateNotNullOrEmpty()] | |
[string]$gitRegistrationToken, | |
[Parameter(Mandatory = $false)] | |
[ValidateNotNullOrEmpty()] | |
[string]$gitlab_runner_name = $env:computername, | |
[Parameter(Mandatory = $false)] | |
[string]$gitlab_runner_description = $env:computername + " Experimental windows runner", | |
[Parameter(Mandatory = $true)] | |
[ValidateNotNullOrEmpty()] | |
[string]$hostTags = "windows", | |
[Parameter(Mandatory = $false)] | |
[ValidateNotNullOrEmpty()] | |
[string] $gitlab_builds_dir = "c:/workspaces/builds", | |
[Parameter(Mandatory = $false)] | |
[ValidateNotNullOrEmpty()] | |
[string] $gitlab_caches_dir = "c:/workspaces/caches", | |
[Parameter(Mandatory = $false)] | |
[ValidateNotNullOrEmpty()] | |
[string] $gitlab_executor = "powershell", | |
[Parameter(Mandatory = $false)] | |
[ValidateNotNullOrEmpty()] | |
[string] $gitlab_shell = "", | |
[Parameter(Mandatory = $false)] | |
$gitlab_environment = @('GIT_SSL_NO_VERIFY=true'), | |
[Parameter(Mandatory = $false)] | |
[string]$skipSSLValidation = "false" | |
) | |
Write-Host "Script will register gitlab runner for $gitlabUrl with tags $hostTags runner $gitlab_shell" | |
Set-Location c:\gitlab-runner | |
# Ensure the runner is stopped before the registration. | |
$serviceName = 'gitlab-runner' | |
Write-Host "Stopping $serviceName in case if it is already running" | |
If (Get-Service $serviceName -ErrorAction SilentlyContinue) { | |
If ((Get-Service $serviceName).Status -eq 'Running') { | |
Stop-Service $serviceName | |
Write-Host "Stopping $serviceName" | |
./gitlab-runner.exe stop | |
./gitlab-runner.exe status | |
} Else { | |
Write-Host "$serviceName found, but it is not running." | |
./gitlab-runner.exe status | |
} | |
} Else { | |
Write-Host "$serviceName not found" | |
} | |
./gitlab-runner.exe status | |
# Install without any other params will install a windows service named gitlab-runner running under the built-in system account. | |
#Set-Location c:\gitlab-runner | |
#./gitlab-runner.exe install | |
$parameters="--non-interactive --url " + $gitlabUrl + " --registration-token " + $gitRegistrationToken | |
$parameters += " --executor " + '"' + $gitlab_executor + '"' | |
If ([string]::IsNullOrEmpty($gitlab_shell)) { | |
$parameters += " --shell " + '"' + $gitlab_shell + '"' | |
} | |
$parameters += " --description " + '"' + $gitlab_runner_description + '"' | |
$defaultHostTags = "windows" | |
If ([string]::IsNullOrEmpty($hostTags)) { | |
$hostTags = $defaultHostTags | |
} Else { | |
$hostTags = "$hostTags" | |
} | |
$parameters += " --tag-list " + '"' + $hostTags + '"' | |
$parameters += " --builds-dir " + '"' + $gitlab_builds_dir + '"' | |
$parameters += " --cache-dir " + '"' + $gitlab_caches_dir + '"' | |
Write-Host "=======================================================" | |
Write-Host "gitlab-runner.exe register $parameters" | |
iex "./gitlab-runner.exe register $parameters" | |
If (Get-Service $serviceName -ErrorAction SilentlyContinue) { | |
./gitlab-runner.exe start | |
} | |
} | |
function GitlabServiceRegister { | |
param ( | |
[Parameter(Mandatory = $false)] | |
[string] $gitlab_runner_username = "", | |
[Parameter(Mandatory = $false)] | |
[string] $gitlab_runner_pass = "", | |
[Parameter(Mandatory = $false)] | |
[string] $gitlab_runner_domain = $env:computername | |
) | |
$installparameters ="" | |
If (![string]::IsNullOrEmpty($gitlab_runner_username)) { | |
Write-Host "gitlab-runner.exe install on behalf of specific user" | |
$installparameters += " --user $gitlab_runner_domain" + '\\' + $gitlab_runner_username + " --password " + $gitlab_runner_pass | |
} else { | |
Write-Host "gitlab-runner.exe install on behalf of specific user" | |
} | |
Set-Location c:\gitlab-runner | |
Write-Host "./gitlab-runner.exe install $installparameters" | |
iex "./gitlab-runner.exe install $installparameters" | |
} | |
Set-StrictMode -Version Latest | |
$ErrorActionPreference = "Stop" | |
$PSDefaultParameterValues['*:ErrorAction'] = 'Stop' | |
set-alias gitlab-runner-register -value GitlabRunnerRegister | |
set-alias gitlab-service-register -value GitlabServiceRegister | |
set-alias gitlab-cert-install -value GitlabRunnerGetSelfSignedCert | |
export-modulemember -function 'GitlabServiceRegister' -alias 'gitlab-service-register' | |
export-modulemember -function 'GitlabRunnerRegister' -alias 'gitlab-runner-register' | |
export-modulemember -function 'GitlabRunnerGetSelfSignedCert' -alias 'gitlab-cert-install' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment