Skip to content

Commit 96582d1

Browse files
committed
Update
1 parent 900d094 commit 96582d1

File tree

16 files changed

+427
-15
lines changed

16 files changed

+427
-15
lines changed

PowerShellModules/Terraform.psm1

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,14 @@ function Get-TerraformStackFolders
4545
[string[]]$StacksToRun
4646
)
4747

48-
#─────────────────────────────────── pre-checks ────────────────────────────
4948
if (-not (Test-Path $CodeRoot))
5049
{
5150
_LogMessage -Level 'ERROR' -Message "Code root not found: $CodeRoot" `
5251
-InvocationName $MyInvocation.MyCommand.Name
5352
throw "Code root not found: $CodeRoot"
5453
}
5554

56-
# Match folders like 0_rg or 0-rg or allstackskip_99-azdo-pipelines-setup
57-
$allDirs = Get-ChildItem -Path $CodeRoot -Directory |
58-
Where-Object { $_.Name -match '^\d+[-_].+' -or $_.Name -match '^allstackskip[-_].+' }
55+
$allDirs = Get-ChildItem -Path $CodeRoot -Directory
5956

6057
if (-not $allDirs)
6158
{
@@ -64,7 +61,6 @@ function Get-TerraformStackFolders
6461
throw "No stack folders found underneath $CodeRoot"
6562
}
6663

67-
#──────────────────────────── discover / index stacks ─────────────────────
6864
$stackLookup = @{ }
6965
foreach ($dir in $allDirs)
7066
{
@@ -73,25 +69,33 @@ function Get-TerraformStackFolders
7369
$stackLookup[$matches.name.ToLower()] = @{
7470
Path = $dir.FullName
7571
Order = [int]$matches.order
72+
IsNumbered = $true
7673
}
7774
}
7875
elseif ($dir.Name -match '^allstackskip[-_](?<rest>.+)$')
7976
{
80-
# Remove leading number/underscore from rest if present
8177
$stackName = $matches.rest -replace '^\d+[-_]', ''
8278
$stackLookup[$stackName.ToLower()] = @{
8379
Path = $dir.FullName
8480
Order = 9999
8581
IsStackSkip = $true
82+
IsNumbered = $false
83+
}
84+
}
85+
else
86+
{
87+
$stackLookup[$dir.Name.ToLower()] = @{
88+
Path = $dir.FullName
89+
Order = 9999
90+
IsNumbered = $false
8691
}
8792
}
8893
}
8994

90-
#──────────────────────────── argument sanitisation ────────────────────────
9195
$requested = @(
9296
$StacksToRun |
9397
ForEach-Object { $_.Trim() } |
94-
Where-Object { $_ } # drop empty entries
98+
Where-Object { $_ }
9599
)
96100

97101
if ($requested -contains 'all' -and $requested.Count -gt 1)
@@ -103,7 +107,6 @@ function Get-TerraformStackFolders
103107
$requested = $requested | Where-Object { $_.ToLower() -ne 'all' }
104108
}
105109

106-
#──────────────────────────── resolve stack list ───────────────────────────
107110
$result = [System.Collections.Generic.List[string]]::new()
108111

109112
if (($requested.Count -eq 1) -and ($requested[0].ToLower() -eq 'all'))
@@ -112,11 +115,8 @@ function Get-TerraformStackFolders
112115
-InvocationName $MyInvocation.MyCommand.Name
113116

114117
$stackLookup.GetEnumerator() |
118+
Where-Object { $_.Value.IsNumbered -eq $true -and (-not ($_.Value.PSObject.Properties['IsStackSkip'] -and $_.Value.IsStackSkip)) } |
115119
Sort-Object { $_.Value.Order } |
116-
Where-Object {
117-
$folderName = Split-Path -Path $_.Value.Path -Leaf
118-
$folderName -notmatch '^allstackskip[-_]'
119-
} |
120120
ForEach-Object { [void]$result.Add($_.Value.Path) }
121121
}
122122
else
@@ -134,7 +134,6 @@ function Get-TerraformStackFolders
134134
}
135135
}
136136

137-
#────────────────────────────────── debug log ──────────────────────────────
138137
_LogMessage -Level 'DEBUG' `
139138
-Message "Stack execution order → $( $result -join ', ' )" `
140139
-InvocationName $MyInvocation.MyCommand.Name
@@ -143,6 +142,7 @@ function Get-TerraformStackFolders
143142
}
144143

145144

145+
146146
###############################################################################
147147
# Run `terraform init`
148148
###############################################################################

Run-AzTerraform.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ param (
2525
[string]$TerraformPlanFileName = "tfplan.plan",
2626
[string]$TerraformDestroyPlanFileName = "tfplan-destroy.plan",
2727
[string]$TerraformCodeLocation = "terraform",
28-
[string]$TerraformStackToRunJson = '["rg"]', # JSON format Use 'all' to run 0_, 1_, etc and destroy in reverse order 1_, 0_ etc
28+
[string]$TerraformStackToRunJson = '["all"]', # JSON format Use 'all' to run 0_, 1_, etc and destroy in reverse order 1_, 0_ etc
2929
[string]$CreateTerraformWorkspace = "true",
3030
[string]$TerraformWorkspace = "dev",
3131
[string]$InstallAzureCli = "false",

terraform/module-dev/.terraform.lock.hcl

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "azuredevops" {
2+
use_oidc = true
3+
}

terraform/module-dev/build-jobs.tf

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
resource "azuredevops_build_definition" "job_terraform_build" {
2+
for_each = local.envs
3+
project_id = data.azuredevops_project.target.id
4+
name = "${title(local.repo_name)} - ${title(each.key)} - Terraform Build"
5+
path = "${local.folders_path[each.key]}\\jobs"
6+
agent_pool_name = "Default"
7+
8+
ci_trigger {
9+
use_yaml = false
10+
}
11+
12+
repository {
13+
repo_type = "GitHub"
14+
repo_id = "${var.github_org_name}/${var.github_project_name}"
15+
branch_name = local.default_branch
16+
yml_path = ".azuredevops/workflows/jobs/${each.key}/terraform-build.yaml"
17+
service_connection_id = data.azuredevops_serviceendpoint_github.github.id
18+
}
19+
}
20+
21+
resource "azuredevops_build_definition" "job_terraform_destroy" {
22+
for_each = local.envs
23+
project_id = data.azuredevops_project.target.id
24+
name = "${title(local.repo_name)} - ${title(each.key)} - Terraform Destroy"
25+
path = "${local.folders_path[each.key]}\\jobs"
26+
agent_pool_name = "Default"
27+
28+
ci_trigger {
29+
use_yaml = false
30+
}
31+
32+
repository {
33+
repo_type = "GitHub"
34+
repo_id = "${var.github_org_name}/${var.github_project_name}"
35+
branch_name = local.default_branch
36+
yml_path = ".azuredevops/workflows/jobs/${each.key}/terraform-destroy.yaml"
37+
service_connection_id = data.azuredevops_serviceendpoint_github.github.id
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
resource "azuredevops_build_definition" "stage_terraform_build" {
2+
for_each = local.envs
3+
project_id = data.azuredevops_project.target.id
4+
name = "${title(local.repo_name)} - ${title(each.key)} - Terraform Build"
5+
path = "${local.folders_path[each.key]}\\stages"
6+
agent_pool_name = "Default"
7+
8+
ci_trigger {
9+
use_yaml = false
10+
}
11+
12+
repository {
13+
repo_type = "GitHub"
14+
repo_id = "${var.github_org_name}/${var.github_project_name}"
15+
branch_name = local.default_branch
16+
yml_path = ".azuredevops/workflows/stages/${each.key}/terraform-build.yaml"
17+
service_connection_id = data.azuredevops_serviceendpoint_github.github.id
18+
}
19+
}
20+
21+
resource "azuredevops_build_definition" "stage_terraform_destroy" {
22+
for_each = local.envs
23+
project_id = data.azuredevops_project.target.id
24+
name = "${title(local.repo_name)} - ${title(each.key)} - Terraform Destroy"
25+
path = "${local.folders_path[each.key]}\\stages"
26+
agent_pool_name = "Default"
27+
28+
ci_trigger {
29+
use_yaml = false
30+
}
31+
32+
repository {
33+
repo_type = "GitHub"
34+
repo_id = "${var.github_org_name}/${var.github_project_name}"
35+
branch_name = local.default_branch
36+
yml_path = ".azuredevops/workflows/stages/${each.key}/terraform-destroy.yaml"
37+
service_connection_id = data.azuredevops_serviceendpoint_github.github.id
38+
}
39+
}

terraform/module-dev/data.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
data "azuredevops_project" "target" {
2+
name = "libredevops"
3+
}
4+
5+
data "azuredevops_serviceendpoint_github" "github" {
6+
project_id = data.azuredevops_project.target.id
7+
service_endpoint_name = data.azuredevops_project.target.name
8+
}

terraform/module-dev/jobs.tf

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
resource "azuredevops_build_definition" "jobs_init_plan" {
2+
for_each = local.envs
3+
project_id = data.azuredevops_project.target.id
4+
name = "${title(local.repo_name)} - Jobs - ${title(each.key)} - Terraform Init & Terraform Plan"
5+
path = "${local.folders_path[each.key]}\\jobs"
6+
agent_pool_name = "Default"
7+
8+
ci_trigger {
9+
use_yaml = false
10+
}
11+
12+
repository {
13+
repo_type = "GitHub"
14+
repo_id = "${var.github_org_name}/${var.github_project_name}"
15+
branch_name = local.default_branch
16+
yml_path = ".azuredevops/workflows/jobs/${each.key}/terraform-init-plan.yaml"
17+
service_connection_id = data.azuredevops_serviceendpoint_github.github.id
18+
}
19+
}
20+
21+
resource "azuredevops_build_definition" "jobs_init_plan_apply" {
22+
for_each = local.envs
23+
project_id = data.azuredevops_project.target.id
24+
name = "${title(local.repo_name)} - Jobs - ${title(each.key)} - Terraform Init, Terraform Plan & Terraform Apply"
25+
path = "${local.folders_path[each.key]}\\jobs"
26+
agent_pool_name = "Default"
27+
28+
ci_trigger {
29+
use_yaml = false
30+
}
31+
32+
repository {
33+
repo_type = "GitHub"
34+
repo_id = "${var.github_org_name}/${var.github_project_name}"
35+
branch_name = local.default_branch
36+
yml_path = ".azuredevops/workflows/jobs/${each.key}/terraform-init-plan-apply.yaml"
37+
service_connection_id = data.azuredevops_serviceendpoint_github.github.id
38+
}
39+
}
40+
41+
resource "azuredevops_build_definition" "jobs_init_plan_destroy" {
42+
for_each = local.envs
43+
project_id = data.azuredevops_project.target.id
44+
name = "${title(local.repo_name)} - Jobs - ${title(each.key)} - Terraform Init & Terraform Plan Destroy"
45+
path = "${local.folders_path[each.key]}\\jobs"
46+
agent_pool_name = "Default"
47+
48+
ci_trigger {
49+
use_yaml = false
50+
}
51+
52+
repository {
53+
repo_type = "GitHub"
54+
repo_id = "${var.github_org_name}/${var.github_project_name}"
55+
branch_name = local.default_branch
56+
yml_path = ".azuredevops/workflows/jobs/${each.key}/terraform-init-plan-destroy.yaml"
57+
service_connection_id = data.azuredevops_serviceendpoint_github.github.id
58+
}
59+
}
60+
61+
resource "azuredevops_build_definition" "jobs_init_plan_destroy_apply" {
62+
for_each = local.envs
63+
project_id = data.azuredevops_project.target.id
64+
name = "${title(local.repo_name)} - Jobs - ${title(each.key)} - Terraform Init, Terraform Plan Destroy & Terraform Destroy"
65+
path = "${local.folders_path[each.key]}\\jobs"
66+
agent_pool_name = "Default"
67+
68+
ci_trigger {
69+
use_yaml = false
70+
}
71+
72+
repository {
73+
repo_type = "GitHub"
74+
repo_id = "${var.github_org_name}/${var.github_project_name}"
75+
branch_name = local.default_branch
76+
yml_path = ".azuredevops/workflows/jobs/${each.key}/terraform-init-plan-destroy-apply.yaml"
77+
service_connection_id = data.azuredevops_serviceendpoint_github.github.id
78+
}
79+
}

terraform/module-dev/main.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
locals {
2+
envs = {
3+
dev = "Development"
4+
}
5+
repo_name = split("/", var.repository_url)[4]
6+
project = local.repo_name
7+
default_branch = "refs/heads/main"
8+
folders_path = { for env, name in local.envs : env => "\\PipelineTemplates\\${local.project}\\${env}" }
9+
}

terraform/module-dev/outputs.tf

Whitespace-only changes.

0 commit comments

Comments
 (0)