Skip to content

Commit

Permalink
Merge branch 'Azure:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Brunoga-MS authored Oct 11, 2024
2 parents c532095 + 78ca438 commit bd6136a
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 123 deletions.
72 changes: 72 additions & 0 deletions .github/actions-pester/Test-ArmTemplates.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<# Script to validate ARM templates using the Test-AzTemplate cmdlet #>

# Define a function to get the list of changed files in a pull request
function Get-ChangedFile {
[CmdletBinding(SupportsShouldProcess)]
param (
# Parameter to filter files by path
[Parameter()]
[String]$pathFilter,

# Parameter to filter files by extension
[Parameter()]
[String]$extensionFilter,

# Parameter to specify the pull request branch, defaulting to the GitHub head reference
[Parameter()]
[String]$PRBranch = "$($env:GITHUB_HEAD_REF)"
)
# Get the list of changed files between the main branch and the pull request branch
$changedFiles = git diff --name-only origin/main origin/$PRBranch

# Create a regex pattern to filter files based on the provided path and extension
$regex = "$pathFilter.*\.$extensionFilter"

# Filter the changed files using the regex pattern
$resultFiles = $changedFiles | Where-Object { $PSItem -match $regex }

# Return the filtered files
$resultFiles | ForEach-Object {
return $_
}
}

# Get the list of modified ARM template files
$ModifiedFiles = @(Get-ChangedFile -pathFilter 'templates/arm' -extensionFilter 'json')

# Check if there are any modified ARM template files
if ($null -ne $ModifiedFiles) {
Write-Output "These are the modified ARM templates: $($ModifiedFiles)"
} else {
Write-Output "There are no modified ARM templates"
}

# Initialize a counter for the number of failed tests
$NumberOfFailedTests = 0

# Iterate over each modified ARM template file
$ModifiedFiles | ForEach-Object {
$TemplatePath = $PSItem
Write-Output "Test $TemplatePath"

# Run the Test-AzTemplate cmdlet to test the ARM template
$testResults = Test-AzTemplate -TemplatePath $TemplatePath -Test deploymentTemplate -ErrorAction Continue

# Filter the test results to find any failed tests
$failedTests = $testResults | Where-Object { $PSItem.Passed -ne $True }

# If there are failed tests, log a warning and increment the failed tests counter
if ($failedTests -ne $null) {
$failedTests | ForEach-Object {
Write-Warning "$($PSItem | Out-String)"
$NumberOfFailedTests++
}
}
}

# If there are any failed tests, log an error and exit with a non-zero status
If ($NumberOfFailedTests -gt 0) {
Write-Error "There are $NumberOfFailedTests failed tests"
exit 1
}

43 changes: 43 additions & 0 deletions .github/workflows/unit-test-arm-templates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
name: Unit Test ARM templates with arm-ttk

##########################################
# Start the job on PR for all branches #
#########################################

# yamllint disable-line rule:truthy
on:
pull_request:
types:
- opened
- reopened
- synchronize
- ready_for_review
paths:
- "services/**/**/templates/arm/**.json"
workflow_dispatch: { }

permissions:
id-token: write
contents: read

jobs:
validate-arm-files:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{github.event.pull_request.head.ref}}
repository: ${{github.event.pull_request.head.repo.full_name}}
- name: Clone ARM-TTK repo
uses: GuillaumeFalourd/clone-github-repo-action@v3
with:
owner: 'Azure'
repository: 'arm-ttk'
- name: Test Modified ARM templates
shell: pwsh
run: |
Import-Module ./arm-ttk/arm-ttk/arm-ttk.psd1
./.github/actions-pester/Test-ArmTemplates.Tests.ps1
Loading

0 comments on commit bd6136a

Please sign in to comment.