Skip to content

Commit ddc249f

Browse files
Add e2e test for agent release (#4882)
* Use dry-run option in createAdoPrs script * Add CI trigger to release pipeline to be able to test it * Temporarily comment out some steps * Test change * Test change * Fix issue with git commands and releaseId variable * Change test agent version * Disable signing for tests * Fix branch cleanup step * Add test git status * Comment out publish steps. Change test version schema * Fix issue with comment block * Change conditions * Change container name * Change container name assignment * Change condition * Testing * Testing change * Fix * Test * Test * Fix * Fix * Add variable sign * Fix * Enable sign parameter
1 parent ce96634 commit ddc249f

File tree

2 files changed

+95
-16
lines changed

2 files changed

+95
-16
lines changed

.vsts.release.yml

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# This Yaml Document has been converted by ESAI Yaml Pipeline Conversion Tool.
22

3+
trigger:
4+
paths:
5+
include:
6+
- release
7+
- .azure-pipelines
8+
- .vsts.release.yml
9+
310
schedules:
411
- cron: '0 6 * * 2'
512
displayName: Scheduled weekly run
@@ -88,11 +95,15 @@ extends:
8895
Write-Host "isRelease = $isRelease"
8996
Write-Host "##vso[task.setVariable variable=isRelease;isOutput=true]$isRelease"
9097
98+
$isTestRun = ($buildReason -eq 'IndividualCI' -or $buildReason -eq 'PullRequest')
99+
Write-Host "isTestRun = $isTestRun"
100+
Write-Host "##vso[task.setVariable variable=isTestRun;isOutput=true]$isTestRun"
101+
91102
$isScheduledRelease = $isRelease -and $buildReason -eq 'Schedule'
92103
Write-Host "isScheduledRelease = $isScheduledRelease"
93104
94-
if ($isRelease) {
95-
if ($isScheduledRelease) {
105+
if ($isRelease -or $isTestRun) {
106+
if ($isScheduledRelease -or $isTestRun) {
96107
$majorAndMinorVersion = "3.$($currentSprint.sprint)"
97108
$patchVersion = 0
98109
## Looking for a free patch version
@@ -116,6 +127,9 @@ extends:
116127
Write-Error "Version parameter is required for manual release." -ErrorAction Stop
117128
}
118129
}
130+
if ($isTestRun) {
131+
$agentVersion = '3.000.999'
132+
}
119133
Write-Host "agentVersion = $agentVersion"
120134
Write-Host "##vso[task.setVariable variable=agentVersion;isOutput=true]$agentVersion"
121135
@@ -137,10 +151,12 @@ extends:
137151
################################################################################
138152
displayName: Create Release Branch
139153
variables:
154+
IsTestRun: $[ stageDependencies.Verify_release.Set_variables.outputs['SetReleaseVariables.isTestRun'] ]
140155
IsRelease: $[ stageDependencies.Verify_release.Set_variables.outputs['SetReleaseVariables.isRelease'] ]
141156
ReleaseBranch: $[ stageDependencies.Verify_release.Set_variables.outputs['SetReleaseVariables.releaseBranch'] ]
142157
AgentVersion: $[ stageDependencies.Verify_release.Set_variables.outputs['SetReleaseVariables.agentVersion'] ]
143-
condition: and(succeeded(), eq(variables.IsRelease, 'True'))
158+
condition: and(succeeded(), or(eq(variables.IsRelease, 'True'), eq(variables.IsTestRun, 'True')))
159+
144160
pool:
145161
name: 1ES-ABTT-Shared-Pool
146162
image: abtt-ubuntu-2204
@@ -178,10 +194,11 @@ extends:
178194
name: 1ES-Shared-Hosted-Pool_Windows-Server-2022
179195
demands: AzurePS
180196
variables:
197+
IsTestRun: $[ stageDependencies.Verify_release.Set_variables.outputs['SetReleaseVariables.isTestRun'] ]
181198
IsRelease: $[ stageDependencies.Verify_release.Set_variables.outputs['SetReleaseVariables.isRelease'] ]
182199
ReleaseBranch: $[ stageDependencies.Verify_release.Set_variables.outputs['SetReleaseVariables.releaseBranch'] ]
183200
AgentVersion: $[ stageDependencies.Verify_release.Set_variables.outputs['SetReleaseVariables.agentVersion'] ]
184-
condition: and(succeeded(), eq(variables.IsRelease, 'True'))
201+
condition: and(succeeded(), or(eq(variables.IsRelease, 'True'), eq(variables.IsTestRun, 'True')))
185202
steps:
186203

187204
# Clean
@@ -215,20 +232,46 @@ extends:
215232
Select-AzSubscription -SubscriptionId $(SubscriptionId)
216233
$storageContext = New-AzStorageContext -StorageAccountName vstsagentpackage -UseConnectedAccount
217234
$versionDir = "$(AgentVersion)"
235+
236+
$container = "agent"
237+
$isTestContainer = "$(IsTestRun)"
238+
Write-Host "isTestContainer = $isTestContainer"
239+
240+
if ($isTestContainer -eq "True") {
241+
$container = "testagent"
242+
New-AzStorageContainer -Context $storageContext -Name $container -Permission Off
243+
}
244+
Write-Host "container = $container"
245+
218246
Get-ChildItem -LiteralPath "$(System.ArtifactsDirectory)/agent" | ForEach-Object {
219247
$target=$_
220248
Get-ChildItem -LiteralPath "$(System.ArtifactsDirectory)/agent/$target" -Include "*.zip","*.tar.gz" | ForEach-Object {
221249
$executable = $_
222-
Write-Host "Uploading $executable to BlobStorage vstsagentpackage/agent/$versionDir"
223-
Set-AzStorageBlobContent -Context $storageContext -Container agent -File "$(System.ArtifactsDirectory)/agent/$target/$executable" -Blob "$versionDir/$executable" -Force
224-
$uploadFiles.Add("/agent/$versionDir/$executable")
250+
Write-Host "Uploading $executable to BlobStorage vstsagentpackage/$container/$versionDir"
251+
Set-AzStorageBlobContent -Context $storageContext -Container $container -File "$(System.ArtifactsDirectory)/agent/$target/$executable" -Blob "$versionDir/$executable" -Force
252+
$uploadFiles.Add("/$container/$versionDir/$executable")
225253
}
226254
}
227255
Write-Host "Purge Azure CDN Cache"
228256
Clear-AzCdnEndpointContent -EndpointName vstsagentpackage -ProfileName vstsagentpackage -ResourceGroupName vstsagentpackage -ContentPath $uploadFiles
229257
Write-Host "Force Refresh Azure CDN Cache"
230258
Import-AzCdnEndpointContent -EndpointName vstsagentpackage -ProfileName vstsagentpackage -ResourceGroupName vstsagentpackage -ContentPath $uploadFiles
231259
260+
# Clean up blob container with test agent version
261+
- task: AzurePowerShell@5
262+
displayName: Delete Azure Blob container with test agent version
263+
condition: and(succeeded(), eq(variables.IsTestRun, 'True'))
264+
inputs:
265+
azurePowerShellVersion: 'LatestVersion'
266+
azureSubscription: 'azure-pipelines-agent-vstsagentpackage-oauth'
267+
scriptType: 'InlineScript'
268+
inline: |
269+
Import-Module Azure, Az.Accounts, Az.Storage -ErrorAction Ignore -PassThru
270+
Select-AzSubscription -SubscriptionId $(SubscriptionId)
271+
$storageContext = New-AzStorageContext -StorageAccountName vstsagentpackage -UseConnectedAccount
272+
$container = 'testagent'
273+
Remove-AzStorageContainer -Name $container -Context $storageContext -Force
274+
232275
# Download all agent hashes created in previous phases
233276
- task: DownloadBuildArtifacts@0
234277
displayName: Download Agent Hashes
@@ -267,6 +310,7 @@ extends:
267310
$releaseCreated = Invoke-RestMethod @releaseParams
268311
Write-Host $releaseCreated
269312
$releaseId = $releaseCreated.id
313+
Write-Host "##vso[task.setVariable variable=releaseId;isoutput=true]$releaseId"
270314
$assets = [System.IO.File]::ReadAllText("$(Build.SourcesDirectory)\assets.json").Replace("<AGENT_VERSION>","$(AgentVersion)")
271315
$assetsParams = @{
272316
Uri = "https://uploads.github.com/repos/Microsoft/azure-pipelines-agent/releases/$releaseId/assets?name=assets.json"
@@ -279,6 +323,36 @@ extends:
279323
}
280324
Invoke-RestMethod @assetsParams
281325
displayName: Create agent release on Github
326+
name: create_github_release
327+
328+
# Delete test agent release
329+
- powershell: |
330+
Write-Host "Deleting test github release."
331+
$releaseId = $(create_github_release.releaseId)
332+
333+
$releaseParams = @{
334+
Uri = "https://api.github.com/repos/Microsoft/azure-pipelines-agent/releases/$releaseId";
335+
Method = 'DELETE';
336+
Headers = @{
337+
Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("vsts:$(GithubToken)"));
338+
}
339+
ContentType = 'application/json';
340+
}
341+
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
342+
$releaseDeleted = Invoke-RestMethod @releaseParams
343+
Write-Host $releaseDeleted.Id
344+
displayName: Delete test agent release from Github
345+
condition: and(succeeded(), eq(variables.IsTestRun, 'True'))
346+
347+
# Clean up test release branch
348+
- powershell: |
349+
git config --global user.email "[email protected]"
350+
git config --global user.name "azure-pipelines-bot"
351+
git status
352+
git -c credential.helper='!f() { echo "username=pat"; echo "password=$(GithubToken)"; };f' push origin --delete $(ReleaseBranch)
353+
git push --delete origin v$(AgentVersion)
354+
displayName: Clean up test release branch
355+
condition: and(succeeded(), eq(variables.IsTestRun, 'True'))
282356
283357
- stage: CreatePRs
284358
dependsOn:
@@ -295,10 +369,11 @@ extends:
295369
################################################################################
296370
displayName: Create PRs in AzureDevOps and ConfigChange
297371
variables:
372+
IsTestRun: $[ stageDependencies.Verify_release.Set_variables.outputs['SetReleaseVariables.isTestRun'] ]
298373
IsRelease: $[ stageDependencies.Verify_release.Set_variables.outputs['SetReleaseVariables.isRelease'] ]
299374
ReleaseBranch: $[ stageDependencies.Verify_release.Set_variables.outputs['SetReleaseVariables.releaseBranch'] ]
300375
AgentVersion: $[ stageDependencies.Verify_release.Set_variables.outputs['SetReleaseVariables.agentVersion'] ]
301-
condition: and(succeeded(), eq(variables.IsRelease, 'True'))
376+
condition: and(succeeded(), or(eq(variables.IsRelease, 'True'), eq(variables.IsTestRun, 'True')))
302377
steps:
303378
- checkout: self
304379

@@ -320,7 +395,7 @@ extends:
320395
cd release
321396
npm install
322397
ls
323-
node createAdoPrs.js $(AgentVersion)
398+
node createAdoPrs.js $(AgentVersion) --dryrun=$(IsTestRun)
324399
name: s_CreateAdoPrs
325400
displayName: Create PRs in AzureDevOps and ConfigChange
326401
env:
@@ -341,6 +416,7 @@ extends:
341416
- job: j_SendPRsNotifications
342417
displayName: Send Release PRs notifications
343418
variables:
419+
IsTestRun: $[ stageDependencies.Verify_release.Set_variables.outputs['SetReleaseVariables.isTestRun'] ]
344420
IsRelease: $[ stageDependencies.Verify_release.Set_variables.outputs['SetReleaseVariables.isRelease'] ]
345421
AdoPrId: $[ stageDependencies.CreatePRs.create_ado_prs.outputs['s_CreateAdoPrs.AdoPrId'] ]
346422
AdoPrLink: $[ stageDependencies.CreatePRs.create_ado_prs.outputs['s_CreateAdoPrs.AdoPrLink'] ]
@@ -350,6 +426,7 @@ extends:
350426
and(
351427
not(${{ parameters.disableNotifications }}),
352428
eq(variables.IsRelease, 'True'),
429+
eq(variables.IsTestRun, 'False'),
353430
not(${{ parameters.onlyGitHubRelease }})
354431
)
355432
steps:

release/createAdoPrs.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ async function openPR(repo, project, sourceBranch, targetBranch, commitMessage,
8585

8686
if (!fs.existsSync(repoPath)) {
8787
const gitUrl = `https://${process.env.PAT}@${orgUrl}/${project}/_git/${repo}`;
88-
util.execInForeground(`${GIT} clone --depth 1 ${gitUrl} ${repoPath}`, null, opt.dryrun);
88+
util.execInForeground(`${GIT} clone --depth 1 ${gitUrl} ${repoPath}`, null, opt.options.dryrun);
8989
}
9090

9191
for (const targetToCommit of targetsToCommit) {
@@ -105,12 +105,12 @@ async function openPR(repo, project, sourceBranch, targetBranch, commitMessage,
105105
}
106106

107107
for (const targetToCommit of targetsToCommit) {
108-
util.execInForeground(`${GIT} add ${targetToCommit}`, repoPath, opt.dryrun);
108+
util.execInForeground(`${GIT} add ${targetToCommit}`, repoPath, opt.options.dryrun);
109109
}
110110

111-
util.execInForeground(`${GIT} checkout -b ${sourceBranch}`, repoPath);
112-
util.execInForeground(`${GIT} commit -m "${commitMessage}"`, repoPath);
113-
util.execInForeground(`${GIT} push --force origin ${sourceBranch}`, repoPath);
111+
util.execInForeground(`${GIT} checkout -b ${sourceBranch}`, repoPath, opt.options.dryrun);
112+
util.execInForeground(`${GIT} commit -m "${commitMessage}"`, repoPath, opt.options.dryrun);
113+
util.execInForeground(`${GIT} push --force origin ${sourceBranch}`, repoPath, opt.options.dryrun);
114114

115115
const prefix = 'refs/heads/';
116116

@@ -129,6 +129,8 @@ async function openPR(repo, project, sourceBranch, targetBranch, commitMessage,
129129

130130
if (PR) {
131131
console.log('PR already exists');
132+
} else if (opt.options.dryrun) {
133+
return [-1, 'test']; // return without creating PR for test runs
132134
} else {
133135
console.log('PR does not exist; creating PR');
134136
PR = await gitApi.createPullRequest(pullRequest, repo, project);
@@ -167,8 +169,8 @@ async function main() {
167169
util.verifyMinimumNodeVersion();
168170
util.verifyMinimumGitVersion();
169171
createIntegrationFiles(agentVersion);
170-
util.execInForeground(`${GIT} config --global user.email "${process.env.USEREMAIL}"`, null, opt.dryrun);
171-
util.execInForeground(`${GIT} config --global user.name "${process.env.USERNAME}"`, null, opt.dryrun);
172+
util.execInForeground(`${GIT} config --global user.email "${process.env.USEREMAIL}"`, null, opt.options.dryrun);
173+
util.execInForeground(`${GIT} config --global user.name "${process.env.USERNAME}"`, null, opt.options.dryrun);
172174

173175
const sprint = await getCurrentSprint();
174176

0 commit comments

Comments
 (0)