Skip to content

Commit 15124a4

Browse files
enhancement: env file version automation (#89)
1 parent 1d8a0db commit 15124a4

File tree

6 files changed

+55
-16
lines changed

6 files changed

+55
-16
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ This currently tests for:
8181
#### Azure Landing Zone Environment with Bicep and GitHub Actions Workflows
8282

8383
```powershell
84-
New-ALZEnvironment -o <output_directory> -IaC "bicep" -cicd "github
84+
New-ALZEnvironment -o <output_directory> -i "bicep" -c "github"
8585
```
8686

8787
#### Azure Landing Zone Environment with Bicep and Azure DevOps Pipelines
8888

8989
```powershell
90-
New-ALZEnvironment -o <output_directory> -IaC "bicep" -cicd "azuredevops"
90+
New-ALZEnvironment -o <output_directory> -i "bicep" -c "azuredevops"
9191
```
9292

9393
> **Note**
@@ -96,13 +96,13 @@ New-ALZEnvironment -o <output_directory> -IaC "bicep" -cicd "azuredevops"
9696
#### Azure Landing Zone Environment with Terraform and GitHub Pipelines
9797

9898
```powershell
99-
New-ALZEnvironment -o <output_directory> -IaC "terraform" -cicd "github"
99+
New-ALZEnvironment -o <output_directory> -i "terraform" -c "github"
100100
```
101101

102102
#### Azure Landing Zone Environment with Terraform and Azure DevOps Pipelines
103103

104104
```powershell
105-
New-ALZEnvironment -o <output_directory> -IaC "terraform" -cicd "azuredevops"
105+
New-ALZEnvironment -o <output_directory> -i "terraform" -c "azuredevops"
106106
```
107107

108108
## Additional Cmdlets
@@ -112,7 +112,7 @@ New-ALZEnvironment -o <output_directory> -IaC "terraform" -cicd "azuredevops"
112112
#### Downloads and pulls down the specified release version from the remote GitHub repository to a local directory
113113

114114
```powershell
115-
Get-ALZGithubRelease -githubRepoUrl "https://github.com/Azure/ALZ-Bicep" -releases "v0.14.0" -directoryForReleases "C:\Repos\ALZ\accelerator\upstream-releases\"
115+
Get-ALZGithubRelease -i "bicep" -v "v0.14.0" -o "C:\Repos\ALZ\accelerator"
116116
```
117117

118118
## Development

src/ALZ/Private/New-ALZEnvironmentBicep.ps1

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ function New-ALZEnvironmentBicep {
1616
[string] $alzCicdPlatform
1717
)
1818

19-
$bicepModuleUrl = "https://github.com/Azure/ALZ-Bicep"
20-
2119
if ($PSCmdlet.ShouldProcess("ALZ-Bicep module configuration", "modify")) {
2220

2321
if($alzVersion -ne "latest" -and $alzVersion -notlike "*-preview") {
@@ -34,7 +32,7 @@ function New-ALZEnvironmentBicep {
3432
$alzEnvironmentDestinationInternalCode = Join-Path $alzEnvironmentDestination "upstream-releases"
3533

3634
# Downloading the latest or specified version of the bicep accelerator module
37-
$releaseTag = Get-ALZGithubRelease -directoryForReleases $alzEnvironmentDestinationInternalCode -githubRepoUrl $bicepModuleUrl -release $alzVersion
35+
$releaseTag = Get-ALZGithubRelease -directoryForReleases $alzEnvironmentDestination -iac "bicep" -release $alzVersion
3836
$releasePath = Join-Path -Path $alzEnvironmentDestinationInternalCode -ChildPath $releaseTag
3937

4038
# Getting the configuration

src/ALZ/Private/New-ALZEnvironmentTerraform.ps1

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ function New-ALZEnvironmentTerraform {
2323
[switch] $autoApprove
2424
)
2525

26-
$terraformModuleUrl = "https://github.com/Azure/alz-terraform-accelerator"
27-
2826
if ($PSCmdlet.ShouldProcess("ALZ-Terraform module configuration", "modify")) {
2927

3028
Write-InformationColored "Downloading alz-terraform-accelerator Terraform module to $alzEnvironmentDestination" -ForegroundColor Green -InformationAction Continue
@@ -36,7 +34,7 @@ function New-ALZEnvironmentTerraform {
3634
}
3735

3836
# Downloading the latest or specified version of the alz-terraform-accelerator module
39-
$releaseTag = Get-ALZGithubRelease -directoryForReleases $alzEnvironmentDestination -githubRepoUrl $terraformModuleUrl -release $alzVersion
37+
$releaseTag = Get-ALZGithubRelease -directoryForReleases $alzEnvironmentDestination -iac "terraform" -release $alzVersion
4038
$releasePath = Join-Path -Path $alzEnvironmentDestination -ChildPath $releaseTag
4139

4240
# Getting the configuration for the initial bootstrap user input and validators

src/ALZ/Public/Get-ALZGithubRelease.ps1

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,28 @@ Checks for the releases of a GitHub repository and downloads the latest release
2222
function Get-ALZGithubRelease {
2323
[CmdletBinding()]
2424
param (
25-
[Parameter(Mandatory = $true, Position = 1, HelpMessage = "Please the provide the full URL of the GitHub repository you wish to check for the latest release.")]
25+
[Parameter(Mandatory = $true, Position = 0, HelpMessage = "The IaC provider to use for the ALZ environment.")]
26+
[ValidateSet("bicep", "terraform")]
27+
[Alias("Iac")]
28+
[Alias("i")]
2629
[string]
27-
$githubRepoUrl,
30+
$alzIacProvider,
31+
32+
[Parameter(Mandatory = $false, Position = 1, HelpMessage = "Please the provide the full URL of the GitHub repository you wish to check for the latest release.")]
33+
[string]
34+
$githubRepoUrl = "",
2835

2936
[Parameter(Mandatory = $false, Position = 2, HelpMessage = "The releases to download. Specify 'all' to download all releases or 'latest' to download the latest release. Defaults to the latest release.")]
3037
[array]
38+
[Alias("version")]
39+
[Alias("v")]
3140
$release = "latest",
3241

3342
[Parameter(Mandatory = $false, Position = 3, HelpMessage = "The directory to download the releases to. Defaults to the current directory.")]
3443
[string]
44+
[Alias("Output")]
45+
[Alias("OutputDirectory")]
46+
[Alias("O")]
3547
$directoryForReleases = "$PWD/releases",
3648

3749
[Parameter(Mandatory = $false, Position = 4, HelpMessage = "An array of strings contianing the paths to the directories or files that you wish to keep when downloading and extracting from the releases.")]
@@ -43,6 +55,23 @@ function Get-ALZGithubRelease {
4355
$queryOnly
4456
)
4557

58+
# Set the repository URL if not provided
59+
$bicepModuleUrl = "https://github.com/Azure/ALZ-Bicep"
60+
$terraformModuleUrl = "https://github.com/Azure/alz-terraform-accelerator"
61+
if($githubRepoUrl -eq "") {
62+
if($alzIacProvider -eq "bicep") {
63+
$githubRepoUrl = $bicepModuleUrl
64+
} elseif($alzIacProvider -eq "terraform") {
65+
$githubRepoUrl = $terraformModuleUrl
66+
}
67+
}
68+
69+
$parentDirectory = $directoryForReleases
70+
# Bicep specific path setup
71+
if($alzIacProvider -eq "bicep") {
72+
$directoryForReleases = Join-Path $directoryForReleases "upstream-releases"
73+
}
74+
4675
# Split Repo URL into parts
4776
$repoOrgPlusRepo = $githubRepoUrl.Split("/")[-2..-1] -join "/"
4877

@@ -81,7 +110,7 @@ function Get-ALZGithubRelease {
81110
New-Item -ItemType Directory -Path $directoryForReleases | Out-String | Write-Verbose
82111
}
83112

84-
# Check the firectory for this release
113+
# Check the directory for this release
85114
$releaseDirectory = "$directoryForReleases/$releaseTag"
86115

87116
Write-Verbose "===> Checking if directory for release version exists: $releaseDirectory"
@@ -117,8 +146,18 @@ function Get-ALZGithubRelease {
117146
Remove-Item -Path "$releaseDirectory/tmp" -Force -Recurse
118147

119148
} else {
149+
Write-InformationColored "The release directory for this version already exists and has content in it, so we are not over-writing it." -ForegroundColor Yellow -InformationAction Continue
120150
Write-Verbose "===> Content already exists in $releaseDirectory. Skipping"
121151
}
122152

153+
# Check and replace the .env file release version if it is Bicep
154+
if($alzIacProvider -eq "bicep") {
155+
$envFilePath = Join-Path -Path $parentDirectory -ChildPath ".env"
156+
if(Test-Path $envFilePath) {
157+
Write-Verbose "===> Replacing the .env file release version with $releaseTag"
158+
(Get-Content $envFilePath) -replace "UPSTREAM_RELEASE_VERSION=.*", "UPSTREAM_RELEASE_VERSION=$releaseTag" | Set-Content $envFilePath
159+
}
160+
}
161+
123162
return $releaseTag
124163
}

src/ALZ/Public/New-ALZEnvironment.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,20 @@ function New-ALZEnvironment {
3535

3636
[Parameter(Mandatory = $false)]
3737
[Alias("alzBicepVersion")]
38+
[Alias("version")]
39+
[Alias("v")]
3840
[string] $alzVersion = "latest",
3941

4042
[Parameter(Mandatory = $false)]
4143
[ValidateSet("bicep", "terraform")]
4244
[Alias("Iac")]
45+
[Alias("i")]
4346
[string] $alzIacProvider = "bicep",
4447

4548
[Parameter(Mandatory = $false)]
4649
[ValidateSet("github", "azuredevops")]
4750
[Alias("Cicd")]
51+
[Alias("c")]
4852
[string] $alzCicdPlatform = "github",
4953

5054
[Parameter(Mandatory = $false)]

src/Tests/Unit/Public/Get-ALZGithubRelease.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ InModuleScope 'ALZ' {
100100
}
101101

102102
It 'Should get the correct releases' {
103-
Get-ALZGithubRelease -githubRepoUrl "http://github.com/test/repo" -directoryAndFilesToKeep @('repo-1.0.0') -directoryForReleases "output"
103+
Get-ALZGithubRelease -iac "bicep" -githubRepoUrl "http://github.com/test/repo" -directoryAndFilesToKeep @('repo-1.0.0') -directoryForReleases "output"
104104
Should -Invoke Expand-Archive
105105
Should -Not -Invoke Write-Warning
106106
}
107107

108108
It 'Should throw an exception when you ask for a release that does not exist' {
109-
{ Get-ALZGithubRelease -githubRepoUrl "http://github.com/test/repo" -release 'v2.0.0' -directoryAndFilesToKeep @('repo-1.0.0') -directoryForReleases "output" } | Should -Throw
109+
{ Get-ALZGithubRelease -iac "bicep" -githubRepoUrl "http://github.com/test/repo" -release 'v2.0.0' -directoryAndFilesToKeep @('repo-1.0.0') -directoryForReleases "output" } | Should -Throw
110110
Should -Invoke Write-Error
111111
}
112112
}

0 commit comments

Comments
 (0)