-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
242 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.