Skip to content

Commit

Permalink
Merge pull request #15 from sam-cogan/samcogan/az-bicep
Browse files Browse the repository at this point in the history
Add Az Bicep Option
  • Loading branch information
sam-cogan authored Dec 24, 2022
2 parents aff7530 + a210eb4 commit c38903b
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 31 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ You can also provide these optional parameters:
7. A boolean to indicate whether we should ignore the exit code of the tests and so not fail the build on a failed test (advanced section)
8. A boolean to indicate whether we should use PowerShell Core on Windows machines. On Linux, PowerShell core will always be used (advanced section)
9. An Azure RM service connection providing credentials to connect to Azure for testing code that uses Bicep Module Registries or Template Specs

10. A boolean to indicate whether to use the Azure CLI version of bicep (Az Bicep) instead of the standard Bicep.exe. The Az CLI must be installed and the ```az bicep install``` command needs to be run prior to running tests, the extension will not install it for you. If you leave this set to false the extension will install Bicep.exe for you if needed.
```yaml
- task: RunARMTTKTestsXPlat@1
inputs:
Expand All @@ -53,6 +53,7 @@ You can also provide these optional parameters:
ignoreExitCode: false
usePSCore: true
azureServiceConnection: serviceConnectionName
useAzBicep: false
```
### Test Results
Expand Down
23 changes: 23 additions & 0 deletions Tests/invoke-ttk.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,29 @@ describe "bicep file tests"{

}

describe "bicep file tests with Az Bicep"{
BeforeAll{
$testPath = "TestDrive:\"
}
write-host "$here"
it "has generated the correct result files"{


try{
Invoke-TTK -templatelocation "$here\testfiles\bicep" -resultlocation "$testPath" -useAzBicep $true
}
catch{
$_.Exception.Message | should -be "Failures found in test results"
}
finally{
Get-ChildItem $testPath
$(Get-ChildItem $testPath).count | should -be 2
}

}

}

