From 0475a60c1c53616baab0fd9d60a92edbc0871bdc Mon Sep 17 00:00:00 2001 From: Peter Collins Date: Mon, 13 Jan 2025 19:14:02 -0500 Subject: [PATCH] [ci] Add template for setting JAVA_HOME vars (#9671) 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. --- .../automation/azure-pipelines-apidocs.yaml | 7 +++-- .../automation/azure-pipelines-nightly.yaml | 16 +++--------- build-tools/automation/azure-pipelines.yaml | 5 ---- .../yaml-templates/build-windows.yaml | 4 +-- .../yaml-templates/commercial-build.yaml | 5 ++-- .../yaml-templates/run-emulator-tests.yaml | 10 +++---- .../yaml-templates/setup-jdk-variables.yaml | 26 +++++++++++++++++++ .../setup-test-environment.yaml | 18 +++++-------- .../automation/yaml-templates/variables.yaml | 2 ++ build-tools/scripts/TestApks.targets | 3 +++ .../Steps/Step_InstallAdoptOpenJDK.cs | 21 +++++++-------- 11 files changed, 61 insertions(+), 56 deletions(-) create mode 100644 build-tools/automation/yaml-templates/setup-jdk-variables.yaml diff --git a/build-tools/automation/azure-pipelines-apidocs.yaml b/build-tools/automation/azure-pipelines-apidocs.yaml index e568abf1004..5bdbd58d155 100644 --- a/build-tools/automation/azure-pipelines-apidocs.yaml +++ b/build-tools/automation/azure-pipelines-apidocs.yaml @@ -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') }}: diff --git a/build-tools/automation/azure-pipelines-nightly.yaml b/build-tools/automation/azure-pipelines-nightly.yaml index a6c89bbf41f..2fc23d55aff 100644 --- a/build-tools/automation/azure-pipelines-nightly.yaml +++ b/build-tools/automation/azure-pipelines-nightly.yaml @@ -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: @@ -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: @@ -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 @@ -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: @@ -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: diff --git a/build-tools/automation/azure-pipelines.yaml b/build-tools/automation/azure-pipelines.yaml index 4dbf996a2f1..511d7338185 100644 --- a/build-tools/automation/azure-pipelines.yaml +++ b/build-tools/automation/azure-pipelines.yaml @@ -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 diff --git a/build-tools/automation/yaml-templates/build-windows.yaml b/build-tools/automation/yaml-templates/build-windows.yaml index 832cddfbede..52c6e17838d 100644 --- a/build-tools/automation/yaml-templates/build-windows.yaml +++ b/build-tools/automation/yaml-templates/build-windows.yaml @@ -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: diff --git a/build-tools/automation/yaml-templates/commercial-build.yaml b/build-tools/automation/yaml-templates/commercial-build.yaml index 8fa529440f0..0e2ed655eee 100644 --- a/build-tools/automation/yaml-templates/commercial-build.yaml +++ b/build-tools/automation/yaml-templates/commercial-build.yaml @@ -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: diff --git a/build-tools/automation/yaml-templates/run-emulator-tests.yaml b/build-tools/automation/yaml-templates/run-emulator-tests.yaml index cffa60b6b04..3142a2c6a5d 100644 --- a/build-tools/automation/yaml-templates/run-emulator-tests.yaml +++ b/build-tools/automation/yaml-templates/run-emulator-tests.yaml @@ -4,7 +4,8 @@ parameters: emulatorMSBuildArgs: '' jobName: CheckTimeZoneInfoIsCorrectNode1 jobTimeout: 360 - jdkTestFolder: $(JAVA_HOME_17_X64) + jdkMajorVersion: $(DefaultJavaSdkMajorVersion) + useAgentJdkPath: false testSteps: [] use1ESTemplate: true @@ -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: diff --git a/build-tools/automation/yaml-templates/setup-jdk-variables.yaml b/build-tools/automation/yaml-templates/setup-jdk-variables.yaml new file mode 100644 index 00000000000..780952babd4 --- /dev/null +++ b/build-tools/automation/yaml-templates/setup-jdk-variables.yaml @@ -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 diff --git a/build-tools/automation/yaml-templates/setup-test-environment.yaml b/build-tools/automation/yaml-templates/setup-test-environment.yaml index 8973223431f..e95a9782ece 100644 --- a/build-tools/automation/yaml-templates/setup-test-environment.yaml +++ b/build-tools/automation/yaml-templates/setup-test-environment.yaml @@ -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) @@ -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 diff --git a/build-tools/automation/yaml-templates/variables.yaml b/build-tools/automation/yaml-templates/variables.yaml index 1d8b14a9779..48b60fe65f3 100644 --- a/build-tools/automation/yaml-templates/variables.yaml +++ b/build-tools/automation/yaml-templates/variables.yaml @@ -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 diff --git a/build-tools/scripts/TestApks.targets b/build-tools/scripts/TestApks.targets index a75e6beecb8..fb95425ea5c 100644 --- a/build-tools/scripts/TestApks.targets +++ b/build-tools/scripts/TestApks.targets @@ -380,6 +380,7 @@ @@ -389,6 +390,7 @@ /> @@ -399,6 +401,7 @@ /> diff --git a/build-tools/xaprepare/xaprepare/Steps/Step_InstallAdoptOpenJDK.cs b/build-tools/xaprepare/xaprepare/Steps/Step_InstallAdoptOpenJDK.cs index 36d4190575e..2d3c9e5d99e 100644 --- a/build-tools/xaprepare/xaprepare/Steps/Step_InstallAdoptOpenJDK.cs +++ b/build-tools/xaprepare/xaprepare/Steps/Step_InstallAdoptOpenJDK.cs @@ -59,19 +59,11 @@ protected override async Task 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}"); @@ -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)) {