Skip to content

Commit cebc8f1

Browse files
committed
chore: Refactor Azure Terraform
1 parent 766725a commit cebc8f1

File tree

12 files changed

+84
-92
lines changed

12 files changed

+84
-92
lines changed

.github/workflows/deploy_azure_infra.yaml

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,53 @@ name: GHA Self-hosted Infra - Azure
33
on:
44
push:
55
paths:
6-
- terraform_modules/gh-self-hosted-runner-infra/azure/**
7-
8-
env:
9-
AZURE_FUNCTIONAPP_NAME: '' # set this to your function app name on Azure
10-
AZURE_FUNCTIONAPP_PACKAGE_PATH: './github-runner-receiver-function' # set this to the path to your function app project, defaults to the repository root
11-
PYTHON_VERSION: '3.11' # set this to the python version to use (e.g. '3.6', '3.7', '3.8')
6+
- create-azure-infra/**
7+
workflow_dispatch:
8+
inputs:
9+
confirm:
10+
description: "Type 'yes' to confirm Terraform destroy"
11+
required: true
1212

1313
jobs:
1414
build-and-deploy:
1515
runs-on: ubuntu-latest
1616
environment: dev
17+
18+
defaults:
19+
run:
20+
shell: bash
21+
working-directory: create-azure-infra
22+
1723
steps:
1824
- name: 'Checkout Azure Infra'
1925
uses: actions/checkout@v4
2026
with:
2127
sparse-checkout:
22-
terraform_modules/gh-self-hosted-runner-infra/azure
28+
create-azure-infra
29+
30+
- name: Authenticate with Azure
31+
uses: azure/login@v2
32+
with:
33+
creds: '{"clientId":"${{ secrets.ARM_CLIENT_ID }}", "clientSecret":"${{ secrets.ARM_CLIENT_SECRET }}", "subscriptionId":"${{ secrets.ARM_SUBSCRIPTION_ID }}", "tenantId":"${{ secrets.ARM_TENANT_ID }}"}'
34+
35+
- name: "Terraform init"
36+
run : |
37+
pwd
38+
terraform init
39+
40+
- name: Terraform Apply
41+
if: github.event_name == 'push'
42+
run: terraform apply -auto-approve tfplan
43+
44+
- name: Terraform destroy plan
45+
if: github.event.inputs.confirm == 'yes'
46+
run: terraform plan -destroy -out=destroyplan
47+
48+
- name: Terraform destroy infra
49+
if: github.event.inputs.confirm == 'yes'
50+
run: terraform apply -destroy -out=destroyplan
51+
52+
53+
54+
55+

create-azure-infra/dev.tfvars

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
project = "awesome-proj"
2+
env = "dev"
3+
location = "eastus2"
4+
acr_sku = "Basic"
5+
acr_admin_enabled = false
6+
kv_sku_name = "standard"

terraform_modules/gh-self-hosted-runner-infra/azure/main.tf renamed to create-azure-infra/main.tf

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
locals {
2-
current_date = formatdate("YYYYMMDD", timestamp())
1+
resource "random_string" "resource_code" {
2+
length = 5
3+
special = false
4+
upper = false
35
}
46

57
resource "azurerm_resource_group" "gha_runner_rg" {
6-
name = "${var.project}-rg"
8+
name = "${var.project}-${var.env}-rg"
79
location = var.location
810
}
911

1012
# Storage Account
1113
resource "azurerm_storage_account" "gha_runner_sa" {
12-
name = "${var.project}${var.env}${local.current_date}" # Unique name for the storage account
14+
name = "${var.project}${var.env}${random_string.resource_code}" # Unique name for the storage account
1315
resource_group_name = azurerm_resource_group.gha_runner_rg.name
1416
location = azurerm_resource_group.gha_runner_rg.location
1517
account_tier = var.storage_account_account_tier #"Standard"
1618
account_replication_type = var.storage_account_replication_type #"LRS"
1719
account_kind = "Storage"
1820
}
1921

20-
2122
# App Service Plan (Hosting plan for the function app)
2223
resource "azurerm_service_plan" "gha_runner_asp" {
2324
name = "${var.project}-${var.env}-asp"

create-azure-infra/provider.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
terraform {
2+
required_providers {
3+
azurerm = {
4+
source = "hashicorp/azurerm"
5+
version = "4.12.0"
6+
}
7+
}
8+
backend "azurerm" {
9+
resource_group_name = azurerm_resource_group.gha_runner_rg.name
10+
storage_account_name = azurerm_storage_account.gha_runner_sa.name
11+
container_name = azurerm_storage_container.tfstate_container.name
12+
key = "${var.project}-${var.env}-terraform.tfstate"
13+
}
14+
}
15+
16+
# Provider Configuration
17+
provider "azurerm" {
18+
features {
19+
resource_group {
20+
prevent_deletion_if_contains_resources = false
21+
}
22+
}
23+
}
24+
25+
resource "azurerm_storage_container" "tfstate_container" {
26+
name = ""${var.project}-${var.env}-tfstate-container"
27+
storage_account_id = azurerm_storage_account.gha_runner_sa.id
28+
container_access_type = "private"
29+
}
30+
31+
32+
data "azurerm_client_config" "current" {}

terraform_modules/gh-self-hosted-runner-infra/azure/README.md

Whitespace-only changes.

terraform_modules/gh-self-hosted-runner-infra/azure/examples/main.tf

Lines changed: 0 additions & 15 deletions
This file was deleted.

terraform_modules/gh-self-hosted-runner-infra/azure/examples/terraform.tfvars

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)