在运行VSTS构建定义时,如何将功能作为环境变量传递给批处理脚本? [英] How to pass capabilities as a environmental variable to batch script while running the VSTS build definition?

查看:82
本文介绍了在运行VSTS构建定义时,如何将功能作为环境变量传递给批处理脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行VSTS构建定义时,我正在运行一个批处理脚本.在该批处理脚本中,我具有从托管代理功能部分获取的Maven路径.我不希望该路径在该批处理文件中进行硬编码.我想在运行构建定义时在脚本中使用环境变量来调用该路径.我从托管代理功能部分获得的路径.下面是批处理脚本.

I am running one batch script while running the VSTS build definition. In that batch script I have maven path which is taken from the hosted agent capabilities section. I don't want that path to be hard coded in that batch file. I want to call instead that path using an environmental variable in the script while running the build definition. The path I have taken from hosted agent capabilities section. Below is the batch script.

在VSTS中使用批处理脚本任务,我正在VSTS构建定义中调用下面的abc.bat文件.

Using batch script task in VSTS I am calling the below abc.bat file in VSTS build definition.

批处理脚本:

abc.bat:

call C:\java\maven\apache-maven-3.2.2\bin\mvn.bat install:install-file -Dfile=DevOps/proj_Dep_libs/application-agent-1.0.3.jar -DgroupId=application-agent -DpomFile=DevOps/Pss_Dep_libs/application-agent-1.0.3.pom -DartifactId=application-agent -Dversion=1.0.3 -Dpackaging=jar

请帮助我在运行VSTS构建定义时如何在批处理脚本中将路径作为变量传递.

Please help me on how to pass the path as a variable in the batch script while running the VSTS build definition.

推荐答案

您可以从托管代理功能部分检索Maven路径,然后使用

You can retrieve maven path from the hosted agent capabilities section, then create the variable using the Logging Commands. Then you can use the variable in batch script instead calling maven path.

  1. 创建一个PowerShell脚本以设置变量(下面的参考) 示例,您还可以

  1. Create a PowerShell script to set the avariables (Reference below sample, you can also Use the OAuth token to access the REST API), then check in the script into VSTS.

在定义中添加PowerShell任务以运行PS脚本

Add a PowerShell task in your definition to run the PS script

3.在设置变量步骤之后的步骤中使用该变量

3.Use the variable in steps which behind the set variable step

$collectionurl = "https://xxx.visualstudio.com"
$user = "username"
$token = "password"

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$baseUrl = "$collectionurl/_apis/distributedtask/pools/2/agents/1?includeCapabilities=true"          
$response = (Invoke-RestMethod -Uri $baseUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})

#Retrieve values 
$maven = $response.systemCapabilities.maven

#Set variable
Write-Host "##vso[task.setvariable variable=maven]$maven"

#Then you can use the variable in the next step: $(maven) in TFS, $env:maven in PowerShell, %maven% in batch script.


更新:

好吧,您可以在下面的脚本中使用不带用户名的PAT(如果您不想在脚本中对令牌进行硬编码,则可以创建一个秘密变量并将令牌设置为变量的值,然后在脚本中使用变量):

Well, you can use PAT without the username with below script (If you don't want to hardcode the token in the script, then you can create a secret variable and set the token as the value of the variable, then use the variable in script):

$PAT = "nvkoa6qrdrtrxweruiodfiamwd3ya2dkt7r6cx3xxxxw5pyxxxxq" 

# Base64-encodes the Personal Access Token (PAT) appropriately 
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$PAT))) 
$baseUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)_apis/distributedtask/pools/2/agents/1?includeCapabilities=true"
$response = (Invoke-RestMethod -Uri $baseUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}) 

#Retrieve values 
$maven = $response.systemCapabilities.maven 
Write-Host "##vso[task.setvariable variable=maven]$maven"

这篇关于在运行VSTS构建定义时,如何将功能作为环境变量传递给批处理脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