Skip to content
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

[CI Environment] Ported latest AVM pipline changes (e.g. naming) #4477

Merged
merged 41 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
ba3cd1a
Ported several changes
AlexanderSehr Feb 4, 2024
5e395a9
Update to latest
AlexanderSehr Feb 4, 2024
1053fca
Update to latest
AlexanderSehr Feb 4, 2024
557a200
Fixed PSRule setup
AlexanderSehr Mar 3, 2024
25743b9
Updated pester test file path
AlexanderSehr Mar 3, 2024
69cfc12
Update to latest
AlexanderSehr Mar 3, 2024
ea82433
Update to latest
AlexanderSehr Mar 3, 2024
8b23021
Update to latest
AlexanderSehr Mar 3, 2024
d9298d8
Update to latest
AlexanderSehr Mar 3, 2024
5cb8ba2
Update to latest
AlexanderSehr Mar 3, 2024
25ae64e
Update to latest
AlexanderSehr Mar 3, 2024
c90aa06
Update to latest
AlexanderSehr Mar 3, 2024
2d5cae5
Update to latest
AlexanderSehr Mar 3, 2024
5dfbab5
Update to latest
AlexanderSehr Mar 3, 2024
4742bfa
Update to latest
AlexanderSehr Mar 3, 2024
6176c43
Changed pwsh
AlexanderSehr Mar 3, 2024
49dd490
Update to latest
AlexanderSehr Mar 3, 2024
bda4dfc
Update to latest
AlexanderSehr Mar 3, 2024
c9bfa8d
Tried alternative path ref
AlexanderSehr Mar 3, 2024
d29e786
Added PS Rule ref
AlexanderSehr Mar 3, 2024
00a7aa9
Update to latest
AlexanderSehr Mar 3, 2024
dee19c3
Update to latest
AlexanderSehr Mar 3, 2024
4220654
Update to latest
AlexanderSehr Mar 3, 2024
1f844ae
Update to latest
AlexanderSehr Mar 3, 2024
31fb8d7
Update to latest
AlexanderSehr Mar 3, 2024
4ccdb2e
Update to latest
AlexanderSehr Mar 3, 2024
a4251fd
Update to latest
AlexanderSehr Mar 3, 2024
d0f1e72
Update to latest
AlexanderSehr Mar 3, 2024
f1f0d16
Update to latest
AlexanderSehr Mar 3, 2024
1720291
Update to latest
AlexanderSehr Mar 3, 2024
e219061
Update to latest
AlexanderSehr Mar 3, 2024
7b19604
Update to latest
AlexanderSehr Mar 3, 2024
242ffd4
Update to latest
AlexanderSehr Mar 3, 2024
e658b56
Renamed paths
AlexanderSehr Mar 3, 2024
c398654
Updated to more specific naming to avoid overlaps with e.g., pwsh pat…
AlexanderSehr Mar 3, 2024
44dd3ea
Undid unrelated changes
AlexanderSehr Mar 3, 2024
13cb3eb
Added Az.Resources install to PSRule prep
AlexanderSehr Mar 3, 2024
cdace73
Update to latest
AlexanderSehr Mar 3, 2024
99ca0a5
GH PS Rule config fix
AlexanderSehr Mar 4, 2024
a599bb2
Update to latest
AlexanderSehr Mar 4, 2024
6bfe276
Update to latest
AlexanderSehr Mar 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 41 additions & 14 deletions .azuredevops/pipelineTemplates/jobs.getModuleTestFiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ parameters:

# Logic-related parameters
modulePath: '$(modulePath)'
psRuleFilterRegex: '(defaults|waf-aligned)'

##---------------------------------------------##
## TEMPLATE LOGIC ##
Expand All @@ -19,32 +20,58 @@ jobs:
name: ${{ parameters.poolName }}
steps:
- task: PowerShell@2
displayName: 'Get parameter files'
displayName: 'Get module test file paths'
name: getModuleTestFilesTask
inputs:
targetType: inline
pwsh: true
script: |
## ======= ##
## All ##
## ======= ##

# Get the list of parameter file paths
$moduleFolderPath = Join-Path '$(System.DefaultWorkingDirectory)' '${{ parameters.modulePath }}'
$testFilePaths = (Get-ChildItem -Path $moduleFolderPath -Recurse -Filter 'main.test.bicep').FullName | Sort-Object

