Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

azure devops - How to pass capabilities as a environmental variable to batch script while running the VSTS build definition?

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.

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

Batch script:

abc.bat:

call C:javamavenapache-maven-3.2.2inmvn.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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. 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.

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

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.

UPDATE:

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"

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...