Skip to content

Commit

Permalink
enhancement: env file version automation (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredfholgate authored Nov 23, 2023
1 parent 1d8a0db commit 15124a4
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 16 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ This currently tests for:
#### Azure Landing Zone Environment with Bicep and GitHub Actions Workflows

```powershell
New-ALZEnvironment -o <output_directory> -IaC "bicep" -cicd "github
New-ALZEnvironment -o <output_directory> -i "bicep" -c "github"
```

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

```powershell
New-ALZEnvironment -o <output_directory> -IaC "bicep" -cicd "azuredevops"
New-ALZEnvironment -o <output_directory> -i "bicep" -c "azuredevops"
```

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

```powershell
New-ALZEnvironment -o <output_directory> -IaC "terraform" -cicd "github"
New-ALZEnvironment -o <output_directory> -i "terraform" -c "github"
```

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

```powershell
New-ALZEnvironment -o <output_directory> -IaC "terraform" -cicd "azuredevops"
New-ALZEnvironment -o <output_directory> -i "terraform" -c "azuredevops"
```

## Additional Cmdlets
Expand All @@ -112,7 +112,7 @@ New-ALZEnvironment -o <output_directory> -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
Expand Down
4 changes: 1 addition & 3 deletions src/ALZ/Private/New-ALZEnvironmentBicep.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/ALZ/Private/New-ALZEnvironmentTerraform.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
45 changes: 42 additions & 3 deletions src/ALZ/Public/Get-ALZGithubRelease.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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.")]
Expand All @@ -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 "/"

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions src/ALZ/Public/New-ALZEnvironment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
4 changes: 2 additions & 2 deletions src/Tests/Unit/Public/Get-ALZGithubRelease.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit 15124a4

Please sign in to comment.