Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/terraform/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
| ----- | ----------- | -------- | ------------- | ------- |
| `modulesPath` | The path to the Terraform modules to build | *false* | `.tf` |
| `modulesPathDepth` | The path to the projects to test - can be a .csproj or a .sln file | *false* | `1` |
| `workspaceName` | The name of the Terraform workspace where to plan changes | *false* | `""` | Requires valid credentials to access the state file |
| `verbosity` | The verbosity of the scripts | *false* | `minimal` | Set to `minimal`, `normal` or `detailed` |

### Outputs
Expand Down
8 changes: 7 additions & 1 deletion build/terraform/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ inputs:
modulesPathDepth:
description: The depth of the path search, to find the Terraform modules to build
default: '1'
workspaceName:
description: The name of the Terraform workspace where to plan changes
required: false
default: ''
verbosity:
description: The verbosity of the scripts
default: minimal
Expand All @@ -23,6 +27,8 @@ runs:
- name: Build Terraform modules with script
shell: pwsh
run: ${{ github.action_path }}/build-terraform.ps1 `
-modulesPath "${{ inputs.modulesPath }}" `
-modulesPathDepth "${{ inputs.modulesPathDepth }}" `
-workspaceName "${{ inputs.workspaceName }}" `
-verbosity ${{ inputs.verbosity }}
working-directory: ${{ inputs.modulesPath }}

42 changes: 35 additions & 7 deletions build/terraform/build-terraform.ps1
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
<#
.SYNOPSIS
This script builds and tests Terraform modules
.PARAMETER modulesPath
The path to the Terraform modules to build
.PARAMETER modulesPathDepth
The depth of the path search, to find the Terraform modules to build
.PARAMETER workspaceName
The name of the Terraform workspace
.PARAMETER verbosity
The verbosity level
#>

[CmdletBinding()]
Param(
[parameter(Mandatory = $true)]
[string]$modulesPath,

[parameter(Mandatory = $true)]
[int]$modulesPathDepth,

[parameter(Mandatory = $false)]
[string]$workspaceName,

[parameter(Mandatory = $true)]
[ValidateSet('minimal', 'normal', 'detailed')]
[string]$verbosity
)

Write-Output "Modules path is: $modulesPath"
Write-Output "Modules path depth is: $modulesPathDepth"
Write-Output "Workspace name is: $workspaceName"
Write-Output "Verbosity is: $verbosity"

Write-Output '=========='

$sw = [Diagnostics.Stopwatch]::StartNew()
$pathFilter = 'main.tf'
$childItems = Get-ChildItem -Path $modulesPath -Recurse -Depth $modulesPathDepth -Filter $pathFilter -Force
$childItems = Get-ChildItem -Recurse -Depth $modulesPathDepth -Filter $pathFilter -Force
$childItemsCount = $childItems.Count
Write-Output "Items found: $childItemsCount"

Expand Down Expand Up @@ -62,6 +62,34 @@ $childItems | Foreach-Object -ThrottleLimit 5 -Parallel {
throw 1
}
}

if (-not ([string]::IsNullOrWhiteSpace($workspaceName))) {
Write-Output "Terraform initialisation..."
terraform init -input=false -upgrade -no-color 2>&1
if (!$?) {
Write-Output "::error title=Terraform failed::Terraform initialization failed"
throw 1
}

Write-Output "Terraform workspace selection..."
terraform workspace select -or-create $workspaceName -no-color 2>&1
if (!$?) {
Write-Output "::error title=Terraform failed::Terraform workspace selection failed"
throw 1
}

Write-Output "Terraform plan..."
$planResult = terraform plan -var-file="hosts/$workspaceName.tfvars" -input=false -no-color -lock=false 2>&1
if (!$?) {
Write-Output $planResult
Write-Output "::error title=Terraform failed::Terraform plan failed"
throw 1
}

Write-Output $planResult
} else {
Write-Output "No workspace defined: no plan performed."
}

Write-Output '=========='

Expand Down
3 changes: 1 addition & 2 deletions deploy/terraform/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ runs:
- name: Build Terraform modules with script
shell: pwsh
run: ${{ github.action_path }}/deploy-terraform.ps1 `
-modulePath ${{ inputs.modulePath }}
-workspaceName ${{ inputs.workspaceName }}
-workspaceName ${{ inputs.workspaceName }} `
-verbosity ${{ inputs.verbosity }}
working-directory: ${{ inputs.modulePath }}
6 changes: 0 additions & 6 deletions deploy/terraform/deploy-terraform.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<#
.SYNOPSIS
This script deploy a Terraform module
.PARAMETER modulePath
The path to the Terraform module to deploy
.PARAMETER workspaceName
The name of the Terraform workspace
.PARAMETER verbosity
Expand All @@ -11,9 +9,6 @@

[CmdletBinding()]
Param(
[parameter(Mandatory = $true)]
[string]$modulePath,

[parameter(Mandatory = $true)]
[string]$workspaceName,

Expand All @@ -22,7 +17,6 @@ Param(
[string]$verbosity
)

Write-Output "Modules path is: $modulesPath"
Write-Output "Workspace name is: $workspaceName"
Write-Output "Verbosity is: $verbosity"

Expand Down