How do I determined a User Environment Variable in YAML file for Microsoft Hosted Agent? - azure-devops

As per title above.
I am trying to mimic my automation setup on my local machine to match the automation i have running using Microsoft Hosted Agent machine.
This is how it set up in my local machine. therefore when the automation inserts "%repoic%" in a file explorer and the select folder button is pressed, it will open the desired folder
Now, I need to do the same using the Microsoft Hosted Agent to run my automation. Would anyone give me a clue on how this can be done? Is it simply do the following within my DevOps Pipeline's YAML file?
cheers!

From your requirement, you need to set the Environment variable and use it to navigate to path of the agent machine.
In Azure DevOps Pipeline, you can set the Pipeline variable as the screenshot show in the question.
Then you can use the format: $(variablename) to use the Pipeline variable.
Or you can use the format: %NAME% for batch and $env:NAME in PowerShell to use the Pipeline Environment Variable. Refer to this doc: Environment Variable
Here is an example:
pool:
vmImage: windows-latest
variables:
testpath: ./src/app/head
steps:
- powershell: Get-Location
workingDirectory: $(testpath)
Result:
OR you can use the environment variable in Pipeline.
Example:
pool:
vmImage: windows-latest
variables:
testpath: ./src/app/head
steps:
- task: CmdLine#2
inputs:
script: |
cd %testpath%

Related

Use azure keyvault secret as environment variable in Azure DevOps pipeline

As part of build I am using 'envsubst' command to replace all secrets from environment variables to my application configuration file. We are using Azure DevOps pipeline for our build process and now start using Azure KeyVault to store all these Secrets. Current Issue is that I am not able to make these secrets as environment variable in MS based agent runner. I tried to refer multiple documents but nothing helps.
Did anyone able to set the Azure KeyVault secrets as environment variable on the build agent runner. Any clue/guidance will be of help
Tried using setvariable task but it helps to read the secret and use it within job but not help insetting that variable as environment variable
task: Bash#3
inputs:
targetType: 'inline'
script: |
# Write your commands here
echo "##vso[task.setvariable variable=MySecret;issecret=true]$(MY-SECRET)"
Export and set command inside shell task is not allowing to setup environment variable
Also tried env setting, but that too didn't help
env:
MYSECRET: $(MY-SECRET)
Anyone who have implemented the same, do let me know

Image version by BuildID when do task 'Build module images' for iotedge - Azure pipeline

I am building a docker image for Azure IoTEdge using pipeline, then push to Azure Container Registry. Everything worked but:
Current the image version is fixed (or need to set manual) in module.json:
However I want image version will update by BuidID (Or any unique ID), I tried below code but it did not work:
This is error from pipeline log:
I tried to read document build module image, but it dont have option for tagging like build image using docker v2 task.
Hope you can help me on it!
The Pipeline variable $(Build.BuildId) will not directly pass to module.json file.
To meet your requirement, you can add the task File transform to pass the variable value to module.json file.
Here is an example:
variables:
image.tag.version: $(Build.BuildId)
steps:
- task: FileTransform#1
inputs:
folderPath: '$(System.DefaultWorkingDirectory)'
fileType: 'json'
targetFiles: 'module.json'
- task: Docker#2
Or you can also use the Replace token task. Refer to this ticket: How to apply a variable in Azure Pipelines to a node app during build

Is there a predefined variable for "/home/vsts" in Azure Pipelines?

I need to cache files in /home/vsts/.cache/torch/.
Is there a predefined variable for the home folder?
If by "Predefined variables" you mean one of the variables specified in the documentation then no.
However if you are using Hosted Linux Agents you can simply look at the environment variable $HOME, which works at least if you are in a script:, which may or may not fit your use case.
Example:
trigger:
- main
pool:
vmImage: ubuntu-latest
steps:
- script: echo $HOME
The output of the script task is /home/vsts
No, there is no such variable. You can use one of the /home/vsts/work paths and cache $(System.WorkFolder)/../.cache/torch/.
Values of predefined variables for an Ubuntu agent:
Agent.AcceptTeeEula True
Agent.BuildDirectory /home/vsts/work/1
Agent.HomeDirectory /home/vsts/agents/2.181.2
Agent.Id 32
Agent.JobName Job
Agent.JobStatus Succeeded
Agent.MachineName fv-az160-540
Agent.Name Azure Pipelines 2
Agent.OS Linux
Agent.OSArchitecture X64
Agent.RetainDefaultEncoding false
Agent.ReadOnlyVariables true
Agent.RootDirectory /home/vsts/work
Agent.TempDirectory /home/vsts/work/_temp
Agent.ToolsDirectory /opt/hostedtoolcache
Agent.Version 2.181.2
Agent.WorkFolder /home/vsts/work
Build.ArtifactStagingDirectory /home/vsts/work/1/a
Build.BinariesDirectory /home/vsts/work/1/b
Build.DefinitionName FooBar
Build.SourceBranch refs/heads/FOOBAR-123-caching
Build.SourceVersion 2b2c45223722fc7226eb23d752fc722bcdee2c54
Build.SourcesDirectory /home/vsts/work/1/s
Build.StagingDirectory /home/vsts/work/1/a
Common.TestResultsDirectory /home/vsts/work/1/TestResults
Pipeline.Workspace /home/vsts/work/1
System.AccessToken ***
System.ArtifactsDirectory /home/vsts/work/1/a
System.CollectionId c02d3c7b-9d74-4532-aa21-abccdf07c888
System.Culture en-US
System.DefaultWorkingDirectory /home/vsts/work/1/s
System.DefinitionId 26
System.EnableAccessToken SecretVariable
System.HostType build
System.JobAttempt 1
System.JobId 33f11733-54f2-5aa3-20dd-22fc7dcf5912
System.JobName __default
System.PhaseAttempt 1
System.PhaseDisplayName Job
System.PhaseName Job
System.ServerType Hosted
System.StageAttempt 1
System.StageName __default
System.TeamProject Foo Bar
System.TeamProjectId 3337a4f6-1411-40aa-beeb-0c7d86b9ecba
System.WorkFolder /home/vsts/work
Task.DisplayName print predefined variables