describe "Recursing"{
BeforeAll{
$testPath = "TestDrive:\"
Expand Down
Empty file modified Tests/testfiles/bicep/sql.json
100644 → 100755
Empty file.
Empty file modified Tests/testfiles/bicep/storage.json
100644 → 100755
Empty file.
58 changes: 33 additions & 25 deletions arm-ttk-extension-xplatform/powershell/invoke-ttk.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ Function Invoke-TTK {
[string] $subscriptionId,
[string] $clientId,
[string] $clientSecret,
[string] $tenantId
[string] $tenantId,
[boolean] $useAzBicep = $false

)

Expand Down Expand Up @@ -92,35 +93,42 @@ Function Invoke-TTK {
}

if($bicepFiles.count -gt 0){
if($isWindows){
if ((Get-Command "bicep.exe" -ErrorAction SilentlyContinue) -eq $null -and (Get-Command "$PSScriptRoot\bicep.exe" -ErrorAction SilentlyContinue) -eq $null) {
write-Host "Bicep Not Found, Downloading Bicep for Windows..."
(New-Object Net.WebClient).DownloadFile("https://github.com/Azure/bicep/releases/latest/download/bicep-win-x64.exe", "$PSScriptRoot\bicep.exe")
}
$bicepCommand = "bicep.exe"
if((Get-Command "bicep" -ErrorAction SilentlyContinue) -eq $null){
$bicepCommand = "$PSScriptRoot\bicep.exe"
}
foreach($bicepFile in $bicepFiles){
& $bicepCommand build $bicepFile
}
if($useAzBicep){
$bicepCommand = "az bicep"
}
if($isLinux){
if ((Get-Command "bicep" -ErrorAction SilentlyContinue) -eq $null -and (Get-Command "$PSScriptRoot/bicep" -ErrorAction SilentlyContinue) -eq $null) {
write-Host "Bicep Not Found, Downloading Bicep for Linux..."
(New-Object Net.WebClient).DownloadFile("https://github.com/Azure/bicep/releases/latest/download/bicep-linux-x64", "$PSScriptRoot/bicep")
chmod +x "$PSScriptRoot/bicep"
else {
if($isWindows){
if ((Get-Command "bicep.exe" -ErrorAction SilentlyContinue) -eq $null -and (Get-Command "$PSScriptRoot\bicep.exe" -ErrorAction SilentlyContinue) -eq $null) {
write-Host "Bicep Not Found, Downloading Bicep for Windows..."
(New-Object Net.WebClient).DownloadFile("https://github.com/Azure/bicep/releases/latest/download/bicep-win-x64.exe", "$PSScriptRoot\bicep.exe")
}
$bicepCommand = "bicep.exe"
if((Get-Command "bicep" -ErrorAction SilentlyContinue) -eq $null){
$bicepCommand = "$PSScriptRoot\bicep.exe"
}
}
if($isLinux){
if ((Get-Command "bicep" -ErrorAction SilentlyContinue) -eq $null -and (Get-Command "$PSScriptRoot/bicep" -ErrorAction SilentlyContinue) -eq $null) {
write-Host "Bicep Not Found, Downloading Bicep for Linux..."
(New-Object Net.WebClient).DownloadFile("https://github.com/Azure/bicep/releases/latest/download/bicep-linux-x64", "$PSScriptRoot/bicep")
chmod +x "$PSScriptRoot/bicep"
}
$bicepCommand = "bicep"
if((Get-Command "bicep" -ErrorAction SilentlyContinue) -eq $null){
$bicepCommand = "$PSScriptRoot/bicep"
}
}
$bicepCommand = "bicep"
if((Get-Command "bicep" -ErrorAction SilentlyContinue) -eq $null){
$bicepCommand = "$PSScriptRoot/bicep"
}

foreach($bicepFile in $bicepFiles){
if($useAzBicep){
$cmd = "$bicepCommand build --file $bicepFile"
}
foreach($bicepFile in $bicepFiles){
write-host "building $bicepFile"
& $bicepCommand build $bicepFile
else {
$cmd = "$bicepCommand build $bicepFile"
}
invoke-expression $cmd
}

if (!($item -is [System.IO.DirectoryInfo])) {
$templatelocation = $templatelocation.replace(".bicep",".json")
}
Expand Down
5 changes: 3 additions & 2 deletions arm-ttk-extension-xplatform/powershell/ps-runner.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Param(
[string] $subscriptionId,
[string] $clientId,
[string] $clientSecret,
[string] $tenantId
[string] $tenantId,
[switch] $useAzBicep
)

Import-Module "$PSScriptRoot\..\arm-ttk\arm-ttk.psd1"
Expand Down Expand Up @@ -42,4 +43,4 @@ else{
}


Invoke-TTK -templateLocation $templateLocation -resultLocation $resultLocation -Test $Test -Skip $Skip -mainTemplates $Main -allTemplatesAreMain $allTemplatesAreMain -cliOutputResults $cliOutputResults -ignoreExitCode $ignoreExitCode -recurse $recurse -subscriptionId $subscriptionId -clientId $clientId -clientSecret $clientSecret -tenantId $tenantId
Invoke-TTK -templateLocation $templateLocation -resultLocation $resultLocation -Test $Test -Skip $Skip -mainTemplates $Main -allTemplatesAreMain $allTemplatesAreMain -cliOutputResults $cliOutputResults -ignoreExitCode $ignoreExitCode -recurse $recurse -subscriptionId $subscriptionId -clientId $clientId -clientSecret $clientSecret -tenantId $tenantId -useAzBicep $useAzBicep
13 changes: 11 additions & 2 deletions arm-ttk-extension-xplatform/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"author": "Sam Cogan",
"version": {
"Major": 1,
"Minor": 1,
"Patch": 13
"Minor": 2,
"Patch": 0
},
"instanceNameFormat": "Run Azure RM TTK Tests (Cross Platform)",
"groups": [
Expand Down Expand Up @@ -102,6 +102,15 @@
"defaultValue": false,
"groupName": "advanced"
},
{
"name": "useAzBicep",
"type": "boolean",
"label": "Use Az CLI version of Bicep (az bicep)",
"required": false,
"helpMarkDown": "Use the Azure CLI version of Bicep for transpiling Bicep files (az bicep command). This requires the Azure CLI to be present on the machine and bicep installed using this CLI",
"defaultValue": false,
"groupName": "advanced"
},
{
"name": "azureServiceConnection",
"type": "connectedService:AzureRM",
Expand Down
6 changes: 5 additions & 1 deletion arm-ttk-extension-xplatform/ttk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function run() {
let ignoreExitCode = tl.getBoolInput("ignoreExitCode");
let recurse = tl.getBoolInput("recurse");
let azureServiceConnection = tl.getInput("azureServiceConnection",false)

let useAzBicep = tl.getBoolInput("useAzBicep")

// we need to get the verbose flag passed in as script flag
var verbose = (tl.getVariable("System.Debug") === "true");
Expand Down Expand Up @@ -82,6 +82,10 @@ export async function run() {
if (recurse) {
args.push("-recurse");
}
if (useAzBicep) {
args.push("-useAzBicep");
}



if(azureServiceConnection !== undefined){
Expand Down

0 comments on commit c38903b

Please sign in to comment.