Skip to content
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

[WIP]: Add support for Windows runners #88

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
feat: added start of windows init script module
  • Loading branch information
4lch4 committed Jun 17, 2023

Verified

This commit was signed with the committer’s verified signature. The key has expired.
lilnasy Arsh
commit c4e77232f271f6018a42c208683318ad9b9df908
15 changes: 15 additions & 0 deletions modules/app-config/main.tf
Original file line number Diff line number Diff line change
@@ -97,6 +97,17 @@ module "ubuntu_init" {
github_runner_group = var.github_runner_group
}

module "windows_init" {
source = "../windows-init"

github_organization = var.github_organization
github_runner_version = var.github_runner_version
github_runner_labels = local.github_runner_labels
azure_registration_key_vault_name = var.azure_registration_key_vault_name
github_runner_username = var.github_runner_username
github_runner_group = var.github_runner_group
}

resource "azurerm_app_configuration_key" "config_ubuntu_init_script" {
configuration_store_id = azurerm_app_configuration.github_runner_app_config.id
content_type = "text/plain"
@@ -113,3 +124,7 @@ resource "azurerm_app_configuration_key" "config_ubuntu_init_script" {
output "ubuntu_init_script" {
value = module.ubuntu_init.raw_script
}

output "windows_init_script" {
value = module.windows_init.raw_script
}
10 changes: 10 additions & 0 deletions modules/windows-init/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
locals {
ubuntu_init_script = templatefile("${path.module}/ubuntu-init.sh.tpl", {
runner_version = var.github_runner_version
runner_labels = join(",", var.github_runner_labels)
runner_owner = var.github_organization
runner_username = var.github_runner_username
registration_key_vault_name = var.azure_registration_key_vault_name
runner_group = var.github_runner_group
})
}
7 changes: 7 additions & 0 deletions modules/windows-init/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "base64_encoded_script" {
value = base64encode(local.ubuntu_init_script)
}

output "raw_script" {
value = local.ubuntu_init_script
}
23 changes: 23 additions & 0 deletions modules/windows-init/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
variable "github_organization" {
type = string
}

variable "github_runner_version" {
type = string
}

variable "github_runner_labels" {
type = list(string)
}

variable "azure_registration_key_vault_name" {
type = string
}

variable "github_runner_username" {
type = string
}

variable "github_runner_group" {
type = string
}
92 changes: 92 additions & 0 deletions modules/windows-init/windows-init.ps1.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
param (
[Parameter(Mandatory = $true, HelpMessage = 'e.g. 2.295.0')]
[ValidateScript({
if ($_ -match $RunnerVersionPattern)
{
$true
}
else
{
throw "$_ is an invalid format. It must be in the format of x.x.x."
}
})]
[string]$RunnerVersion,

[Parameter(Mandatory = $true, HelpMessage = 'e.g. "azure, vm"')]
[string[]]$RunnerLabels,

[Parameter(Mandatory = $true, HelpMessage = 'e.g. "liatrio-enterprise"')]
[string]$RunnerOwner,

[Parameter(Mandatory = $true, HelpMessage = 'e.g. "kv-gh-run-reg-liatriodev"')]
[string]$RegistrationKeyVaultName,

[Parameter(Mandatory = $false, HelpMessage = 'e.g. [sha256 sum for runner binary]')]
[string]$RunnerSha,

[Parameter(Mandatory = $false, HelpMessage = 'e.g. github.mydomain.com')]
[string]$GHUrl,

[Parameter(Mandatory = $false, HelpMessage = 'e.g. C:\user-data.log')]
[string]$TranscriptLogPath = 'C:\user-data.log'
)

# Redirect output to log file
Start-Transcript -Path $TranscriptLogPath -Append

# Write-Debug "Input parameters:"
# Write-Debug "RunnerVersion: $RunnerVersion"
# Write-Debug "RunnerLabels: $RunnerLabels"
# Write-Debug "RunnerOwner: $RunnerOwner"
# Write-Debug "RegistrationKeyVaultName: $RegistrationKeyVaultName"
# Write-Debug "RunnerSha: $RunnerSha"
# Write-Debug "GHUrl: $GHUrl"

# I commented out the $USER_ID line because it doesn't seem to be required for Windows.
# $USER_ID = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value

# Retain variable setup for later dependent step(s).
$USER_NAME = $runner_username

$RunnerDestination = 'C:\actions-runner'

# Create a folder for the runner.
New-Item -ItemType Directory -Path $RunnerDestination
Set-Location $RunnerDestination

# Write-Debug "Created actions-runner folder."

# Download the latest runner package to the previously created folder.
$RunnerFileName = "actions-runner-win-x64-$RunnerVersion.zip"
$RunnerPackageURL = "https://github.com/actions/runner/releases/download/v$RunnerVersion/$RunnerFileName"

# Write-Debug "Downloading runner package from $RunnerPackageURL to $RunnerFileName"

Invoke-WebRequest -Uri $RunnerPackageURL -OutFile $(Join-Path -)

# Extract the installer.
Expand-Archive -Path $RunnerFileName -DestinationPath '.'

# Config runner for rootless docker
Set-Location 'C:\actions-runner'
# Add-Content -Path ".env" -Value "DOCKER_HOST=unix:///run/user/${USER_ID}/docker.sock"
Add-Content -Path '.env' -Value 'DOCKER_HOST=npipe:////./pipe/docker_engine'
Add-Content -Path '.env' -Value "PATH=C:\Users\${USER_NAME}\bin;${PATH}"

# Retrieve gh registration token from azure key vault
az login --identity --allow-no-subscription
$REGISTRATION_TOKEN = (az keyvault secret show -n $(hostname) --vault-name ${registration_key_vault_name} | ConvertFrom-Json).value

Set-Location 'C:\'
Set-Location 'actions-runner'

# Configure and run as the specified user
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $USER_NAME, (ConvertTo-SecureString 'Password' -AsPlainText -Force)
Start-Process 'powershell.exe' -Credential $cred -ArgumentList @"
Set-Location C:\actions-runner
.\config.cmd --unattended --ephemeral --replace --runnergroup ${runner_group} --labels ${runner_labels} --url https://github.com/${runner_owner} --token $${REGISTRATION_TOKEN}
.\run.cmd
"@

# Stop transcript
Stop-Transcript
3 changes: 3 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
output "ubuntu_init_script" {
value = module.app_config.ubuntu_init_script
}

output "windows_init_script" {
value = module.app_config.windows_init_script
}

output "function_webhook_url" {