简体   繁体   English

Azure Devops 管道:访问 xml 文件中的代理 JAVA_HOME 变量

[英]Azure Devops Pipeline : Accessing agent JAVA_HOME variable in xml file

For my java project, I am using azure devops pipeline for building.对于我的 java 项目,我正在使用 azure devops 管道进行构建。 For configuring the jdkhome path, I am putting it in the toolchains xml file placed in root folder of project:为了配置 jdkhome 路径,我将其放在项目根文件夹中的工具链 xml 文件中:

<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.但是 azure devops 代理已经更新了他们的 jdk 并且它失败了我的构建。 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.我看到在代理上,环境变量设置为 $JAVA_HOME_8_X64 并分配了路径。

So, how can I use this environment variable in my pipeline weather refer it in toolschain xml file or in some other way?那么,我如何在我的管道天气中使用这个环境变量,在工具链 xml 文件中或以其他方式引用它?

I tried to directly refer in xml and it did not work.我尝试直接参考xml,但没有用。 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.我们可以使用扩展Replace TokensMagic Chunks来更新 pom.xml 文件。

pom.xml content: pom.xml 内容:

<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.创建构建流水线A,打开构建流水线A,添加新变量JAVAHOME,并赋予测试构建服务(xxx)账号编辑构建流水线权限。 (open the build pipeline(A)-->... --> Security --> Edit build pipeline set to Allow) (打开构建管道(A)-->...--> 安全--> 编辑构建管道设置为允许) 在此处输入图像描述

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.启用功能允许脚本访问 OAuth 令牌(单击代理作业名称=>其他选项)添加任务 powershell(更新 JAVAHOME 值)并输入下面的脚本。

$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 . c Add task Replace Tokens and configure the task.添加任务 Replace Tokens 并配置任务。 在此处输入图像描述

d . d . Add a task powershell and use the code $ourfilesdata = Get-Content "pom.xml" Write-Output $ourfilesdata to output the file content.添加任务 powershell 并使用代码$ourfilesdata = Get-Content "pom.xml" Write-Output $ourfilesdata到 output 文件内容。

Result:结果: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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