From 15124a43f969651b7c69e48dfe2dcbe0ffd983e3 Mon Sep 17 00:00:00 2001 From: Jared Holgate Date: Thu, 23 Nov 2023 11:09:31 +0000 Subject: [PATCH] enhancement: env file version automation (#89) --- README.md | 10 ++--- src/ALZ/Private/New-ALZEnvironmentBicep.ps1 | 4 +- .../Private/New-ALZEnvironmentTerraform.ps1 | 4 +- src/ALZ/Public/Get-ALZGithubRelease.ps1 | 45 +++++++++++++++++-- src/ALZ/Public/New-ALZEnvironment.ps1 | 4 ++ .../Public/Get-ALZGithubRelease.Tests.ps1 | 4 +- 6 files changed, 55 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index b6629c4a..d24f9f07 100644 --- a/README.md +++ b/README.md @@ -81,13 +81,13 @@ This currently tests for: #### Azure Landing Zone Environment with Bicep and GitHub Actions Workflows ```powershell -New-ALZEnvironment -o -IaC "bicep" -cicd "github +New-ALZEnvironment -o -i "bicep" -c "github" ``` #### Azure Landing Zone Environment with Bicep and Azure DevOps Pipelines ```powershell -New-ALZEnvironment -o -IaC "bicep" -cicd "azuredevops" +New-ALZEnvironment -o -i "bicep" -c "azuredevops" ``` > **Note** @@ -96,13 +96,13 @@ New-ALZEnvironment -o -IaC "bicep" -cicd "azuredevops" #### Azure Landing Zone Environment with Terraform and GitHub Pipelines ```powershell -New-ALZEnvironment -o -IaC "terraform" -cicd "github" +New-ALZEnvironment -o -i "terraform" -c "github" ``` #### Azure Landing Zone Environment with Terraform and Azure DevOps Pipelines ```powershell -New-ALZEnvironment -o -IaC "terraform" -cicd "azuredevops" +New-ALZEnvironment -o -i "terraform" -c "azuredevops" ``` ## Additional Cmdlets @@ -112,7 +112,7 @@ New-ALZEnvironment -o -IaC "terraform" -cicd "azuredevops" #### Downloads and pulls down the specified release version from the remote GitHub repository to a local directory ```powershell -Get-ALZGithubRelease -githubRepoUrl "https://github.com/Azure/ALZ-Bicep" -releases "v0.14.0" -directoryForReleases "C:\Repos\ALZ\accelerator\upstream-releases\" +Get-ALZGithubRelease -i "bicep" -v "v0.14.0" -o "C:\Repos\ALZ\accelerator" ``` ## Development diff --git a/src/ALZ/Private/New-ALZEnvironmentBicep.ps1 b/src/ALZ/Private/New-ALZEnvironmentBicep.ps1 index 6af7baf0..fc174937 100644 --- a/src/ALZ/Private/New-ALZEnvironmentBicep.ps1 +++ b/src/ALZ/Private/New-ALZEnvironmentBicep.ps1 @@ -16,8 +16,6 @@ function New-ALZEnvironmentBicep { [string] $alzCicdPlatform ) - $bicepModuleUrl = "https://github.com/Azure/ALZ-Bicep" - if ($PSCmdlet.ShouldProcess("ALZ-Bicep module configuration", "modify")) { if($alzVersion -ne "latest" -and $alzVersion -notlike "*-preview") { @@ -34,7 +32,7 @@ function New-ALZEnvironmentBicep { $alzEnvironmentDestinationInternalCode = Join-Path $alzEnvironmentDestination "upstream-releases" # Downloading the latest or specified version of the bicep accelerator module - $releaseTag = Get-ALZGithubRelease -directoryForReleases $alzEnvironmentDestinationInternalCode -githubRepoUrl $bicepModuleUrl -release $alzVersion + $releaseTag = Get-ALZGithubRelease -directoryForReleases $alzEnvironmentDestination -iac "bicep" -release $alzVersion $releasePath = Join-Path -Path $alzEnvironmentDestinationInternalCode -ChildPath $releaseTag # Getting the configuration diff --git a/src/ALZ/Private/New-ALZEnvironmentTerraform.ps1 b/src/ALZ/Private/New-ALZEnvironmentTerraform.ps1 index 353d92da..dc0ddc27 100644 --- a/src/ALZ/Private/New-ALZEnvironmentTerraform.ps1 +++ b/src/ALZ/Private/New-ALZEnvironmentTerraform.ps1 @@ -23,8 +23,6 @@ function New-ALZEnvironmentTerraform { [switch] $autoApprove ) - $terraformModuleUrl = "https://github.com/Azure/alz-terraform-accelerator" - if ($PSCmdlet.ShouldProcess("ALZ-Terraform module configuration", "modify")) { Write-InformationColored "Downloading alz-terraform-accelerator Terraform module to $alzEnvironmentDestination" -ForegroundColor Green -InformationAction Continue @@ -36,7 +34,7 @@ function New-ALZEnvironmentTerraform { } # Downloading the latest or specified version of the alz-terraform-accelerator module - $releaseTag = Get-ALZGithubRelease -directoryForReleases $alzEnvironmentDestination -githubRepoUrl $terraformModuleUrl -release $alzVersion + $releaseTag = Get-ALZGithubRelease -directoryForReleases $alzEnvironmentDestination -iac "terraform" -release $alzVersion $releasePath = Join-Path -Path $alzEnvironmentDestination -ChildPath $releaseTag # Getting the configuration for the initial bootstrap user input and validators diff --git a/src/ALZ/Public/Get-ALZGithubRelease.ps1 b/src/ALZ/Public/Get-ALZGithubRelease.ps1 index 30b63ebd..6e5e7601 100644 --- a/src/ALZ/Public/Get-ALZGithubRelease.ps1 +++ b/src/ALZ/Public/Get-ALZGithubRelease.ps1 @@ -22,16 +22,28 @@ Checks for the releases of a GitHub repository and downloads the latest release function Get-ALZGithubRelease { [CmdletBinding()] param ( - [Parameter(Mandatory = $true, Position = 1, HelpMessage = "Please the provide the full URL of the GitHub repository you wish to check for the latest release.")] + [Parameter(Mandatory = $true, Position = 0, HelpMessage = "The IaC provider to use for the ALZ environment.")] + [ValidateSet("bicep", "terraform")] + [Alias("Iac")] + [Alias("i")] [string] - $githubRepoUrl, + $alzIacProvider, + + [Parameter(Mandatory = $false, Position = 1, HelpMessage = "Please the provide the full URL of the GitHub repository you wish to check for the latest release.")] + [string] + $githubRepoUrl = "", [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.")] [array] + [Alias("version")] + [Alias("v")] $release = "latest", [Parameter(Mandatory = $false, Position = 3, HelpMessage = "The directory to download the releases to. Defaults to the current directory.")] [string] + [Alias("Output")] + [Alias("OutputDirectory")] + [Alias("O")] $directoryForReleases = "$PWD/releases", [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 { $queryOnly ) + # Set the repository URL if not provided + $bicepModuleUrl = "https://github.com/Azure/ALZ-Bicep" + $terraformModuleUrl = "https://github.com/Azure/alz-terraform-accelerator" + if($githubRepoUrl -eq "") { + if($alzIacProvider -eq "bicep") { + $githubRepoUrl = $bicepModuleUrl + } elseif($alzIacProvider -eq "terraform") { + $githubRepoUrl = $terraformModuleUrl + } + } + + $parentDirectory = $directoryForReleases + # Bicep specific path setup + if($alzIacProvider -eq "bicep") { + $directoryForReleases = Join-Path $directoryForReleases "upstream-releases" + } + # Split Repo URL into parts $repoOrgPlusRepo = $githubRepoUrl.Split("/")[-2..-1] -join "/" @@ -81,7 +110,7 @@ function Get-ALZGithubRelease { New-Item -ItemType Directory -Path $directoryForReleases | Out-String | Write-Verbose } - # Check the firectory for this release + # Check the directory for this release $releaseDirectory = "$directoryForReleases/$releaseTag" Write-Verbose "===> Checking if directory for release version exists: $releaseDirectory" @@ -117,8 +146,18 @@ function Get-ALZGithubRelease { Remove-Item -Path "$releaseDirectory/tmp" -Force -Recurse } else { + 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 Write-Verbose "===> Content already exists in $releaseDirectory. Skipping" } + # Check and replace the .env file release version if it is Bicep + if($alzIacProvider -eq "bicep") { + $envFilePath = Join-Path -Path $parentDirectory -ChildPath ".env" + if(Test-Path $envFilePath) { + Write-Verbose "===> Replacing the .env file release version with $releaseTag" + (Get-Content $envFilePath) -replace "UPSTREAM_RELEASE_VERSION=.*", "UPSTREAM_RELEASE_VERSION=$releaseTag" | Set-Content $envFilePath + } + } + return $releaseTag } \ No newline at end of file diff --git a/src/ALZ/Public/New-ALZEnvironment.ps1 b/src/ALZ/Public/New-ALZEnvironment.ps1 index b5dbb366..327df430 100644 --- a/src/ALZ/Public/New-ALZEnvironment.ps1 +++ b/src/ALZ/Public/New-ALZEnvironment.ps1 @@ -35,16 +35,20 @@ function New-ALZEnvironment { [Parameter(Mandatory = $false)] [Alias("alzBicepVersion")] + [Alias("version")] + [Alias("v")] [string] $alzVersion = "latest", [Parameter(Mandatory = $false)] [ValidateSet("bicep", "terraform")] [Alias("Iac")] + [Alias("i")] [string] $alzIacProvider = "bicep", [Parameter(Mandatory = $false)] [ValidateSet("github", "azuredevops")] [Alias("Cicd")] + [Alias("c")] [string] $alzCicdPlatform = "github", [Parameter(Mandatory = $false)] diff --git a/src/Tests/Unit/Public/Get-ALZGithubRelease.Tests.ps1 b/src/Tests/Unit/Public/Get-ALZGithubRelease.Tests.ps1 index e4fa4915..9551f5cd 100644 --- a/src/Tests/Unit/Public/Get-ALZGithubRelease.Tests.ps1 +++ b/src/Tests/Unit/Public/Get-ALZGithubRelease.Tests.ps1 @@ -100,13 +100,13 @@ InModuleScope 'ALZ' { } It 'Should get the correct releases' { - Get-ALZGithubRelease -githubRepoUrl "http://github.com/test/repo" -directoryAndFilesToKeep @('repo-1.0.0') -directoryForReleases "output" + Get-ALZGithubRelease -iac "bicep" -githubRepoUrl "http://github.com/test/repo" -directoryAndFilesToKeep @('repo-1.0.0') -directoryForReleases "output" Should -Invoke Expand-Archive Should -Not -Invoke Write-Warning } It 'Should throw an exception when you ask for a release that does not exist' { - { Get-ALZGithubRelease -githubRepoUrl "http://github.com/test/repo" -release 'v2.0.0' -directoryAndFilesToKeep @('repo-1.0.0') -directoryForReleases "output" } | Should -Throw + { Get-ALZGithubRelease -iac "bicep" -githubRepoUrl "http://github.com/test/repo" -release 'v2.0.0' -directoryAndFilesToKeep @('repo-1.0.0') -directoryForReleases "output" } | Should -Throw Should -Invoke Write-Error } }