-
Notifications
You must be signed in to change notification settings - Fork 173
Add ARM template validation with GitHub Actions and PSRule #1606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
9
commits into
dev
Choose a base branch
from
copilot/fix-2
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+646
β0
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
40fc824
Initial plan for issue
Copilot f616f73
Create GitHub Actions workflow for ARM template validation
Copilot d172e1b
Update documentation with ARM template validation information
Copilot b9e1d0e
Add Test-ArmTemplate script and update documentation
Copilot 93ec5ee
Potential fix for code scanning alert no. 4: Workflow does not contaiβ¦
MSBrett 3356e4b
Update Azure CLI setup action to v3
Copilot 58f321b
Implement phased rollout for ARM template validation
d956478
Merge branch 'dev' into copilot/fix-2
MSBrett aab720f
docs: fix version number (v13 not v0.13)
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,177 @@ | ||
name: 'ARM Template Validation' | ||
|
||
permissions: | ||
contents: read | ||
|
||
# Phase 1 of ARM template validation rollout - workflow is disabled for CI/CD | ||
# To enable in Phase 2, uncomment the 'on' section below | ||
# on: | ||
# pull_request: | ||
# paths: | ||
# - 'src/templates/**' | ||
# - 'src/bicep-registry/**' | ||
# - '.github/workflows/arm-template-validation.yml' | ||
|
||
# Workflow can still be run manually during Phase 1 | ||
on: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
validate_templates: | ||
name: Validate ARM Templates | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup Az PowerShell module | ||
shell: pwsh | ||
run: | | ||
Install-Module -Name Az -Force -AllowClobber -Scope CurrentUser | ||
Install-Module -Name PSRule.Rules.Azure -Force -Scope CurrentUser | ||
|
||
- name: Setup Azure CLI | ||
uses: azure/setup-azure-cli@v3 | ||
|
||
- name: Setup Bicep | ||
uses: anthony-c-martin/[email protected] | ||
|
||
- name: Build templates | ||
shell: pwsh | ||
run: | | ||
cd ${{ github.workspace }} | ||
./src/scripts/Build-Toolkit | ||
|
||
- name: Download ARM-TTK | ||
shell: pwsh | ||
run: | | ||
cd ${{ github.workspace }} | ||
# ARM-TTK version pinning - using stable release 0.26 (20250401) | ||
# Update this version when newer stable releases are available | ||
$armTtkVersion = "20250401" | ||
$armTtkPath = "./release/.tools/arm-ttk" | ||
|
||
New-Item -Path $armTtkPath -ItemType Directory -Force | ||
Write-Host "Downloading ARM-TTK version $armTtkVersion..." | ||
$armTtkZip = "$armTtkPath/arm-ttk-$armTtkVersion.zip" | ||
Invoke-WebRequest -Uri "https://github.com/Azure/arm-ttk/archive/refs/tags/$armTtkVersion.zip" -OutFile $armTtkZip | ||
|
||
# Extract to a versioned subfolder | ||
$extractPath = "$armTtkPath/arm-ttk-$armTtkVersion" | ||
Expand-Archive -Path $armTtkZip -DestinationPath $extractPath -Force | ||
|
||
# Clean up the zip file | ||
Remove-Item -Path $armTtkZip -Force | ||
|
||
Import-Module "$armTtkPath/arm-ttk-$armTtkVersion/arm-ttk-$armTtkVersion/arm-ttk/arm-ttk.psd1" -Force | ||
|
||
- name: Validate templates with PSRule | ||
shell: pwsh | ||
run: | | ||
cd ${{ github.workspace }} | ||
|
||
# Get all ARM JSON templates | ||
$templates = Get-ChildItem -Path "release" -Filter "*.json" -Recurse | ||
|
||
foreach ($template in $templates) { | ||
Write-Host "Validating template: $($template.FullName)" | ||
|
||
# Run PSRule validation | ||
$results = $template.FullName | Invoke-PSRule -Module PSRule.Rules.Azure -WarningAction SilentlyContinue | ||
|
||
# Check for failures | ||
$failures = $results | Where-Object { $_.Outcome -eq 'Fail' } | ||
if ($failures) { | ||
Write-Host "::error::PSRule validation failed for $($template.Name):" | ||
$failures | Format-Table -Property RuleName, TargetName, Message -AutoSize | Out-String | Write-Host | ||
exit 1 | ||
} | ||
} | ||
|
||
Write-Host "All templates validated successfully with PSRule!" | ||
|
||
- name: Validate templates with ARM-TTK | ||
shell: pwsh | ||
run: | | ||
cd ${{ github.workspace }} | ||
|
||
# Get all ARM JSON templates | ||
$templates = Get-ChildItem -Path "release" -Filter "*.json" -Recurse | ||
|
||
$hasErrors = $false | ||
|
||
foreach ($template in $templates) { | ||
Write-Host "Validating template with ARM-TTK: $($template.FullName)" | ||
|
||
# Run ARM-TTK validation | ||
$testResults = Test-AzTemplate -TemplatePath $template.FullName | ||
|
||
# Check for failures | ||
$failures = $testResults | Where-Object { -not $_.Passed } | ||
if ($failures) { | ||
$hasErrors = $true | ||
Write-Host "::error::ARM-TTK validation failed for $($template.Name):" | ||
$failures | Format-Table -Property Name, Group, Errors -AutoSize | Out-String | Write-Host | ||
} | ||
} | ||
|
||
if ($hasErrors) { | ||
exit 1 | ||
} | ||
|
||
Write-Host "All templates validated successfully with ARM-TTK!" | ||
|
||
- name: Validate templates with az CLI | ||
shell: pwsh | ||
run: | | ||
cd ${{ github.workspace }} | ||
|
||
# Get all ARM JSON templates | ||
$templates = Get-ChildItem -Path "release" -Filter "*.json" -Recurse | ||
|
||
$hasErrors = $false | ||
|
||
foreach ($template in $templates) { | ||
Write-Host "Validating template with az CLI: $($template.FullName)" | ||
|
||
# Skip files that are not ARM templates (like UI definitions) | ||
if ($template.Name -like "*.ui.json") { | ||
Write-Host "Skipping UI definition file: $($template.Name)" | ||
continue | ||
} | ||
|
||
# Determine deployment scope based on template content | ||
$templateContent = Get-Content -Path $template.FullName -Raw | ConvertFrom-Json | ||
$deploymentScope = if ($templateContent.resources -and $templateContent.resources[0].type -eq "Microsoft.Resources/deployments") { | ||
# This is likely a subscription level template | ||
"subscription" | ||
} else { | ||
# Default to resource group level | ||
"resourcegroup" | ||
} | ||
|
||
# Run appropriate az validate command based on scope | ||
try { | ||
if ($deploymentScope -eq "subscription") { | ||
Write-Host "Running subscription-level validation" | ||
az deployment sub validate --location eastus --template-file $template.FullName --no-prompt | ||
} else { | ||
Write-Host "Running resource-group level validation" | ||
az deployment group validate --resource-group "validation-rg" --template-file $template.FullName --no-prompt | ||
} | ||
|
||
if ($LASTEXITCODE -ne 0) { | ||
$hasErrors = $true | ||
Write-Host "::error::Azure CLI validation failed for $($template.Name)" | ||
} | ||
} catch { | ||
$hasErrors = $true | ||
Write-Host "::error::Exception during Azure CLI validation for $($template.Name): $_" | ||
} | ||
} | ||
|
||
if ($hasErrors) { | ||
exit 1 | ||
} | ||
|
||
Write-Host "All templates validated successfully with az CLI!" |
This file contains hidden or 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 |
---|---|---|
|
@@ -368,3 +368,4 @@ venv/ | |
ENV/ | ||
env/ | ||
|
||
CLAUDE.md |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace azure/setup-azure-cli@v1 with the latest supported version. As of now, the latest version is azure/setup-azure-cli@v3. Update the workflow as follows:
uses: azure/setup-azure-cli@v3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the corrected part:
jobs:
validate_templates:
name: Validate ARM Templates
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the Azure CLI setup action from v1 to v3 as requested. The change can be found in commit 3356e4b.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Azure CLI setup action has already been updated from v1 to v3 as requested. This change was implemented in commit 3356e4b.