简体   繁体   中英

Azure Devops Pipeline : Accessing agent JAVA_HOME variable in xml file

For my java project, I am using azure devops pipeline for building. For configuring the jdkhome path, I am putting it in the toolchains xml file placed in root folder of project:

<toolchain>
    <type>jdk</type>
    <provides>
      <version>1.8</version>
      <vendor>oracle</vendor>
    </provides>
    <configuration>
      <!--<jdkHome>/usr/lib/jvm/zulu-8-azure-amd64</jdkHome>-->
    </configuration>
  </toolchain>

But the azure devops agent has updated their jdk and its failing my build. So, now rather than hardcoding, I want to pick the path from agent. I see that on agent, environment variable is set as $JAVA_HOME_8_X64 with the path assigned to it.

So, how can I use this environment variable in my pipeline weather refer it in toolschain xml file or in some other way?

I tried to directly refer in xml and it did not work. I also tried to add some tasks from marketplace but did not work. Can someone help me with this?

We can use the extension Replace Tokens or Magic Chunks to update the pom.xml file.

pom.xml content:

<toolchain>
    <type>jdk</type>
    <provides>
      <version>1.8</version>
      <vendor>oracle</vendor>
    </provides>
    <configuration>
      <jdkHome>JAVAHOME</jdkHome>
    </configuration>
  </toolchain>

Step:

a . Create a build pipeline A, Open the build pipeline A and add a new variable JAVAHOME and grant test Build Service (xxx) account the Edit build pipeline permission. (open the build pipeline(A)-->... --> Security --> Edit build pipeline set to Allow) 在此处输入图像描述

b . enable the feature Allow scripts to access the OAuth token (Click Agent Job Name=>Additional options) add task powershell(update the JAVAHOME value) and enter the script below.

$url = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/definitions/{build pipeline A definition ID}?api-version=5.1"
Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"

# Update an existing variable named JAVAHOME to its new value JAVA_HOME_11_X64
$pipeline.variables.JAVAHOME.value= $($env:JAVA_HOME_11_X64)
# my sample is get the variable JAVA_HOME_11_X64, please update it to JAVA_HOME_8_X64


####****************** update the modified object **************************

$json = $($pipeline | ConvertTo-Json -Depth 100)


$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}

write-host "==========================================================" 
Write-host "The value of Varialbe 'JAVAHOME' is updated to" $updatedef.variables.JAVAHOME.value
write-host "=========================================================="

c . Add task Replace Tokens and configure the task. 在此处输入图像描述

d . Add a task powershell and use the code $ourfilesdata = Get-Content "pom.xml" Write-Output $ourfilesdata to output the file content.

Result: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM