Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ci] Add template for setting *JAVA_HOME vars #9671

Merged
merged 6 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is likely a bug here because $(Agent.OSArchitecture) will be ARM64, the environment variable is JAVA_HOME_17_arm64, and environment variable names are case sensitive on Mac/Linux. But I can fix this in my ARM64 PR since I can test it easier.

$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
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 @@ -87,7 +87,7 @@
<Output TaskParameter="AdbTarget" PropertyName="_EmuTarget" />
<Output TaskParameter="EmulatorProcessId" PropertyName="_EmuPid" />
</StartAndroidEmulator>
<Xamarin.Android.Tools.BootstrapTasks.Adb

Check failure on line 90 in build-tools/scripts/TestApks.targets

View check run for this annotation

Azure Pipelines / Xamarin.Android Nightly (Test APKs SystemApplicationTests Emulator Tests)

build-tools/scripts/TestApks.targets#L90

build-tools/scripts/TestApks.targets(90,5): Error : Command /Users/builder/android-toolchain/sdk/platform-tools/adb -s emulator-5570 wait-for-device failed with exit code -1

Check failure on line 90 in build-tools/scripts/TestApks.targets

View check run for this annotation

Azure Pipelines / Xamarin.Android Nightly (Test TimeZoneInfo TimeZoneInfoTests Emulator Tests 3)

build-tools/scripts/TestApks.targets#L90

build-tools/scripts/TestApks.targets(90,5): Error : Command /Users/builder/android-toolchain/sdk/platform-tools/adb -s emulator-5570 wait-for-device failed with exit code -1

Check failure on line 90 in build-tools/scripts/TestApks.targets

View check run for this annotation

Azure Pipelines / Xamarin.Android Nightly (Test Localization Localization Emulator Tests 5)

build-tools/scripts/TestApks.targets#L90

build-tools/scripts/TestApks.targets(90,5): Error : Command /Users/builder/android-toolchain/sdk/platform-tools/adb -s emulator-5570 wait-for-device failed with exit code -1

Check failure on line 90 in build-tools/scripts/TestApks.targets

View check run for this annotation

Azure Pipelines / Xamarin.Android Nightly (Test Localization Localization Emulator Tests 2)

build-tools/scripts/TestApks.targets#L90

build-tools/scripts/TestApks.targets(90,5): Error : Command /Users/builder/android-toolchain/sdk/platform-tools/adb -s emulator-5570 wait-for-device failed with exit code -1
EnvironmentVariables="ADB_TRACE=all"
Condition=" '$(_ValidAdbTarget)' != 'True' "
Arguments="$(_AdbTarget) wait-for-device"
Expand Down 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
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
Loading