Skip to content

Instantly share code, notes, and snippets.

@johnlokerse
Last active March 21, 2024 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnlokerse/66b1f8c86053307fe60ff33037417d16 to your computer and use it in GitHub Desktop.
Save johnlokerse/66b1f8c86053307fe60ff33037417d16 to your computer and use it in GitHub Desktop.
Lint Azure Bicep templates in Azure DevOps blog
trigger: none
parameters:
- name: OnlyLintChangedFiles
type: boolean
default: true
displayName: "Only lint changed files"
pool:
vmImage: windows-latest
steps:
- task: PowerShell@2
displayName: "[Lint] Azure Bicep Template Files"
inputs:
targetType: "inline"
pwsh: true
script: |
$lintChangedFiles = ${{ parameters.OnlyLintChangedFiles }}
$bicepTemplates = @()
$failedLints = @() # Array to hold paths of failed lints
if ($lintChangedFiles) {
$bicepTemplates = git diff --name-only --diff-filter=AM $(git merge-base HEAD refs/heads/main) HEAD | Select-String -Pattern "\.bicep$"
} else {
$bicepTemplates = Get-ChildItem -Path $(Build.SourcesDirectory) -Recurse -Filter *.bicep | Select-Object FullName -ExpandProperty FullName
}
Write-Host "Found $($bicepTemplates.Count) Bicep templates to lint:"
$bicepTemplates
foreach ($path in $bicepTemplates) {
Write-Host "[Lint] Linting Bicep template $path"
$output = az bicep lint --file $path --only-show-errors 2>&1
if ($LASTEXITCODE -ne 0) {
$failedLints += [PSCustomObject]@{
Path = $path
ErrorMessage = $output
}
}
}
if ($failedLints.Count -gt 0) {
Write-Host "##[error][Failed] Linting failed for the following Bicep templates:"
[System.Environment]::NewLine
foreach ($failedLint in $failedLints) {
Write-Host "Path: $($failedLint.Path)"
Write-Host "Error: $($failedLint.ErrorMessage)"
[System.Environment]::NewLine
}
exit 1
} else {
Write-Host "All Bicep templates passed linting."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment