Skip to content

Commit 400e415

Browse files
Fix terraform.exe location bug by moving OS detection before first use and consolidating PATH setting
Co-authored-by: jaredfholgate <1612200+jaredfholgate@users.noreply.github.com>
1 parent 0a2aadc commit 400e415

1 file changed

Lines changed: 49 additions & 55 deletions

File tree

alz/azuredevops/pipelines/terraform/templates/helpers/terraform-installer.yaml

Lines changed: 49 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,6 @@ steps:
3737
}
3838
}
3939
40-
$unzipdir = Join-Path -Path $TOOLS_PATH -ChildPath "terraform_$TF_VERSION"
41-
if (Test-Path $unzipdir) {
42-
Write-Host "Terraform $TF_VERSION already installed."
43-
if($os -eq "windows") {
44-
$env:PATH = "$($unzipdir);$env:PATH"
45-
} else {
46-
$env:PATH = "$($unzipdir):$env:PATH"
47-
}
48-
Write-Host "##vso[task.setvariable variable=PATH]$env:PATH"
49-
return
50-
}
51-
5240
$os = ""
5341
if ($IsWindows) {
5442
$os = "windows"
@@ -60,58 +48,66 @@ steps:
6048
$os = "darwin"
6149
}
6250
63-
# Enum values can be seen here: https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.architecture?view=net-7.0#fields
64-
$architecture = ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture).ToString().ToLower()
51+
$unzipdir = Join-Path -Path $TOOLS_PATH -ChildPath "terraform_$TF_VERSION"
52+
if (Test-Path $unzipdir) {
53+
Write-Host "Terraform $TF_VERSION already installed."
54+
} else {
55+
# Enum values can be seen here: https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.architecture?view=net-7.0#fields
56+
$architecture = ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture).ToString().ToLower()
6557
66-
if($architecture -eq "x64") {
67-
$architecture = "amd64"
68-
}
69-
if($architecture -eq "x86") {
70-
$architecture = "386"
71-
}
58+
if($architecture -eq "x64") {
59+
$architecture = "amd64"
60+
}
61+
if($architecture -eq "x86") {
62+
$architecture = "386"
63+
}
7264
73-
$osAndArchitecture = "$($os)_$($architecture)"
74-
75-
$supportedOsAndArchitectures = @(
76-
"darwin_amd64",
77-
"darwin_arm64",
78-
"linux_386",
79-
"linux_amd64",
80-
"linux_arm64",
81-
"windows_386",
82-
"windows_amd64"
83-
)
84-
85-
if($supportedOsAndArchitectures -notcontains $osAndArchitecture) {
86-
Write-Error "Unsupported OS and architecture combination: $osAndArchitecture"
87-
exit 1
88-
}
65+
$osAndArchitecture = "$($os)_$($architecture)"
66+
67+
$supportedOsAndArchitectures = @(
68+
"darwin_amd64",
69+
"darwin_arm64",
70+
"linux_386",
71+
"linux_amd64",
72+
"linux_arm64",
73+
"windows_386",
74+
"windows_amd64"
75+
)
76+
77+
if($supportedOsAndArchitectures -notcontains $osAndArchitecture) {
78+
Write-Error "Unsupported OS and architecture combination: $osAndArchitecture"
79+
exit 1
80+
}
8981
90-
$zipfilePath = "$unzipdir.zip"
82+
$zipfilePath = "$unzipdir.zip"
9183
92-
$url = $release.builds | Where-Object { $_.arch -eq $architecture -and $_.os -eq $os } | Select-Object -First 1 -ExpandProperty url
84+
$url = $release.builds | Where-Object { $_.arch -eq $architecture -and $_.os -eq $os } | Select-Object -First 1 -ExpandProperty url
9385
94-
if(!(Test-Path $TOOLS_PATH)) {
95-
New-Item -ItemType Directory -Path $TOOLS_PATH| Out-String | Write-Verbose
96-
}
86+
if(!(Test-Path $TOOLS_PATH)) {
87+
New-Item -ItemType Directory -Path $TOOLS_PATH| Out-String | Write-Verbose
88+
}
9789
98-
Invoke-WebRequest -Uri $url -OutFile "$zipfilePath" | Out-String | Write-Verbose
90+
Invoke-WebRequest -Uri $url -OutFile "$zipfilePath" | Out-String | Write-Verbose
9991
100-
Expand-Archive -Path $zipfilePath -DestinationPath $unzipdir
92+
Expand-Archive -Path $zipfilePath -DestinationPath $unzipdir
10193
102-
$toolFileName = "terraform"
94+
$toolFileName = "terraform"
10395
104-
if($os -eq "windows") {
105-
$toolFileName = "$($toolFileName).exe"
106-
}
96+
if($os -eq "windows") {
97+
$toolFileName = "$($toolFileName).exe"
98+
}
10799
108-
$toolFilePath = Join-Path -Path $unzipdir -ChildPath $toolFileName
100+
$toolFilePath = Join-Path -Path $unzipdir -ChildPath $toolFileName
109101
110-
if($os -ne "windows") {
111-
$isExecutable = $(test -x $toolFilePath; 0 -eq $LASTEXITCODE)
112-
if(!($isExecutable)) {
113-
chmod +x $toolFilePath
114-
}
102+
if($os -ne "windows") {
103+
$isExecutable = $(test -x $toolFilePath; 0 -eq $LASTEXITCODE)
104+
if(!($isExecutable)) {
105+
chmod +x $toolFilePath
106+
}
107+
}
108+
109+
Remove-Item $zipfilePath
110+
Write-Host "Installed Terraform version $TF_VERSION"
115111
}
116112
117113
if($os -eq "windows") {
@@ -120,8 +116,6 @@ steps:
120116
$env:PATH = "$($unzipdir):$env:PATH"
121117
}
122118
Write-Host "##vso[task.setvariable variable=PATH]$env:PATH"
123-
Remove-Item $zipfilePath
124-
Write-Host "Installed Terraform version $TF_VERSION"
125119
126120
displayName: Install Terraform
127121
env:

0 commit comments

Comments
 (0)