Skip to content

Commit 1aa1c5b

Browse files
authored
Install java via native tools in CI (#54421)
* Install java via native tools in CI The VMR doesn't have Java installed globally on the machines, but it does have it in the native tool cache. This PR updates the build scripts to use the native tool cache to install Java in CI builds. I've also updated to 11.0.22 (vs. the .3 version from 2019) and the script used to pull this from native tool assets.
1 parent 40070ae commit 1aa1c5b

File tree

8 files changed

+93
-75
lines changed

8 files changed

+93
-75
lines changed

.azure/pipelines/ci.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ stages:
200200
steps:
201201
- script: ./eng/build.cmd
202202
-ci
203-
-prepareMachine
203+
-prepareMchine
204+
-nativeToolsOnMachine
204205
-arch x64
205206
-all
206207
$(_BuildArgs)
@@ -252,6 +253,7 @@ stages:
252253
- script: ./eng/build.cmd
253254
-ci
254255
-prepareMachine
256+
-nativeToolsOnMachine
255257
-arch x64
256258
-pack
257259
-all
@@ -737,11 +739,11 @@ stages:
737739
timeoutInMinutes: 240
738740
steps:
739741
# Build the shared framework
740-
- script: ./eng/build.cmd -ci -prepareMachine -nobl -all -pack -arch x64
742+
- script: ./eng/build.cmd -ci -prepareMachine -nativeToolsOnMachine -nobl -all -pack -arch x64
741743
/p:CrossgenOutput=false /p:ASPNETCORE_TEST_LOG_DIR=artifacts/log $(_InternalRuntimeDownloadArgs)
742744
displayName: Build shared fx
743745
# -noBuildRepoTasks -noBuildNative -noBuild to avoid repeating work done in the previous step.
744-
- script: ./eng/build.cmd -ci -prepareMachine -nobl -all -noBuildRepoTasks -noBuildNative -noBuild -test
746+
- script: ./eng/build.cmd -ci -prepareMachine -nativeToolsOnMachine -nobl -all -noBuildRepoTasks -noBuildNative -noBuild -test
745747
-projects eng\helix\helix.proj /p:IsHelixPRCheck=true /p:IsHelixJob=true
746748
/p:CrossgenOutput=false /p:ASPNETCORE_TEST_LOG_DIR=artifacts/log $(_InternalRuntimeDownloadArgs)
747749
displayName: Run build.cmd helix target

.azure/pipelines/jobs/codesign-xplat.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ jobs:
1313
jobDisplayName: "Code-sign ${{ parameters.inputName }} packages"
1414
agentOs: Windows
1515
installNodeJs: false
16-
installJdk: false
1716
steps:
1817
- task: DownloadBuildArtifacts@0
1918
displayName: Download ${{ parameters.inputName }} artifacts

.azure/pipelines/jobs/default-build.yml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ parameters:
6868
container: ''
6969
enableRichCodeNavigation: ''
7070
installNodeJs: true
71-
installJdk: true # Ignored unless agentOs == Windows.
7271
timeoutInMinutes: 180
7372
testRunTitle: $(AgentOsName)-$(BuildConfiguration)
7473
useHostedUbuntu: true
@@ -147,8 +146,6 @@ jobs:
147146
- LC_ALL: 'en_US.UTF-8'
148147
- LANG: 'en_US.UTF-8'
149148
- LANGUAGE: 'en_US.UTF-8'
150-
- ${{ if and(eq(parameters.installJdk, 'true'), eq(parameters.agentOs, 'Windows')) }}:
151-
- JAVA_HOME: $(Agent.BuildDirectory)\.tools\jdk\win-x64
152149
- ${{ if or(ne(parameters.codeSign, true), ne(variables['System.TeamProject'], 'internal')) }}:
153150
- _SignType: ''
154151
- ${{ if and(eq(parameters.codeSign, true), eq(variables['System.TeamProject'], 'internal')) }}:
@@ -202,14 +199,11 @@ jobs:
202199
displayName: Install Node 20.x
203200
inputs:
204201
versionSpec: 20.x
205-
- ${{ if and(eq(parameters.installJdk, 'true'), eq(parameters.agentOs, 'Windows')) }}:
206-
- powershell: ./eng/scripts/InstallJdk.ps1
207-
displayName: Install JDK 11
208-
- ${{ if eq(parameters.isAzDOTestingJob, true) }}:
209-
- powershell: |
210-
Write-Host "##vso[task.setvariable variable=SeleniumProcessTrackingFolder]$(Build.SourcesDirectory)\artifacts\tmp\selenium\"
211-
./eng/scripts/InstallGoogleChrome.ps1
212-
displayName: Install Chrome
202+
- ${{ if and(eq(parameters.agentOs, 'Windows'), eq(parameters.isAzDOTestingJob, true)) }}:
203+
- powershell: |
204+
Write-Host "##vso[task.setvariable variable=SeleniumProcessTrackingFolder]$(Build.SourcesDirectory)\artifacts\tmp\selenium\"
205+
./eng/scripts/InstallGoogleChrome.ps1
206+
displayName: Install Chrome
213207
- ${{ if eq(parameters.agentOs, 'Windows') }}:
214208
- powershell: Write-Host "##vso[task.prependpath]$(DOTNET_CLI_HOME)\.dotnet\tools"
215209
displayName: Add dotnet tools to path
@@ -256,7 +250,7 @@ jobs:
256250
${{ step.env }}
257251
- ${{ if eq(parameters.steps, '')}}:
258252
- ${{ if eq(parameters.agentOs, 'Windows') }}:
259-
- script: $(BuildDirectory)\build.cmd -ci -prepareMachine -nobl -Configuration $(BuildConfiguration) $(BuildScriptArgs)
253+
- script: $(BuildDirectory)\build.cmd -ci -prepareMachine -nativeToolsOnMachine -nobl -Configuration $(BuildConfiguration) $(BuildScriptArgs)
260254
/p:DotNetSignType=$(_SignType)
261255
displayName: Run build.cmd
262256
env:

eng/build.ps1

Lines changed: 70 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Sets up CI specific settings and variables.
1515
.PARAMETER PrepareMachine
1616
In CI, Turns on machine preparation/clean up code that changes the machine state (e.g. kills build processes).
1717
18+
.PARAMETER NativeToolsOnMachine
19+
Turns on native tooling handling. On CI machines, promotes native tools listed in global.json to the path.
20+
1821
.PARAMETER Restore
1922
Run restore.
2023
@@ -128,6 +131,7 @@ Online version: https://github.com/dotnet/aspnetcore/blob/main/docs/BuildFromSou
128131
param(
129132
[switch]$CI,
130133
[switch]$PrepareMachine,
134+
[switch]$NativeToolsOnMachine,
131135

132136
# Build lifecycle options
133137
[switch]$Restore,
@@ -312,58 +316,6 @@ $performDesktopBuild = ($BuildInstallers -and $Architecture -ne "arm") -or `
312316
$performDotnetBuild = $BuildJava -or $BuildManaged -or $BuildNodeJS -or `
313317
($All -and -not ($NoBuildJava -and $NoBuildManaged -and $NoBuildNodeJS)) -or `
314318
($Projects -and -not ($BuildInstallers -or $specifiedBuildNative))
315-
$foundJdk = $false
316-
$javac = Get-Command javac -ErrorAction Ignore -CommandType Application
317-
$localJdkPath = "$PSScriptRoot\..\.tools\jdk\win-x64\"
318-
if (Test-Path "$localJdkPath\bin\javac.exe") {
319-
$foundJdk = $true
320-
Write-Host -f Magenta "Detected JDK in $localJdkPath (via local repo convention)"
321-
$env:JAVA_HOME = $localJdkPath
322-
}
323-
elseif ($env:JAVA_HOME) {
324-
if (-not (Test-Path "${env:JAVA_HOME}\bin\javac.exe")) {
325-
Write-Error "The environment variable JAVA_HOME was set, but ${env:JAVA_HOME}\bin\javac.exe does not exist. Remove JAVA_HOME or update it to the correct location for the JDK. See https://www.bing.com/search?q=java_home for details."
326-
}
327-
else {
328-
Write-Host -f Magenta "Detected JDK in ${env:JAVA_HOME} (via JAVA_HOME)"
329-
$foundJdk = $true
330-
}
331-
}
332-
elseif ($javac) {
333-
$foundJdk = $true
334-
$javaHome = Split-Path -Parent (Split-Path -Parent $javac.Path)
335-
$env:JAVA_HOME = $javaHome
336-
Write-Host -f Magenta "Detected JDK in $javaHome (via PATH)"
337-
}
338-
else {
339-
try {
340-
$jdkRegistryKeys = @(
341-
"HKLM:\SOFTWARE\JavaSoft\JDK", # for JDK 10+
342-
"HKLM:\SOFTWARE\JavaSoft\Java Development Kit" # fallback for JDK 8
343-
)
344-
$jdkRegistryKey = $jdkRegistryKeys | Where-Object { Test-Path $_ } | Select-Object -First 1
345-
if ($jdkRegistryKey) {
346-
$jdkVersion = (Get-Item $jdkRegistryKey | Get-ItemProperty -name CurrentVersion).CurrentVersion
347-
$javaHome = (Get-Item $jdkRegistryKey\$jdkVersion | Get-ItemProperty -Name JavaHome).JavaHome
348-
if (Test-Path "${javaHome}\bin\javac.exe") {
349-
$env:JAVA_HOME = $javaHome
350-
Write-Host -f Magenta "Detected JDK $jdkVersion in $env:JAVA_HOME (via registry)"
351-
$foundJdk = $true
352-
}
353-
}
354-
}
355-
catch {
356-
Write-Verbose "Failed to detect Java: $_"
357-
}
358-
}
359-
360-
if ($env:PATH -notlike "*${env:JAVA_HOME}*") {
361-
$env:PATH = "$(Join-Path $env:JAVA_HOME bin);${env:PATH}"
362-
}
363-
364-
if (-not $foundJdk -and $RunBuild -and ($All -or $BuildJava) -and -not $NoBuildJava) {
365-
Write-Error "Could not find the JDK. Either run $PSScriptRoot\scripts\InstallJdk.ps1 to install for this repo, or install the JDK globally on your machine (see $PSScriptRoot\..\docs\BuildFromSource.md for details)."
366-
}
367319

368320
# Initialize global variables need to be set before the import of Arcade is imported
369321
$restore = $RunRestore
@@ -392,6 +344,61 @@ Remove-Item variable:global:_MSBuildExe -ea Ignore
392344
# Import Arcade
393345
. "$PSScriptRoot/common/tools.ps1"
394346

347+
function LocateJava {
348+
$foundJdk = $false
349+
$javac = Get-Command javac -ErrorAction Ignore -CommandType Application
350+
$localJdkPath = "$PSScriptRoot\..\.tools\jdk\win-x64\"
351+
if (Test-Path "$localJdkPath\bin\javac.exe") {
352+
$foundJdk = $true
353+
Write-Host -f Magenta "Detected JDK in $localJdkPath (via local repo convention)"
354+
$env:JAVA_HOME = $localJdkPath
355+
}
356+
elseif ($env:JAVA_HOME) {
357+
if (-not (Test-Path "${env:JAVA_HOME}\bin\javac.exe")) {
358+
Write-Error "The environment variable JAVA_HOME was set, but ${env:JAVA_HOME}\bin\javac.exe does not exist. Remove JAVA_HOME or update it to the correct location for the JDK. See https://www.bing.com/search?q=java_home for details."
359+
}
360+
else {
361+
Write-Host -f Magenta "Detected JDK in ${env:JAVA_HOME} (via JAVA_HOME)"
362+
$foundJdk = $true
363+
}
364+
}
365+
elseif ($javac) {
366+
$foundJdk = $true
367+
$javaHome = Split-Path -Parent (Split-Path -Parent $javac.Path)
368+
$env:JAVA_HOME = $javaHome
369+
Write-Host -f Magenta "Detected JDK in $javaHome (via PATH)"
370+
}
371+
else {
372+
try {
373+
$jdkRegistryKeys = @(
374+
"HKLM:\SOFTWARE\JavaSoft\JDK", # for JDK 10+
375+
"HKLM:\SOFTWARE\JavaSoft\Java Development Kit" # fallback for JDK 8
376+
)
377+
$jdkRegistryKey = $jdkRegistryKeys | Where-Object { Test-Path $_ } | Select-Object -First 1
378+
if ($jdkRegistryKey) {
379+
$jdkVersion = (Get-Item $jdkRegistryKey | Get-ItemProperty -name CurrentVersion).CurrentVersion
380+
$javaHome = (Get-Item $jdkRegistryKey\$jdkVersion | Get-ItemProperty -Name JavaHome).JavaHome
381+
if (Test-Path "${javaHome}\bin\javac.exe") {
382+
$env:JAVA_HOME = $javaHome
383+
Write-Host -f Magenta "Detected JDK $jdkVersion in $env:JAVA_HOME (via registry)"
384+
$foundJdk = $true
385+
}
386+
}
387+
}
388+
catch {
389+
Write-Verbose "Failed to detect Java: $_"
390+
}
391+
}
392+
393+
if ($env:PATH -notlike "*${env:JAVA_HOME}*") {
394+
$env:PATH = "$(Join-Path $env:JAVA_HOME bin);${env:PATH}"
395+
}
396+
397+
if (-not $foundJdk -and $RunBuild -and ($All -or $BuildJava) -and -not $NoBuildJava) {
398+
Write-Error "Could not find the JDK. Either run $PSScriptRoot\scripts\InstallJdk.ps1 to install for this repo, or install the JDK globally on your machine (see $PSScriptRoot\..\docs\BuildFromSource.md for details)."
399+
}
400+
}
401+
395402
# Add default .binlog location if not already on the command line. tools.ps1 does not handle this; it just checks
396403
# $BinaryLog, $CI and $ExcludeCIBinarylog values for an error case. But tools.ps1 provides a nice function to help.
397404
if ($BinaryLog) {
@@ -424,6 +431,17 @@ try {
424431
$tmpRestore = $restore
425432
$restore = $true
426433

434+
# Initialize the native tools before locating java.
435+
if ($NativeToolsOnMachine) {
436+
$env:NativeToolsOnMachine=$true
437+
# Do not promote native tools except in cases where -NativeToolsOnMachine is passed.
438+
# Currently the JDK is laid out in an incorrect pattern: https://github.com/dotnet/dnceng/issues/2185
439+
InitializeNativeTools
440+
}
441+
442+
# Locate java, now that we may have java available after initializing native tools.
443+
LocateJava
444+
427445
$toolsetBuildProj = InitializeToolset
428446

429447
$restore = $tmpRestore

eng/common/tools.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ function InstallDotNet([string] $dotnetRoot,
296296
if ($runtime -eq "aspnetcore") { $runtimePath = $runtimePath + "\Microsoft.AspNetCore.App" }
297297
if ($runtime -eq "windowsdesktop") { $runtimePath = $runtimePath + "\Microsoft.WindowsDesktop.App" }
298298
$runtimePath = $runtimePath + "\" + $version
299-
299+
300300
$dotnetVersionLabel = "runtime toolset '$runtime/$architecture v$version'"
301301

302302
if (Test-Path $runtimePath) {
@@ -608,7 +608,7 @@ function InitializeBuildTool() {
608608
} else {
609609
$initializeBuildToolFramework=$env:_OverrideArcadeInitializeBuildToolFramework
610610
}
611-
611+
612612
$buildTool = @{ Path = $dotnetPath; Command = 'msbuild'; Tool = 'dotnet'; Framework = $initializeBuildToolFramework }
613613
} elseif ($msbuildEngine -eq "vs") {
614614
try {

eng/configure-toolset.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
if [ "${DotNetBuildFromSource:-false}" = false ]; then
66
use_installed_dotnet_cli="false"
77
fi
8+
9+
# Working around issue https://github.com/dotnet/arcade/issues/2673
10+
DisableNativeToolsetInstalls=true

eng/scripts/InstallJdk.ps1

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ $javacExe = "$installDir\bin\javac.exe"
2323
$tempDir = "$repoRoot\obj"
2424
if (-not $JdkVersion) {
2525
$globalJson = Get-Content "$repoRoot\global.json" | ConvertFrom-Json
26-
$JdkVersion = $globalJson.tools.jdk
26+
$JdkVersion = $globalJson.'native-tools'.jdk
2727
}
2828

2929
if (Test-Path $javacExe) {
@@ -40,12 +40,13 @@ Remove-Item -Force -Recurse $tempDir -ErrorAction Ignore | out-null
4040
mkdir $tempDir -ea Ignore | out-null
4141
mkdir $installDir -ea Ignore | out-null
4242
Write-Host "Starting download of JDK ${JdkVersion}"
43-
& $PSScriptRoot\Download.ps1 "https://netcorenativeassets.blob.core.windows.net/resource-packages/external/windows/java/jdk-${JdkVersion}_windows-x64_bin.zip" "$tempDir/jdk.zip"
43+
& $PSScriptRoot\Download.ps1 "https://netcorenativeassets.blob.core.windows.net/resource-packages/external/windows/java/microsoft-jdk-${JdkVersion}-windows-x64.zip" "$tempDir/jdk.zip"
4444
Write-Host "Done downloading JDK ${JdkVersion}"
4545
Expand-Archive "$tempDir/jdk.zip" -d "$tempDir/jdk/"
4646
Write-Host "Expanded JDK to $tempDir"
4747
Write-Host "Installing JDK to $installDir"
48-
Move-Item "$tempDir/jdk/jdk-${JdkVersion}/*" $installDir
48+
# The name of the file directory within the zip is based on the version, but may contain a +N for build number.
49+
Move-Item "$(Get-ChildItem -Path "$tempDir/jdk" | Select-Object -First 1)/*" $installDir
4950
Write-Host "Done installing JDK to $installDir"
5051

5152
if ($env:TF_BUILD) {

global.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
"$(MicrosoftNETCoreBrowserDebugHostTransportVersion)"
1313
]
1414
},
15-
"Git": "2.22.0",
16-
"jdk": "11.0.3",
1715
"vs": {
1816
"version": "17.2",
1917
"components": [
@@ -25,6 +23,9 @@
2523
},
2624
"xcopy-msbuild": "17.1.0"
2725
},
26+
"native-tools": {
27+
"jdk": "11.0.22"
28+
},
2829
"msbuild-sdks": {
2930
"Microsoft.DotNet.Arcade.Sdk": "9.0.0-beta.24159.1",
3031
"Microsoft.DotNet.Helix.Sdk": "9.0.0-beta.24159.1"

0 commit comments

Comments
 (0)