Original question
Is it possible to store the response from a REST API call in a
variable and use it in the downstream jobs?
Question update:
I want to store the resolved value through a PowerShell script, and make it accessible through out the next stages. I have setup a script like this:
$slot = &"c:\temp\GetSlot.exe" 2>&1
Write-host "resolved:" $slot
Write-host "init value output:" $(currentslot)
Write-Output ("##vso[task.setvariable variable=currentslot;isOutput=true;]$slot")
Write-host "updated value output:" $(currentslot)
Along with a variable, to make it accessible throug the $(currentslot) in aditional stages. Its configured like this:
Stages:
The value from the executions is being set into the $slot variable, but the variable is not getting updated, what do I do wrong?
yes, you would use regular way of doing so:
Write-Host "##vso[task.setvariable variable=containerName]$containerName"
^ ^ variable content (string)
^ variable name in downstream tasks
you can also use yaml to share variables between phases (https://github.com/MicrosoftDocs/vsts-docs/blob/master/docs/pipelines/process/multiple-phases.md)
create\update release through api: https://learn.microsoft.com/en-us/rest/api/azure/devops/release/definitions/create?view=azure-devops-rest-5.0 (it has variables property)
Related
In powershell i usually use write-host to print out a variable whilst working in an azuredevops pipeline to check the value of a variable.
Can you do this in terraform? I am running some terraform code but want to check some of the terraform variable values during the pipeline run , is there a way of doing this?
According to documentation :
- pwsh: |
Write-Host "Non-secrets automatically mapped in, sauce is $env:SAUCE"
Write-Host "Secrets are not automatically mapped in, secretSauce is $env:SECRETSAUCE"
Write-Host "You can use macro replacement to get secrets, and they'll be masked in the log: $(secretSauce)"
Write-Host "Future jobs can also see $env:SETVARS_OUTPUTSAUCE"
write-Host "Future jobs can also see $(SetVars.outputSauce)"
Just run terraform plan part of the pipeline to check if the variables are set as expected.
You can also reference variables in terraform output if you need to.
I have a Powershell task in a job in a Pipeline in Azure Devops Server 2020. Here's part of the powershell:
$xml = [xml](Get-Content $configPath)
Write-Output "Iterating over appSettings"
ForEach($add in $xml.configuration.appSettings.add)
{
#Write-Output "Processing AppSetting key $($add.key)"
$SecretVarKey = "MAPPED_"+$add.key
Write-Output $SecretVarKey
$matchingEnvVar = [Environment]::GetEnvironmentVariable($SecretVarKey)
if($matchingEnvVar)
{
Write-Output "Found matching environment variable for key: $($add.key)"
Write-Output "Replacing value $($add.value) with $matchingEnvVar"
$add.value = $matchingEnvVar
}
}
This works fine in the task -- my builds are doing what I want. But when I view the YAML I see comments like this:
#Your build pipeline references an undefined variable named ‘$add.key’. Create or edit the build pipeline for this YAML file, define the variable on the Variables tab. See https://go.microsoft.com/fwlink/?linkid=865972
Again, this doesn't interfere with the execution of the script.
However, now I want to extract this Task into a Task Group. When I do so, the harmless misdetection is now a problem, because it insists these are new parameters:
Is there some magic I can do to change my Powershell script so they are not thought to be parameters?
Your build pipeline references an undefined variable named ‘$add.key’
This is triggered by the line below:
Write-Output "Found matching environment variable for key: $($add.key)"
The $($add.key) is parsed by Azure DevOps as macro syntax. You could avoid this by using string formatting:
'Found matching environment variable for key: {0}' -f $add.key
Btw, in most cases you don't need to use Write-Output - it's slow and superfluous. See this blog post for details: Let’s Kill Write-Output.
I have the following Powershel script that reads a text file. The value that is read is a value I want to pass as a variable to another step in the Azure Devops yml file
$data = Get-Content .\cluster_txt.txt
Write-Output $data
How can I access the value in $data in another task
You suppose to use logging command to create Azure Pipelines variable
Write-Host "##vso[task.setvariable variable=testvar;]$data"
and then in next task you can reffer to variable as
$(testvar)
or
$env:TESTVAR
Be aware that Azure Pipelines variable will not be available in the task you will create it. And if you need variable in another job or stage then you need output variable. Here you will find how to use it.
I'm using TFS 2017.1 Builds and Release feature.
In my release definition, I have a couple of release variables which I need to refer in my PowerShell task (execute on remote machine). So far, I've tried the following options but couldn't get it to work.
Added an Execute PowerShell task to store release variables into Environment variables:
$releaseVaraiables = Get-ChildItem Env: | Where-Object { $_.Name -like "ACL_*" }
Write-Host "##vso[task.setvariable variable=aclVariables]$releaseVaraiables"
Added an Execute PowerShell on remote machine task:
Here I can't read the Environment variables (maybe because this is remote machine task?)
Write-Verbose "problem reading $env:aclVariables" -Verbose
Then I tried passing the environment variable as an argument, but that didn't work either
param
(
$RbacAccessTokenParams
)
$RbacAccessTokenParams.GetEnumerator() | % {$_.Name}
$RbacAccessTokenParams | % {
Write-Verbose "variable is $_" -Verbose
Write-Verbose "name is $_.Name" -Verbose
Write-Verbose "value is $_.Value" -Verbose
}
This is how I passed as argument
-RbacAccessTokenParams $(aclVariables)
What I'm missing here?
I've tested your scenario on my side with TFS 2017.3.1, and it works when pass the environment variable as an argument. You can upgrade your TFS first and try again. Attach my steps for your reference:
1.
2.
3.
4.
Non-secret variables are already stored as environment variables; you do not need to do anything special to access them. You can access them with $ENV:VariableName. Periods are replaced with underscores. So Foo.Bar would be $env:FOO_BAR.
Secret variables should be passed in to the script that requires them.
However, this only applies on the agent. If you're using the PowerShell On Target Machines task to run a script, you need to pass the variables as arguments to the script. There is no way around this, unless you choose to use deployment groups.
Or, better yet, follow a configuration-as-code convention and store application-specific values in source controlled configuration files that your scripts read, so that you are not tightly coupled to your deployment orchestration platform.
Is there a way to persist changes in environment value between tasks in Visual Studio Team Services? I'm using Powershell to change it but it only changes it in the task not the whole process.
script 1
Write-Verbose "Before: $Env:SuperVersion"
$Env:SuperVersion = $NewVersion
Write-Verbose "After: $Env:SuperVersion"
script 2
Write-Verbose "Final: $Env:SuperVersion"
I see the change at After but Final is always getting the original value
Based on this issue following line will do the trick.
Write-Host ("##vso[task.setvariable variable=SuperVersion;]$NewVersion")
You may find more commands like that in here
Correct answer has already been posted for this question below, however I think that the discussion presented at the following blog specifically targets the two different ways of setting build variables: one in which the variable will be available only within the specific task in which it is set and another using which you can set a build variable in one task and then access it in another:
https://blogs.msdn.microsoft.com/premier_developer/2016/04/13/tips-for-writing-powershell-scripts-to-use-in-build-and-release-tasks/
I find that after using
Write-Host ("##vso[task.setvariable variable=SuperVersion;]$NewVersion")
that within the same task, the value has not changed, but in later tasks that value has changed.
This is on TFS 2018 using inline powershell.
FIRST TASK
$ENV:SuperVersion = "2.0"
Write-Host ("##vso[task.setvariable variable=SuperVersion;]"3.2"")
# Output will be "2.0"
Write-Output $ENV:SuperVersion
$ENV:SuperVersion = "5.5"
# Output will be "5.5" but only within the scope of this task.
Write-Output $ENV:SuperVersion
NEXT TASK
Write-Output $ENV:SuperVersion
# Output is "3.2"
Environment variables created with $env: are Process variables, so they're lost when the process exits and you can't access them from another process (PowerShell instance).
You need to create User or Machine environment variable:
[Environment]::SetEnvironmentVariable('SuperVersion', $NewVersion, 'User')
[Environment]::SetEnvironmentVariable('SuperVersion', $NewVersion, 'Machine')
I'm not sure though, that it will work in VS Team Services, you'd have to test it.
Reference:
Types of environment variables
Reuse of environment variables in Path