-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerraform-Release.ps1
More file actions
124 lines (100 loc) · 4.15 KB
/
Copy pathTerraform-Release.ps1
File metadata and controls
124 lines (100 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<#
.SYNOPSIS
Formats Terraform, sorts variables and outputs, regenerates the README, and optionally tags a release.
.DESCRIPTION
Uses the LibreDevOpsHelpers Terraform and TerraformDocs helpers to format code, alphabetise
variable and output blocks, and regenerate the README with terraform-docs. Optionally commits
and pushes a git tag.
.PARAMETER VariablesInFile
Variables file to read. Defaults to ./variables.tf.
.PARAMETER VariablesOutFile
Variables file to write. Defaults to ./variables.tf.
.PARAMETER OutputsInFile
Outputs file to read. Defaults to ./outputs.tf.
.PARAMETER OutputsOutFile
Outputs file to write. Defaults to ./outputs.tf.
.PARAMETER GitTag
Git tag to create when -GitRelease is set. Defaults to 1.0.0.
.PARAMETER GitCommitMessage
Commit message to use when -GitRelease is set.
.PARAMETER SortInputs
Sort variable blocks. Defaults to true.
.PARAMETER SortOutputs
Sort output blocks. Defaults to true.
.PARAMETER GitRelease
Commit, push, and tag the repository. Defaults to false.
.PARAMETER FormatTerraform
Run terraform fmt. Defaults to true.
.PARAMETER GenerateNewReadme
Regenerate the README with terraform-docs. Defaults to true.
#>
param(
[string]$VariablesInFile = './variables.tf',
[string]$VariablesOutFile = './variables.tf',
[string]$OutputsInFile = './outputs.tf',
[string]$OutputsOutFile = './outputs.tf',
[string]$GitTag = '1.0.0',
[string]$GitCommitMessage = 'Update code',
[bool]$SortInputs = $true,
[bool]$SortOutputs = $true,
[bool]$GitRelease = $false,
[bool]$FormatTerraform = $true,
[bool]$GenerateNewReadme = $true
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$manifest = Join-Path $scriptDir 'LibreDevOpsHelpers' 'LibreDevOpsHelpers.psd1'
if (-not (Test-Path -LiteralPath $manifest)) {
throw "Module manifest not found: $manifest"
}
Import-Module $manifest -Force -ErrorAction Stop
function Invoke-GitRelease {
param(
[Parameter(Mandatory)][string]$Tag,
[Parameter(Mandatory)][string]$CommitMessage
)
$gitPath = Get-Command git -ErrorAction Stop
Write-LdoLog -Level INFO -Message "git found at: $($gitPath.Source)"
git add --all
if ($LASTEXITCODE -ne 0) { throw "git add failed (exit $LASTEXITCODE)." }
git commit -m $CommitMessage
if ($LASTEXITCODE -ne 0) { throw "git commit failed (exit $LASTEXITCODE)." }
git push
if ($LASTEXITCODE -ne 0) { throw "git push failed (exit $LASTEXITCODE)." }
git tag $Tag --force
if ($LASTEXITCODE -ne 0) { throw "git tag failed (exit $LASTEXITCODE)." }
git push --tags --force
if ($LASTEXITCODE -ne 0) { throw "git push --tags failed (exit $LASTEXITCODE)." }
Write-LdoLog -Level SUCCESS -Message "Released tag $Tag."
}
if ($FormatTerraform) {
Format-LdoTerraform -CodePath (Get-Location).Path
}
if ($SortInputs) {
$variablesContent = Get-LdoTerraformFileContent -Filename $VariablesInFile
if (-not [string]::IsNullOrWhiteSpace($variablesContent)) {
$sorted = Format-LdoTerraformVariables -VariablesContent $variablesContent
if (-not [string]::IsNullOrWhiteSpace($sorted)) {
Set-LdoTerraformFileContent -Filename $VariablesOutFile -Content $sorted
Write-LdoLog -Level SUCCESS -Message "Sorted Terraform variables written to $VariablesOutFile"
}
}
}
if ($SortOutputs) {
$outputsContent = Get-LdoTerraformFileContent -Filename $OutputsInFile
if (-not [string]::IsNullOrWhiteSpace($outputsContent)) {
$sorted = Format-LdoTerraformOutputs -OutputsContent $outputsContent
if (-not [string]::IsNullOrWhiteSpace($sorted)) {
Set-LdoTerraformFileContent -Filename $OutputsOutFile -Content $sorted
Write-LdoLog -Level SUCCESS -Message "Sorted Terraform outputs written to $OutputsOutFile"
}
}
}
if ($GenerateNewReadme) {
Update-LdoReadmeWithTerraformDocs -CodePath (Get-Location).Path
}
if ($GitRelease) {
Invoke-GitRelease -Tag $GitTag -CommitMessage $GitCommitMessage
}
Write-LdoLog -Level SUCCESS -Message 'Terraform release script completed.'