Skip to content

Commit

Permalink
[ci] Add template for setting JAVA_HOME vars (#9671)
Browse files Browse the repository at this point in the history
Adds a `setup-jdk-variables.yaml` template to set the `JAVA_HOME` and 
`JI_JAVA_HOME` variables from a single script location.

A new `JI_JAVA_HOME_DEFAULT` variable will also be set to point to a
current JDK that can be used to run various Android SDK tools as needed.

Consolidating this logic should make it easier to update the default
JDK version we install and/or test against in the future.

Build jobs and some test jobs that run on custom agents will use the
default android-toolchain path that xaprepare installs into.

Most other test jobs will attempt to use the value of a preinstalled JDK
found at `$JAVA_HOME_$(JAVA_VERSION)_$(OS_ARCH)` if it is defined.
  • Loading branch information
pjcollins authored Jan 14, 2025
1 parent 6d9b295 commit 0475a60
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 56 deletions.
7 changes: 3 additions & 4 deletions build-tools/automation/azure-pipelines-apidocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ extends:
- checkout: self
submodules: recursive

- script: |
echo "##vso[task.setvariable variable=JI_JAVA_HOME]$HOME/android-toolchain/jdk-17"
echo "##vso[task.setvariable variable=JAVA_HOME]$HOME/android-toolchain/jdk-17"
displayName: set JI_JAVA_HOME
- template: /build-tools/automation/yaml-templates/setup-jdk-variables.yaml@self
parameters:
useAgentJdkPath: false

# Set MSBuild property overrides if parameters are set
- ${{ if ne(parameters.apiLevel, 'default') }}:
Expand Down
16 changes: 4 additions & 12 deletions build-tools/automation/azure-pipelines-nightly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ stages:
- template: /build-tools/automation/yaml-templates/setup-test-environment.yaml
parameters:
xaprepareScenario: EmulatorTestDependencies
jdkTestFolder: $(JAVA_HOME_11_X64)
jdkMajorVersion: 11

- template: /build-tools/automation/yaml-templates/run-dotnet-preview.yaml
parameters:
Expand All @@ -138,6 +138,7 @@ stages:
avdApiLevel: $(avdApiLevel)
avdAbi: $(avdAbi)
avdType: $(avdType)
emulatorMSBuildArgs: -p:JavaSdkDirectory=$(JI_JAVA_HOME_DEFAULT)

- template: /build-tools/automation/yaml-templates/apk-instrumentation.yaml
parameters:
Expand All @@ -162,7 +163,6 @@ stages:
emulatorMSBuildArgs: -p:TestAvdExtraBootArgs=-writable-system
jobName: SystemApplicationTests
jobTimeout: 120
jdkTestFolder: $HOME/android-toolchain/jdk-17
use1ESTemplate: false
testSteps:
- template: run-nunit-tests.yaml
Expand Down Expand Up @@ -195,15 +195,11 @@ stages:
steps:
- template: agent-cleanser/v1.yml@yaml-templates

- script: |
echo "##vso[task.setvariable variable=JAVA_HOME]$HOME/android-toolchain/jdk-17"
displayName: set JAVA_HOME to $HOME/android-toolchain/jdk-17
- template: /build-tools/automation/yaml-templates/setup-test-environment.yaml
parameters:
installTestSlicer: true
xaprepareScenario: EmulatorTestDependencies
jdkTestFolder: $HOME/android-toolchain/jdk-17
useAgentJdkPath: false

- task: DownloadPipelineArtifact@2
inputs:
Expand Down Expand Up @@ -251,15 +247,11 @@ stages:
steps:
- template: agent-cleanser/v1.yml@yaml-templates

- script: |
echo "##vso[task.setvariable variable=JAVA_HOME]$HOME/android-toolchain/jdk-17"
displayName: set JAVA_HOME to $HOME/android-toolchain/jdk-17
- template: /build-tools/automation/yaml-templates/setup-test-environment.yaml
parameters:
installTestSlicer: true
xaprepareScenario: EmulatorTestDependencies
jdkTestFolder: $HOME/android-toolchain/jdk-17
useAgentJdkPath: false

- task: DownloadPipelineArtifact@2
inputs:
Expand Down
5 changes: 0 additions & 5 deletions build-tools/automation/azure-pipelines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,6 @@ extends:
inputs:
forceReinstallCredentialProvider: true

- script: |
echo ##vso[task.setvariable variable=JI_JAVA_HOME]%JAVA_HOME_17_X64%
echo ##vso[task.setvariable variable=JAVA_HOME]%JAVA_HOME_17_X64%
displayName: set JI_JAVA_HOME, JAVA_HOME to $(JAVA_HOME_17_X64)
- script: echo "##vso[task.prependpath]C:\Windows\System32\WindowsPowerShell\v1.0\"
displayName: add powershell to path

Expand Down
4 changes: 1 addition & 3 deletions build-tools/automation/yaml-templates/build-windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ stages:

- template: /build-tools/automation/yaml-templates/clean.yaml

- script: |
echo ##vso[task.setvariable variable=JI_JAVA_HOME]%JAVA_HOME_17_X64%
displayName: set JI_JAVA_HOME to $(JAVA_HOME_17_X64)
- template: /build-tools/automation/yaml-templates/setup-jdk-variables.yaml

- template: /build-tools/automation/yaml-templates/use-dot-net.yaml
parameters:
Expand Down
5 changes: 3 additions & 2 deletions build-tools/automation/yaml-templates/commercial-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ parameters:
use1ESTemplate: true

steps:
- script: echo "##vso[task.setvariable variable=JI_JAVA_HOME]$HOME/android-toolchain/jdk-17"
displayName: set JI_JAVA_HOME
- template: /build-tools/automation/yaml-templates/setup-jdk-variables.yaml
parameters:
useAgentJdkPath: false

- template: /build-tools/automation/yaml-templates/use-dot-net.yaml
parameters:
Expand Down
10 changes: 4 additions & 6 deletions build-tools/automation/yaml-templates/run-emulator-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ parameters:
emulatorMSBuildArgs: ''
jobName: CheckTimeZoneInfoIsCorrectNode1
jobTimeout: 360
jdkTestFolder: $(JAVA_HOME_17_X64)
jdkMajorVersion: $(DefaultJavaSdkMajorVersion)
useAgentJdkPath: false
testSteps: []
use1ESTemplate: true

Expand All @@ -22,14 +23,11 @@ jobs:
steps:
- template: agent-cleanser/v1.yml@yaml-templates

- script: |
echo "##vso[task.setvariable variable=JAVA_HOME]${{ parameters.jdkTestFolder }}"
displayName: set JAVA_HOME to ${{ parameters.jdkTestFolder }}
- template: /build-tools/automation/yaml-templates/setup-test-environment.yaml
parameters:
xaprepareScenario: EmulatorTestDependencies
jdkTestFolder: ${{ parameters.jdkTestFolder }}
jdkMajorVersion: ${{ parameters.jdkMajorVersion }}
useAgentJdkPath: ${{ parameters.useAgentJdkPath }}

- task: DownloadPipelineArtifact@2
inputs:
Expand Down
26 changes: 26 additions & 0 deletions build-tools/automation/yaml-templates/setup-jdk-variables.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
parameters:
jdkMajorVersion: $(DefaultJavaSdkMajorVersion)
useAgentJdkPath: true

steps:
- pwsh: |
$agentOS="$(Agent.OS)"
$agentArch="$(Agent.OSArchitecture)"
$jdkMajorVersion="${{ parameters.jdkMajorVersion }}"
$xaPrepareJdkPath="$env:HOME/android-toolchain/jdk-$jdkMajorVersion"
if ("$agentOS" -eq "Windows_NT") {
$xaPrepareJdkPath="$env:USERPROFILE\android-toolchain\jdk-$jdkMajorVersion"
}
$jdkHomePath=$xaPrepareJdkPath
if ("${{ parameters.useAgentJdkPath }}" -eq "true") {
$defaultJdkHomeVarName="JAVA_HOME_$(DefaultJavaSdkMajorVersion)_${agentArch}"
$defaultJdkHomePath=(Get-Item -Path "env:$defaultJdkHomeVarName").Value
$jdkHomeVarName="JAVA_HOME_${jdkMajorVersion}_${agentArch}"
$jdkHomePath=(Get-Item -Path "env:$jdkHomeVarName").Value
}
Write-Host "Setting variable 'JI_JAVA_HOME_DEFAULT' to '$defaultJdkHomePath'"
Write-Host "##vso[task.setvariable variable=JI_JAVA_HOME_DEFAULT]$defaultJdkHomePath"
Write-Host "Setting variable 'JAVA_HOME' and 'JI_JAVA_HOME' to '$jdkHomePath'"
Write-Host "##vso[task.setvariable variable=JAVA_HOME]$jdkHomePath"
Write-Host "##vso[task.setvariable variable=JI_JAVA_HOME]$jdkHomePath"
displayName: set JAVA_HOME and JI_JAVA_HOME
18 changes: 6 additions & 12 deletions build-tools/automation/yaml-templates/setup-test-environment.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
parameters:
configuration: $(XA.Build.Configuration)
xaSourcePath: $(System.DefaultWorkingDirectory)
jdkTestFolder: $(JAVA_HOME_17_X64)
jdkMajorVersion: $(DefaultJavaSdkMajorVersion)
useAgentJdkPath: true
remove_dotnet: false
dotnetVersion: $(DotNetSdkVersion)
dotnetQuality: $(DotNetSdkQuality)
Expand All @@ -21,17 +22,10 @@ steps:
clean: true
submodules: recursive

- script: |
echo "##vso[task.setvariable variable=JI_JAVA_HOME]${{ parameters.jdkTestFolder }}"
echo "##vso[task.setvariable variable=DOTNET_TOOL_PATH]${{ parameters.xaSourcePath }}/bin/${{ parameters.configuration }}/dotnet/dotnet"
displayName: set JI_JAVA_HOME to ${{ parameters.jdkTestFolder }}
condition: and(succeeded(), ne(variables['agent.os'], 'Windows_NT'))

- script: |
echo ##vso[task.setvariable variable=JI_JAVA_HOME]${{ parameters.jdkTestFolder }}
echo ##vso[task.setvariable variable=DOTNET_TOOL_PATH]${{ parameters.xaSourcePath }}\bin\${{ parameters.configuration }}\dotnet\dotnet.exe
displayName: set JI_JAVA_HOME to ${{ parameters.jdkTestFolder }}
condition: and(succeeded(), eq(variables['agent.os'], 'Windows_NT'))
- template: /build-tools/automation/yaml-templates/setup-jdk-variables.yaml
parameters:
jdkMajorVersion: ${{ parameters.jdkMajorVersion }}
useAgentJdkPath: ${{ parameters.useAgentJdkPath }}

# Install latest .NET
- template: /build-tools/automation/yaml-templates/use-dot-net.yaml
Expand Down
2 changes: 2 additions & 0 deletions build-tools/automation/yaml-templates/variables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ variables:
value: $[or(startsWith(variables['Build.SourceBranch'], 'refs/heads/release/'), startsWith(variables['System.PullRequest.TargetBranch'], 'release/'))]
- name: DefaultTestSdkPlatforms # Comma-separated SDK Platform(s) to install on test agents (no spaces)
value: 35,Baklava
- name: DefaultJavaSdkMajorVersion
value: 17
- name: ExcludedNightlyNUnitCategories
value: 'cat != SystemApplication & cat != TimeZoneInfo & cat != Localization'
- name: RunMAUITestJob
Expand Down
3 changes: 3 additions & 0 deletions build-tools/scripts/TestApks.targets
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@
<!-- SDK component installation can be frail, try a few times. -->
<Exec
Command="&quot;$(CommandLineToolsBinPath)\sdkmanager&quot; &quot;$(SdkManagerImageName)&quot;"
EnvironmentVariables="JAVA_HOME=$(JavaSdkDirectory)"
ContinueOnError="true">
<Output TaskParameter="ExitCode" PropertyName="_SdkManagerExitCode" />
</Exec>
Expand All @@ -389,6 +390,7 @@
/>
<Exec
Command="&quot;$(CommandLineToolsBinPath)\sdkmanager&quot; &quot;$(SdkManagerImageName)&quot;"
EnvironmentVariables="JAVA_HOME=$(JavaSdkDirectory)"
ContinueOnError="true"
Condition=" '$(_SdkManagerExitCode)' != '0' ">
<Output TaskParameter="ExitCode" PropertyName="_SdkManagerExitCode" />
Expand All @@ -399,6 +401,7 @@
/>
<Exec
Command="&quot;$(CommandLineToolsBinPath)\sdkmanager&quot; &quot;$(SdkManagerImageName)&quot;"
EnvironmentVariables="JAVA_HOME=$(JavaSdkDirectory)"
ContinueOnError="true"
Condition=" '$(_SdkManagerExitCode)' != '0' ">
<Output TaskParameter="ExitCode" PropertyName="_SdkManagerExitCode" />
Expand Down
21 changes: 9 additions & 12 deletions build-tools/xaprepare/xaprepare/Steps/Step_InstallAdoptOpenJDK.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,11 @@ protected override async Task<bool> Execute (Context context)
return true;
}