$deploymentTestPaths = $testFilePaths | ForEach-Object {
$testFilePaths = (Get-ChildItem -Path $moduleFolderPath -Recurse -Filter 'main.test.bicep').FullName | Sort-Object
$testFilePaths = $testFilePaths | ForEach-Object {
$_.Replace($moduleFolderPath, '').Trim('\').Trim('/')
}

Write-Verbose 'Found module test files' -Verbose
$deploymentTestPaths | ForEach-Object { Write-Verbose "- [$_]" -Verbose }
Write-Verbose 'Found all module test files' -Verbose
$testFilePaths | ForEach-Object { Write-Verbose "- [$_]" -Verbose }

$testTable = @{}
foreach ($deploymentTestPath in $deploymentTestPaths) {
$deploymentTestFileName = Split-Path (Split-Path $deploymentTestPath -Parent) -Leaf
$testTable[$deploymentTestFileName] += @{
moduleTestFilePath = $deploymentTestPath
}
}
$testFilePaths | ForEach-Object {
$testFileName = Split-Path (Split-Path $_) -Leaf
$testTable[$testFileName] = @{
moduleTestFilePath = $_
moduleTestFileName = $testFileName
}
} | ConvertTo-Json -Compress
$deployCompressedOutput = $testTable | ConvertTo-Json -Compress

Write-Verbose "Publishing output: $deployCompressedOutput" -Verbose
Write-Host ('##vso[task.setVariable variable=moduleTestFilePaths;isOutput=true]{0}' -f $deployCompressedOutput)

$deploymentTestPathsOutput = $testTable | ConvertTo-Json -Compress
## =========== ##
## PS-Rule ##
## =========== ##

$psRuleTestFilePaths = $testFilePaths | Where-Object { $_ -match '${{ parameters.psRuleFilterRegex }}' }

Write-Verbose 'Found PSRule module test files' -Verbose
$psRuleTestFilePaths | ForEach-Object { Write-Verbose "- [$_]" -Verbose }

$psRuleTestTable = @{}
$psRuleTestFilePaths | ForEach-Object {
$testFileName = Split-Path (Split-Path $_) -Leaf
$psRuleTestTable[$testFileName] = @{
moduleTestFilePath = $_
moduleTestFileName = $testFileName
}
}
$psRuleCompressedOutput = $psRuleTestTable | ConvertTo-Json -Compress

Write-Host ('##vso[task.setVariable variable=moduleTests;isOutput=true]{0}' -f ($testTable | ConvertTo-Json -Compress))
Write-Verbose "Module test files: $deploymentTestPathsOutput" -Verbose
Write-Host ('##vso[task.setVariable variable=psRuleModuleTestFilePaths;isOutput=true]{0}' -f $psRuleCompressedOutput)
Write-Verbose "PS Rule publishing output: $psRuleCompressedOutput" -Verbose
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
## | vmImage | '$(vmImage)' | You can provide either a [poolname] or [vmImage] to run the job on. | 'ubuntu20.04' |
## | defaultJobTimeoutInMinutes | 120 | The timeout for the job in this pipeline. | 120 |
## | removeDeployment | 'true' | Set to [true] to flag resources for removal. If not provided, defaults to true. | 'true' |
## | templateFilePath | '' | Path to the template file to deploy. | 'modules/analysis-services/servers/main.bicep' |
## | customTokens | '' | Additional token pairs in json format. | '{"tokenName":"tokenValue"}' |
## | jobDisplayName | '' | The display name of the job. | 'Deploy module' |
## | modulePath | '$(modulePath)' | The path to the module to deploy. | 'c:/KeyVault' |
Expand All @@ -50,7 +49,6 @@ parameters:
defaultJobTimeoutInMinutes: 120
# Logic-related parameters
removeDeployment: false
templateFilePath: ''
customTokens: ''
modulePath: '$(modulePath)'
location: '$(location)'
Expand All @@ -76,7 +74,7 @@ jobs:
dependsOn:
- getModuleTestFiles
strategy:
matrix: $[ dependencies.getModuleTestFiles.outputs['getModuleTestFilesTask.moduleTests'] ]
matrix: $[ dependencies.getModuleTestFiles.outputs['getModuleTestFilesTask.moduleTestFilePaths'] ]
##---------------------------------------------##
## TEMPLATE LOGIC ##
##---------------------------------------------##
Expand Down Expand Up @@ -117,20 +115,16 @@ jobs:

# [Agent] Replace tokens
#-----------------------
- task: AzurePowerShell@5
displayName: 'Replace tokens in template file via connection [${{ parameters.serviceConnection }}]'
- task: PowerShell@2
displayName: 'Replace tokens in template files'
inputs:
azureSubscription: ${{ parameters.serviceConnection }}
azurePowerShellVersion: 'latestVersion'
preferredAzurePowerShellVersion: ''
ScriptType: InlineScript
targetType: inline
pwsh: true
inline: |
script: |
# Load used functions
. (Join-Path '$(System.DefaultWorkingDirectory)' 'utilities' 'pipelines' 'tokensReplacement' 'Convert-TokensInFileList.ps1')
. (Join-Path '$(System.DefaultWorkingDirectory)' 'utilities' 'pipelines' 'sharedScripts' 'Get-LocallyReferencedFileList.ps1')


# Get target files
$moduleTestFilePath = Join-Path '$(System.DefaultWorkingDirectory)' '$(modulePath)' '$(moduleTestFilePath)'

Expand Down
144 changes: 144 additions & 0 deletions .azuredevops/pipelineTemplates/jobs.validateModulePSRule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#########################################################
## 'Validate module with Pester' Pipeline Template ##
#########################################################
##
## This pipeline template contains the logic to validate a module using a set of Pester tests
##
## Enabled levels of validation
## - Resource-Group-Level
## - Subscription-Level
## - Management-Group-Level
## - Tenant-Level
##
#########################################################
##
##---------------------------------------------##
## TEMPLATE PARAMETERS ##
##---------------------------------------------##
##
## By default it uses the variables specified in the below [parameters] section. However, you can overwrite these variables in the
## referencing pipeline by providing the parameter explicitly.
##
## NOTE: If you don't need to overwrite a shared value, you can IGNORE this section
##
## |==============================================================================================================================================================================================================================================|
## | Parameter | Default Value | Description | Example |
## |----------------------------|-----------------------------------------------|-------------------------------------------------------------------------------------------------------|---------------------------------------------------------|
## | poolName | '$(poolName)' | You can provide either a [poolname] or [vmImage] to run the job on | 'Custom Deployment Pool' |
## | vmImage | '$(vmImage)' | You can provide either a [poolname] or [vmImage] to run the job on | 'ubuntu20.04' |
## | defaultJobTimeoutInMinutes | 120 | The timeout for the job in this pipeline | 120 |
## | modulePath | '$(modulePath)' | The path to the module to deploy. | 'c:/KeyVault' |
## | psrulePath | 'utilities/pipelines/staticValidation/psrule' | The path to the PS-Rule configuration | 'utilities/pipelines/staticValidation/module.tests.ps1' |
## | location | '$(location)' | The location to validate with | 'France Central' |
## | subscriptionId | '$(ARM_SUBSCRIPTION_ID)' | The id of the subscription to validate with when using a Management group service connection | 'aed7c000-6387-412e-bed0-24dfddf4bbc6' |
## | managementGroupId | '$(ARM_MGMTGROUP_ID)' | The id of the management group to validate with. Required only for Management-Group-Level validations | '477c9620-cb01-454f-9ebc-fc6b1df48c14' |
## |==============================================================================================================================================================================================================================================|
##
##---------------------------------------------##

parameters:
# Pipeline-related parameters
poolName: '$(poolName)'
vmImage: '$(vmImage)'
defaultJobTimeoutInMinutes: 120
# Logic-related parameters
modulePath: '$(modulePath)'
psrulePath: 'utilities/pipelines/staticValidation/psrule'
location: '$(location)'
subscriptionId: '$(ARM_SUBSCRIPTION_ID)'
managementGroupId: '$(ARM_MGMTGROUP_ID)'

##---------------------------------------------##
## TEMPLATE LOGIC ##
##---------------------------------------------##
jobs:
- template: /.azuredevops/pipelineTemplates/jobs.getModuleTestFiles.yml
- job:
displayName: Run PSRule tests
timeoutInMinutes: ${{ parameters.defaultJobTimeoutInMinutes }}
pool:
${{ if ne(parameters.vmImage, '') }}:
vmImage: ${{ parameters.vmImage }}
${{ if ne(parameters.poolName, '') }}:
name: ${{ parameters.poolName }}
dependsOn:
- getModuleTestFiles
strategy:
matrix: $[ dependencies.getModuleTestFiles.outputs['getModuleTestFilesTask.psRuleModuleTestFilePaths'] ]
steps:
# [Agent] Replace tokens
#-----------------------
- task: PowerShell@2
displayName: 'Replace tokens in template files'
inputs:
targetType: inline
pwsh: true
script: |
# Load used functions
. (Join-Path '$(System.DefaultWorkingDirectory)' 'utilities' 'pipelines' 'tokensReplacement' 'Convert-TokensInFileList.ps1')
. (Join-Path '$(System.DefaultWorkingDirectory)' 'utilities' 'pipelines' 'sharedScripts' 'Get-LocallyReferencedFileList.ps1')

# Get target files
$moduleTestFilePath = Join-Path '$(System.DefaultWorkingDirectory)' '$(modulePath)' '$(moduleTestFilePath)'

# Get target files
$targetFileList = @(
$moduleTestFilePath
)

# Add all module template files as they may contain tokens
$targetFileList += (Get-LocallyReferencedFileList -FilePath $moduleTestFilePath)
$targetFileList = $targetFileList | Sort-Object -Unique

# Construct Token Function Input
$ConvertTokensInputs = @{
FilePathList = $targetFileList
Tokens = @{}
TokenPrefix = '$(tokenPrefix)'
TokenSuffix = '$(tokenSuffix)'
}

# Add enforced tokens
$ConvertTokensInputs.Tokens += @{
subscriptionId = '${{ parameters.subscriptionId }}'
managementGroupId = '${{ parameters.managementGroupId }}'
tenantId = '$(ARM_TENANT_ID)'
}

# Add local (source control) tokens
$tokenMap = @{}
foreach ($token in (Get-ChildItem env: | Where-Object -Property Name -Like "localToken_*")) {
$tokenMap += @{ $token.Name.Replace('localToken_','','OrdinalIgnoreCase') = $token.value }
}
Write-Verbose ('Using local tokens [{0}]' -f ($tokenMap.Keys -join ', ')) -Verbose
$ConvertTokensInputs.Tokens += $tokenMap

# Swap 'namePrefix' token if empty and provided as a Azure DevOps variable
if([String]::IsNullOrEmpty($ConvertTokensInputs.Tokens['namePrefix'])){
Write-Verbose 'Using [namePrefix] token from Azure DevOps Variable Groups' -Verbose
$ConvertTokensInputs.Tokens['namePrefix'] = "$(TOKEN_NAMEPREFIX)"
}

# Add custom tokens (passed in via the pipeline)
if(-not [String]::IsNullOrEmpty('${{ parameters.customTokens }}')) {
$customTokens = '${{ parameters.customTokens }}' | ConvertFrom-Json -AsHashTable
Write-Verbose ('Using custom parameter file tokens [{0}]' -f ($customTokens.Keys -join ', ')) -Verbose
$ConvertTokensInputs.Tokens += $customTokens
}

Write-Verbose "Convert Tokens Input:`n $($ConvertTokensInputs | ConvertTo-Json -Depth 10)" -Verbose

# Invoke Token Replacement Functionality [For Module]
$null = Convert-TokensInFileList @ConvertTokensInputs

- task: ps-rule-assert@2
displayName: Analyze Azure template files
inputs:
inputType: inputPath
modules: 'PSRule.Rules.Azure'
inputPath: '$(System.DefaultWorkingDirectory)/$(modulePath)/$(moduleTestFilePath)'
outputFormat: Csv
option: '${{ parameters.psrulePath}}/ps-rule.yaml' # Path to PSRule configuration options file
source: '${{ parameters.psrulePath}}/.ps-rule/' # Path to folder containing suppression rules to use for analysis.
outputPath: '$(System.DefaultWorkingDirectory)/$(modulePath)/$(moduleTestFilePath)-PSRule-output.csv'
continueOnError: true
Loading
Loading