Skip to content

Commit 5669edd

Browse files
authored
[Windows] Cache only the latest version of CodeQL (#8421)
* Windows: Cache only the latest version of CodeQL Previously, we cached two versions since we prioritized hitting the toolcache over landing new releases quicker. However after experimenting with this, we have decided to prioritize getting new releases into customers' hands more quickly. * Break Windows tests down into separate assertions * List contents of bundle after extracting
1 parent 59805f5 commit 5669edd

File tree

4 files changed

+39
-101
lines changed

4 files changed

+39
-101
lines changed

images/win/scripts/Installers/Install-CodeQLBundle.ps1

Lines changed: 22 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,75 +3,34 @@
33
## Desc: Install the CodeQL CLI Bundle to the toolcache.
44
################################################################################
55

6-
# Retrieve the CLI versions and bundle tags of the latest two CodeQL bundles.
6+
# Retrieve the CLI version of the latest CodeQL bundle.
77
$Defaults = (Invoke-RestMethod "https://raw.githubusercontent.com/github/codeql-action/v2/src/defaults.json")
8-
$CodeQLTagName = $Defaults.bundleVersion
9-
$CodeQLCliVersion = $Defaults.cliVersion
10-
$PriorCodeQLTagName = $Defaults.priorBundleVersion
11-
$PriorCodeQLCliVersion = $Defaults.priorCliVersion
8+
$CliVersion = $Defaults.cliVersion
9+
$TagName = "codeql-bundle-v" + $CliVersion
1210

13-
# Compute the toolcache version number for each bundle. This is either `x.y.z` or `x.y.z-YYYYMMDD`.
14-
if ($CodeQLTagName.split("-")[-1].StartsWith("v")) {
15-
# Tag name of the format `codeql-bundle-vx.y.z`, where x.y.z is the CLI version.
16-
# We don't need to include the tag name in the toolcache version number because it's derivable
17-
# from the CLI version.
18-
$CodeQLBundleVersion = $CodeQLCliVersion
19-
} elseif ($CodeQLTagName.split("-")[-1] -match "^\d+$") {
20-
# Tag name of the format `codeql-bundle-YYYYMMDD`.
21-
# We need to include the tag name in the toolcache version number because it can't be derived
22-
# from the CLI version.
23-
$CodeQLBundleVersion = $CodeQLCliVersion + "-" + $CodeQLTagName.split("-")[-1]
24-
} else {
25-
Write-Error "Unrecognised current CodeQL bundle tag name: $CodeQLTagName. Could not compute toolcache version number."
26-
exit 1
27-
}
28-
if ($PriorCodeQLTagName.split("-")[-1].StartsWith("v")) {
29-
# Tag name of the format `codeql-bundle-vx.y.z`, where x.y.z is the CLI version.
30-
# We don't need to include the tag name in the toolcache version number because it's derivable
31-
# from the CLI version.
32-
$PriorCodeQLBundleVersion = $PriorCodeQLCliVersion
33-
} elseif ($PriorCodeQLTagName.split("-")[-1] -match "^\d+$") {
34-
# Tag name of the format `codeql-bundle-YYYYMMDD`.
35-
# We need to include the tag name in the toolcache version number because it can't be derived
36-
# from the CLI version.
37-
$PriorCodeQLBundleVersion = $PriorCodeQLCliVersion + "-" + $PriorCodeQLTagName.split("-")[-1]
38-
} else {
39-
Write-Error "Unrecognised prior CodeQL bundle tag name: $PriorCodeQLTagName. Could not compute toolcache version number."
40-
exit 1
41-
}
11+
Write-Host "Downloading CodeQL bundle $($CliVersion)..."
12+
# Note that this is the all-platforms CodeQL bundle, to support scenarios where customers run
13+
# different operating systems within containers.
14+
$CodeQLBundlePath = Start-DownloadWithRetry -Url "https://github.com/github/codeql-action/releases/download/$($TagName)/codeql-bundle.tar.gz" -Name "codeql-bundle.tar.gz"
15+
$DownloadDirectoryPath = (Get-Item $CodeQLBundlePath).Directory.FullName
4216

43-
$Bundles = @(
44-
[PSCustomObject]@{
45-
TagName=$CodeQLTagName;
46-
BundleVersion=$CodeQLBundleVersion;
47-
},
48-
[PSCustomObject]@{
49-
TagName=$PriorCodeQLTagName;
50-
BundleVersion=$PriorCodeQLBundleVersion;
51-
}
52-
)
17+
$CodeQLToolcachePath = Join-Path $Env:AGENT_TOOLSDIRECTORY -ChildPath "CodeQL" | Join-Path -ChildPath $CliVersion | Join-Path -ChildPath "x64"
18+
New-Item -Path $CodeQLToolcachePath -ItemType Directory -Force | Out-Null
5319

54-
foreach ($Bundle in $Bundles) {
55-
Write-Host "Downloading CodeQL bundle $($Bundle.BundleVersion)..."
56-
$CodeQLBundlePath = Start-DownloadWithRetry -Url "https://github.com/github/codeql-action/releases/download/$($Bundle.TagName)/codeql-bundle.tar.gz" -Name "codeql-bundle.tar.gz"
57-
$DownloadDirectoryPath = (Get-Item $CodeQLBundlePath).Directory.FullName
20+
Write-Host "Unpacking the downloaded CodeQL bundle archive..."
21+
Extract-7Zip -Path $CodeQLBundlePath -DestinationPath $DownloadDirectoryPath
22+
$UnGzipedCodeQLBundlePath = Join-Path $DownloadDirectoryPath "codeql-bundle.tar"
23+
Extract-7Zip -Path $UnGzipedCodeQLBundlePath -DestinationPath $CodeQLToolcachePath
5824

59-
$CodeQLToolcachePath = Join-Path $Env:AGENT_TOOLSDIRECTORY -ChildPath "CodeQL" | Join-Path -ChildPath $Bundle.BundleVersion | Join-Path -ChildPath "x64"
60-
New-Item -Path $CodeQLToolcachePath -ItemType Directory -Force | Out-Null
25+
Write-Host "CodeQL bundle at $($CodeQLToolcachePath) contains the following directories:"
26+
Get-ChildItem -Path $CodeQLToolcachePath -Depth 2
6127

62-
Write-Host "Unpacking the downloaded CodeQL bundle archive..."
63-
Extract-7Zip -Path $CodeQLBundlePath -DestinationPath $DownloadDirectoryPath
64-
$UnGzipedCodeQLBundlePath = Join-Path $DownloadDirectoryPath "codeql-bundle.tar"
65-
Extract-7Zip -Path $UnGzipedCodeQLBundlePath -DestinationPath $CodeQLToolcachePath
28+
# Touch a file to indicate to the CodeQL Action that this bundle shipped with the toolcache. This is
29+
# to support overriding the CodeQL version specified in defaults.json on GitHub Enterprise.
30+
New-Item -ItemType file (Join-Path $CodeQLToolcachePath -ChildPath "pinned-version")
6631

67-
# We only pin the latest version in the toolcache, to support overriding the CodeQL version specified in defaults.json on GitHub Enterprise.
68-
if ($Bundle.BundleVersion -eq $CodeQLBundleVersion) {
69-
New-Item -ItemType file (Join-Path $CodeQLToolcachePath -ChildPath "pinned-version")
70-
}
71-
72-
# Touch a file to indicate to the toolcache that setting up CodeQL is complete.
73-
New-Item -ItemType file "$CodeQLToolcachePath.complete"
74-
}
32+
# Touch a file to indicate to the toolcache that setting up CodeQL is complete.
33+
New-Item -ItemType file "$CodeQLToolcachePath.complete"
7534

7635
# Test that the tools have been extracted successfully.
77-
Invoke-PesterTests -TestFile "Tools" -TestName "CodeQLBundles"
36+
Invoke-PesterTests -TestFile "Tools" -TestName "CodeQL Bundle"

images/win/scripts/SoftwareReport/SoftwareReport.Generator.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ $tools.AddToolVersion("Bazelisk", $(Get-BazeliskVersion))
6969
$tools.AddToolVersion("Bicep", $(Get-BicepVersion))
7070
$tools.AddToolVersion("Cabal", $(Get-CabalVersion))
7171
$tools.AddToolVersion("CMake", $(Get-CMakeVersion))
72-
$tools.AddToolVersion("CodeQL Action Bundles", $(Get-CodeQLBundleVersions))
72+
$tools.AddToolVersion("CodeQL Action Bundle", $(Get-CodeQLBundleVersion))
7373
$tools.AddToolVersion("Docker", $(Get-DockerVersion))
7474
$tools.AddToolVersion("Docker Compose v1", $(Get-DockerComposeVersion))
7575
$tools.AddToolVersion("Docker Compose v2", $(Get-DockerComposeVersionV2))

images/win/scripts/SoftwareReport/SoftwareReport.Tools.psm1

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,12 @@ function Get-CMakeVersion {
4242
return $cmakeVersion
4343
}
4444

45-
function Get-CodeQLBundleVersions {
45+
function Get-CodeQLBundleVersion {
4646
$CodeQLVersionsWildcard = Join-Path $Env:AGENT_TOOLSDIRECTORY -ChildPath "CodeQL" | Join-Path -ChildPath "*"
47-
$CodeQLVersionPaths = Get-ChildItem $CodeQLVersionsWildcard
48-
$CodeQlVersions=@()
49-
foreach ($CodeQLVersionPath in $CodeQLVersionPaths) {
50-
$FullCodeQLVersionPath = $CodeQLVersionPath | Select-Object -Expand FullName
51-
$CodeQLPath = Join-Path $FullCodeQLVersionPath -ChildPath "x64" | Join-Path -ChildPath "codeql" | Join-Path -ChildPath "codeql.exe"
52-
$CodeQLVersion = & $CodeQLPath version --quiet
53-
$CodeQLVersions += $CodeQLVersion
54-
}
55-
return $CodeQLVersions
47+
$CodeQLVersionPath = Get-ChildItem $CodeQLVersionsWildcard | Select-Object -First 1 -Expand FullName
48+
$CodeQLPath = Join-Path $CodeQLVersionPath -ChildPath "x64" | Join-Path -ChildPath "codeql" | Join-Path -ChildPath "codeql.exe"
49+
$CodeQLVersion = & $CodeQLPath version --quiet
50+
return $CodeQLVersion
5651
}
5752

5853
function Get-DockerVersion {

images/win/scripts/Tests/Tools.Tests.ps1

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,25 @@ Describe "Bazel" {
2121
}
2222
}
2323

24-
Describe "CodeQLBundles" {
25-
It "Latest CodeQL Bundle" {
24+
Describe "CodeQL Bundle" {
25+
It "Single distribution installed" {
2626
$CodeQLVersionsWildcard = Join-Path $Env:AGENT_TOOLSDIRECTORY -ChildPath "CodeQL" | Join-Path -ChildPath "*"
27-
$LatestCodeQLVersionPath = Get-ChildItem $CodeQLVersionsWildcard | Sort-Object -Descending | Select-Object -First 1 -Expand FullName
28-
$LatestCodeQLPath = Join-Path $LatestCodeQLVersionPath -ChildPath "x64" | Join-Path -ChildPath "codeql" | Join-Path -ChildPath "codeql.exe"
29-
"$LatestCodeQLPath version --quiet" | Should -ReturnZeroExitCode
30-
31-
$LatestCodeQLPacksPath = Join-Path $LatestCodeQLVersionPath -ChildPath "x64" | Join-Path -ChildPath "codeql" | Join-Path -ChildPath "qlpacks"
32-
$LatestCodeQLPacksPath | Should -Exist
27+
$CodeQLVersionPath = Get-ChildItem $CodeQLVersionsWildcard | Should -HaveCount 1
3328
}
3429

35-
It "Prior CodeQL Bundle" {
30+
It "Contains CodeQL executable" {
3631
$CodeQLVersionsWildcard = Join-Path $Env:AGENT_TOOLSDIRECTORY -ChildPath "CodeQL" | Join-Path -ChildPath "*"
37-
$PriorCodeQLVersionPath = Get-ChildItem $CodeQLVersionsWildcard | Sort-Object -Descending | Select-Object -Last 1 -Expand FullName
38-
$PriorCodeQLPath = Join-Path $PriorCodeQLVersionPath -ChildPath "x64" | Join-Path -ChildPath "codeql" | Join-Path -ChildPath "codeql.exe"
39-
"$PriorCodeQLPath version --quiet" | Should -ReturnZeroExitCode
40-
41-
$PriorCodeQLPacksPath = Join-Path $PriorCodeQLVersionPath -ChildPath "x64" | Join-Path -ChildPath "codeql" | Join-Path -ChildPath "qlpacks"
42-
$PriorCodeQLPacksPath | Should -Exist
32+
$CodeQLVersionPath = Get-ChildItem $CodeQLVersionsWildcard | Sort-Object -Descending | Select-Object -First 1 -Expand FullName
33+
$CodeQLPath = Join-Path $CodeQLVersionPath -ChildPath "x64" | Join-Path -ChildPath "codeql" | Join-Path -ChildPath "codeql.exe"
34+
"$CodeQLPath version --quiet" | Should -ReturnZeroExitCode
4335
}
4436

45-
It "Latest and Prior CodeQL Bundles are unique" {
37+
It "Contains CodeQL packs" {
4638
$CodeQLVersionsWildcard = Join-Path $Env:AGENT_TOOLSDIRECTORY -ChildPath "CodeQL" | Join-Path -ChildPath "*"
47-
48-
$LatestCodeQLVersionPath = Get-ChildItem $CodeQLVersionsWildcard | Sort-Object -Descending | Select-Object -First 1 -Expand FullName
49-
$LatestCodeQLPath = Join-Path $LatestCodeQLVersionPath -ChildPath "x64" | Join-Path -ChildPath "codeql" | Join-Path -ChildPath "codeql.exe"
50-
$LatestCodeQLVersion = & $LatestCodeQLPath version --quiet
51-
52-
$PriorCodeQLVersionPath = Get-ChildItem $CodeQLVersionsWildcard | Sort-Object -Descending | Select-Object -Last 1 -Expand FullName
53-
$PriorCodeQLPath = Join-Path $PriorCodeQLVersionPath -ChildPath "x64" | Join-Path -ChildPath "codeql" | Join-Path -ChildPath "codeql.exe"
54-
$PriorCodeQLVersion = & $PriorCodeQLPath version --quiet
55-
56-
$LatestCodeQLVersion | Should -Not -Match $PriorCodeQLVersion
39+
$CodeQLVersionPath = Get-ChildItem $CodeQLVersionsWildcard | Sort-Object -Descending | Select-Object -First 1 -Expand FullName
40+
$CodeQLPacksPath = Join-Path $CodeQLVersionPath -ChildPath "x64" | Join-Path -ChildPath "codeql" | Join-Path -ChildPath "qlpacks"
41+
$CodeQLPacksPath | Should -Exist
5742
}
58-
5943
}
6044

6145
Describe "R" {

0 commit comments

Comments
 (0)