Setting Up Virtual Machine and Self-Hosted GitHub Runner on a Virtual Machine
In our organization, we use GitHub Actions for our continuous integration and deployment workflows. To enhance our CI/CD capabilities, security and maintain flexibility, we've decided to set up a self-hosted GitHub runner on a virtual machine. This document outlines the steps involved in creating the virtual machine and automating the runner setup.
Step 1 :Virtual Machine Creation:
So In order to provision vm first we have defined the variable in respective tfvars of our environment. vm_sku_size = "Standard_DS1_v2" vm_disk_size = 50
we have terraform file virtual_machine where the steps required to provision virtual machine which contains dynamic naming for each environment and the username and password are saved in respective key vault, to login into vm from ssh find the secert in key vault.
snippet of vm provision: ```hcl
resource "azurerm_linux_virtual_machine" "runnersrv" { name = "runner-${var.tags.environment}-${var.common_tags.region}-001" location = azurerm_resource_group.main.location resource_group_name = azurerm_resource_group.main.name network_interface_ids = [azurerm_network_interface.runnernic.id] size = var.vm_sku_size computer_name = local.runner_computername admin_username = local.runner_username admin_password = random_password.password.result disable_password_authentication = false
source_image_reference { publisher = "Canonical" offer = "0001-com-ubuntu-server-focal" sku = var.vm_sku_os version = "latest" } os_disk { name = "runner-disk-${var.tags.environment}-${var.common_tags.region}-001" caching = "ReadWrite" storage_account_type = "StandardSSD_LRS" disk_size_gb = var.vm_disk_size } tags = { environment = var.tags.environment } } ```
Step 2: Setting up VM for Self-Hosted Github Runner
Now we have custom script directory where we have defined the runner setup scrip runner.tftpl, this script is standared procedure script that will registered github runner on our organization repository.
this script is passed as linux vm extension on our virtual machine which in turn configure our runner automation.
resource "azurerm_virtual_machine_extension" "userdata" {
name = "github-runner-setup"
virtual_machine_id = azurerm_linux_virtual_machine.runnersrv.id
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type_handler_version = "2.0"
protected_settings = <<PROT
{
"script" : "${base64encode(templatefile("${path.module}/scripts/runner.tftpl", local.data_inputs))}"
}
PROT
tags = {
environment = var.tags.environment
}
}