Skip to content

Commit 600f686

Browse files
authored
Enable PR validation for template changes (#1102)
This PR enables template tests to run as part of PR validation for modified templates. Only the specific templates being modified are tested.
1 parent 14ea12b commit 600f686

7 files changed

+110
-15
lines changed

eng/pipelines/repoman.yml

+28-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ stages:
3939
name: azsdk-pool-mms-ubuntu-2004-general
4040
vmImage: MMSUbuntu20.04
4141

42+
variables:
43+
RepoManResultsFile: $(Pipeline.Workspace)/repoman.md
44+
4245
steps:
4346
- checkout: self
4447

@@ -55,10 +58,23 @@ stages:
5558
parameters:
5659
TargetBranchName: pr/$(PRNumber)
5760
RemoteName: $(StagingRemoteName)
61+
ResultsFileLocation: $(RepoManResultsFile)
62+
63+
- task: PowerShell@2
64+
name: DetermineChanged
65+
displayName: Set repoman changed templates
66+
inputs:
67+
pwsh: true
68+
targetType: filePath
69+
filePath: ./eng/scripts/Set-RepomanChangedRepositories.ps1
70+
arguments: >-
71+
-ResultsFile $(RepoManResultsFile)
72+
-OutputTemplatesVariable "TemplateList"
73+
-OutputTemplateBranchVariable "TemplateBranch"
5874
5975
- pwsh: |
6076
$repomanContent = "No changes detected."
61-
$repomanOutputFile = "$([System.IO.Path]::GetTempPath())/repoman.md"
77+
$repomanOutputFile = "$(RepoManResultsFile)"
6278
if (Test-Path $repomanOutputFile) {
6379
$repomanContent = Get-Content $repomanOutputFile -Raw
6480
}
@@ -89,6 +105,17 @@ stages:
89105
env:
90106
GH_TOKEN: $(azuresdk-github-pat)
91107

108+
- template: /eng/pipelines/templates/jobs/run-template-tests.yml
109+
parameters:
110+
TemplateList: $[ dependencies.Generate_Repos_For_PR.outputs['DetermineChanged.TemplateList'] ]
111+
TemplateBranchName: $[ dependencies.Generate_Repos_For_PR.outputs['DetermineChanged.TemplateBranch'] ]
112+
JobCondition: >-
113+
and(
114+
succeeded(),
115+
ne(dependencies.Generate_Repos_For_PR.outputs['DetermineChanged.TemplateList'] , '')
116+
)
117+
JobDependsOn: Generate_Repos_For_PR
118+
92119
- job: Publish_Staging
93120
condition: >-
94121
and(

eng/pipelines/templates/steps/azd-login.yml

+8
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,13 @@ steps:
2525
2626
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
2727
28+
# Export subscription ID
29+
Write-Host "##vso[task.setvariable variable=SubscriptionId]$($subscriptionConfiguration.SubscriptionId)"
30+
31+
# Export service principal auth information for terraform testing
32+
Write-Host "##vso[task.setvariable variable=arm-client-id;issecret=false]$($subscriptionConfiguration.TestApplicationId)"
33+
Write-Host "##vso[task.setvariable variable=arm-client-secret;issecret=true]$($subscriptionConfiguration.TestApplicationSecret)"
34+
Write-Host "##vso[task.setvariable variable=arm-tenant-id;issecret=false]$($subscriptionConfiguration.TenantId)"
35+
2836
condition: and(succeeded(), ne(variables['Skip.LiveTest'], 'true'))
2937
displayName: Azure Dev Login

eng/pipelines/templates/steps/repoman-generate.yml

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ parameters:
22
TargetBranchName: $(TargetBranchName)
33
RemoteName: azure-samples-main
44
GitHubPat: $(azuresdk-github-pat)
5+
ResultsFileLocation: ""
56

67
steps:
78
- task: NodeTool@0
@@ -31,3 +32,4 @@ steps:
3132
arguments: >-
3233
-TargetBranchName "${{ parameters.TargetBranchName }}"
3334
-RemoteName "${{ parameters.RemoteName }}"
35+
-ResultsFileLocation "${{ parameters.ResultsFileLocation }}"

eng/pipelines/templates/steps/template-test-run-job.yml

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
# and additional variables specified by template-test-generate-jobs.yml
55

66
steps:
7-
- template: /eng/pipelines/templates/steps/az-login.yml
8-
97
- template: /eng/pipelines/templates/steps/install-azd-live-sh.yml
108
parameters:
119
Version: $(AzdVersion)
1210

11+
- template: /eng/pipelines/templates/steps/azd-login.yml
12+
1313
# Java pre-requisites that isn't met
1414
# TODO: Use azd container as a form of validation that the container works
1515
- template: /eng/pipelines/templates/steps/install-ms-openjdk.yml
@@ -30,6 +30,12 @@ steps:
3030
Write-Host "##vso[task.setvariable variable=ResourceGroupName]$resourceGroupName"
3131
Write-Host "##vso[task.setvariable variable=EnvPrefixName]$envPrefixName"
3232
33+
$SynchronousCleanup = 'true'
34+
if ('$(Build.Reason)' -eq 'PullRequest') {
35+
$SynchronousCleanup = 'false'
36+
}
37+
Write-Host "##vso[task.setvariable variable=SynchronousCleanup]$SynchronousCleanup"
38+
3339
$CleanupImmediate = 'true'
3440
if ($(CleanupHoursDelay) -ne '0') {
3541
$CleanupImmediate = 'false'
@@ -48,16 +54,19 @@ steps:
4854
targetType: filePath
4955
filePath: templates/tests/test-templates.sh
5056
arguments: >
57+
-f '$(Pipeline.Workspace)'
5158
-e '$(EnvPrefixName)'
5259
-t '$(TemplateName)'
5360
-b '$(TemplateBranchName)'
5461
-s '$(SubscriptionId)'
5562
-u '$(Build.BuildId)'
5663
-l '$(AzureLocation)'
5764
-p 'list'
58-
-c '$(CleanupImmediate)'
65+
-c '$(SynchronousCleanup)'
5966
workingDirectory: templates/tests
6067

68+
- template: /eng/pipelines/templates/steps/az-login.yml
69+
6170
# First tag the resource group (if exists) so that it can get cleaned up
6271
# by the cleanup pipeline. Then attempt to delete the resource group
6372
# directly. If the delete fails the cleanup pipeline will delete it.

eng/scripts/Invoke-RepomanGenerate.ps1

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
param(
22
[string] $TargetBranchName,
33
[string] $RemoteName,
4-
[string] $ResultsFileLocation = "$([System.IO.Path]::GetTempPath())/repoman.md",
4+
[string] $ResultsFileLocation,
55
[string] $RunnerTemp = [System.IO.Path]::GetTempPath(),
66
[switch] $WhatIf
77
)
88

99
$projectsJson = repoman list --format json | Out-String
1010
$projects = ConvertFrom-Json $projectsJson
1111

12-
foreach ($project in $projects) {
13-
$additionalParameters = '--update'
14-
if ($WhatIf) {
15-
$additionalParameters = ''
16-
}
12+
$additionalParameters = @()
13+
if (-not $WhatIf) {
14+
$additionalParameters += '--update'
15+
}
1716

17+
if ($ResultsFileLocation) {
18+
$additionalParameters += "--resultsFile", $ResultsFileLocation
19+
}
20+
21+
foreach ($project in $projects) {
1822
$projectPath = $project.projectPath
1923
$templatePath = $project.templatePath.Replace($projectPath, "")
2024

@@ -27,19 +31,17 @@ repoman generate `
2731
--remote "$RemoteName" `
2832
--https `
2933
--fail-on-update-error `
30-
--resultsFile $ResultsFileLocation `
31-
$additionalParameters
34+
$($additionalParameters -join ([System.Environment]::NewLine + " "))
3235
"@
3336

34-
repoman generate `
37+
& repoman generate `
3538
-s $projectPath `
3639
-o $RunnerTemp `
3740
-t $templatePath `
3841
--branch `"$TargetBranchName`" `
3942
--remote "$RemoteName" `
4043
--https `
4144
--fail-on-update-error `
42-
--resultsFile $ResultsFileLocation `
4345
$additionalParameters
4446

4547
if ($LASTEXITCODE) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<#
2+
.SYNOPSIS
3+
Parses a repoman generate results file, sets a variable that contains changed template repositories
4+
5+
.PARAMETER ResultsFile
6+
Path to a repoman generate results file.
7+
8+
.PARAMETER OutputTemplatesVariable
9+
The output variable to set that contains the list of comma-separated templates
10+
11+
.PARAMETER OutputTemplateBranchVariable
12+
The output variable to set that contains the template branch
13+
14+
#>
15+
param(
16+
[string]$ResultsFile,
17+
[string]$OutputTemplatesVariable,
18+
[string]$OutputTemplateBranchVariable
19+
)
20+
21+
if (-not (Test-Path -PathType Leaf $ResultsFile)) {
22+
Write-Host "No templates were changed."
23+
exit 0
24+
}
25+
26+
$lines = Get-Content $ResultsFile
27+
$templates = @()
28+
29+
foreach ($line in $lines) {
30+
if ($line -match "azd init -t (.+?) -b (.+?)(?:$|\s)") {
31+
$templateName = $Matches[1]
32+
$branchName = $Matches[2]
33+
$templates += $templateName
34+
}
35+
}
36+
37+
if ($templates.Length -eq 0) {
38+
Write-Host "No templates were changed."
39+
exit 0
40+
}
41+
42+
Write-Host "Following templates were changed on $($branchName):"
43+
44+
$templates | Format-List
45+
46+
$templatesCsv = $templates -join ","
47+
Write-Host "##vso[task.setvariable variable=$OutputTemplatesVariable;isOutput=true]$templatesCsv"
48+
Write-Host "##vso[task.setvariable variable=$OutputTemplateBranchVariable;isOutput=true]$branchName"

eng/scripts/Set-TemplateTestMatrixVariable.ps1

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ if ($TemplateList -eq '(azd template list)') {
6767
}
6868

6969
if ($TemplateListFilter -ne '.*') {
70-
} else {
7170
Write-Host "Filtering with TemplateListFilter regex: $TemplateListFilter"
7271

7372
$templateNames = $templateNames -match $TemplateListFilter

0 commit comments

Comments
 (0)