// Check for a JDK installed on CI with a matching major version to use for test jobs
// Check for a JDK installed on CI to use for test jobs
var jiJavaHomeVarValue = Environment.GetEnvironmentVariable ("JI_JAVA_HOME");
if (AllowJIJavaHomeMatch && Directory.Exists (jiJavaHomeVarValue)) {
jdkInstallDir = jiJavaHomeVarValue;
OpenJDKExistsAndIsValid (jdkInstallDir, out installedVersion);
if (Version.TryParse (installedVersion, out Version? cversion) && cversion != null) {
if (cversion.Major == JdkVersion.Major) {
Log.Status ($"{ProductName} with version ");
Log.Status (installedVersion ?? "Unknown", ConsoleColor.Yellow);
Log.StatusLine (" already installed in: ", jdkInstallDir, tailColor: ConsoleColor.Cyan);
return true;
}
}
if (AllowJIJavaHomeMatch && Directory.Exists (jiJavaHomeVarValue) && JdkFilesExist (jiJavaHomeVarValue)) {
Log.StatusLine ("Skipping JDK install for test job, JDK exists at: ", jdkInstallDir, tailColor: ConsoleColor.Cyan);
return true;
}

Log.StatusLine ($"{ProductName} {JdkVersion} r{JdkRelease} will be installed to {jdkInstallDir}");
Expand Down Expand Up @@ -254,6 +246,11 @@ bool OpenJDKExistsAndIsValid (string installDir, out string? installedVersion)
return false;
}

return JdkFilesExist (installDir);
}

bool JdkFilesExist (string installDir)
{
foreach (string f in jdkFiles) {
string file = Path.Combine (installDir, f);
if (!File.Exists (file)) {
Expand Down

0 comments on commit 0475a60

Please sign in to comment.