Azure Devops - How to pass environment variables into dotnet test?

I have a standard .NET Core (Ubuntu) pipeline on Azure Devops and within my Test project, I use environment variables. Within my pipeline, I have defined my group variables like so
variables:
- group: MyApiVariables
Whenever I run the tests for my project
- task: DotNetCoreCLI#2
displayName: "Testing Application"
inputs:
command: test
projects: '**/*Tests/*.csproj'
arguments: '--configuration $(buildConfiguration)'
The actual environment variables aren't passed in. They are blank.
What am I missing to get this running? I've even defined variables in the edit pipeline page too with no luck
- task: Bash#3
inputs:
targetType: 'inline'
script: echo $AppConfigEndpoint
env:
AppConfigEndpoint: $(AppConfigEndpoint)
ApiConfigSection: $(ApiConfigSection)
Thanks!
CASING Strikes again! MyVariableName was turned into MYVARIABLENAME on Azure Devops. I changed my variable names in my group to all caps and it worked. I spent way too much time on this.
Mike, I see that you use variable groups. I assume it may cause your issue. Take a look at variable passing example I made:
First I had to create new variable group in Library:
Here is a pipeline code that reference created variables:
# Set variables group reference
variables:
- group: SampleVariableGroup
steps:
- powershell: 'Write-Host "Config variable=$(configuration) Platform variable=$(platform)"'
displayName: 'Display Sample Variable'
I used PowerShell task to verify if variables were properly passed to the job.
As you can see both configuration & platform values were displayed correctly.
In fact you can't go wrong that way, unless you start to mix variable groups with variables defined in a yaml. In such scenario you'll have to use name/value syntax for the individual (non-grouped) variables.
Please see Microsoft Variable Groups documentation. Such example is well explained there. I also suggest to take closer look at general Variables Documentation.
In case of referencing variables in other tasks here is a great example from MS (it should work everywhere in same manner):
# Set variables once
variables:
configuration: debug
platform: x64
steps:
# Use them once
- task: MSBuild#1
inputs:
solution: solution1.sln
configuration: $(configuration) # Use the variable
platform: $(platform)
# Use them again
- task: MSBuild#1
inputs:
solution: solution2.sln
configuration: $(configuration) # Use the variable
platform: $(platform)
Good luck!

Is it possible to set an VSTS Build variable in a Build Step so that the value can be used in a subsequent Build Step?

I'm currently using Build in Visual Studio Team Services (was Visual Studio Online), and would like to be able to set a Build Variable in a Build Step so that the new value can be used in a subsequent Build Step.
Obviously you can set it before the Build starts but I'm looking to late bind the variable during a subsequent Build Step.
Is this possible?
When inside of a script you can update a variable by emitting the following in your ps1
"##vso[task.setvariable variable=testvar;]testvalue"
You can then pass the variable into the next script using $(testvar)
This doc from the API talks about what ##vso commands you can use.
Don't forget to set system.debug to true. It seems there is a bug that muted stdout and thus, all ##vso are not working.
https://github.com/Microsoft/vso-agent-tasks/blob/master/docs/authoring/commands.md
You can create a powershell script an reference it as a build task.
Then inside your powershell scripts add this:
"##vso[task.setvariable variable=key]value"
After that on all your tasks you can read the variable as $(key).
If you want to protect your variable, use:
"##vso[task.setvariable variable=secretVar;issecret=true]value"
And then use it as $(secretVar) in your next tasks.
I found this link helpful: https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=powershell
This has the complete options of what you can do: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch
You can reuse set variable from task to task, and also job to job. I couldn't find anything on stage to stage.
In summary:
jobs:
# Set an output variable from job A
- job: A
pool:
vmImage: 'vs2017-win2016'
steps:
- powershell: echo "##vso[task.setvariable variable=myOutputVar;isOutput=true]this is the value"
name: setvarStep
- script: echo $(setvarStep.myOutputVar)
name: echovar
# Map the variable into job B
- job: B
dependsOn: A
pool:
vmImage: 'ubuntu-16.04'
variables:
myVarFromJobA: $[ dependencies.A.outputs['setvarStep.myOutputVar'] ] # map in the variable
# remember, expressions require single quotes
steps:
- script: echo $(myVarFromJobA)
name: echovar