Azure Devops release pipeline - Get artifact variable - azure-devops

I've a Release pipeline in AzureDevops which pulls it's artifact from Azure Container Registry.
By creating a new release, at the "dockerbuild" needs to be selected which image from ACR shall be chosen:
Currently i can't access the variable "dockerbuild" with it's value "1358 in the release pipeline. How do i need to write this, for access from powerShell?
I tried these approaches, without luck:

Try the following,
Write-Host $env:RELEASE_ARTIFACTS_dockerbuild_BUILDNUMBER
You probably already see the reference.

The article of release artifact alias Usage that Charles provided is correct.
But the usage he provided still have problem.
It only works on OS like windows OS which is not case-sensitive.
See the below usage:
Write-Host "$ env:RELEASE_ARTIFACTS_dockerbuild_BUILDNUMBER"
Write-Host $env:RELEASE_ARTIFACTS_dockerbuild_BUILDNUMBER #This only works on windows.
Write-Host "$ env:RELEASE_ARTIFACTS_DOCKERBUILD_BUILDNUMBER"
Write-Host $env:RELEASE_ARTIFACTS_DOCKERBUILD_BUILDNUMBER #This will work both on windows and linux.
Write-Host "$ (Release.Artifacts.dockerbuild.BuildNumber)"
Write-Host $(Release.Artifacts.dockerbuild.BuildNumber) #In Inline script, this will always work.

You can use $(Build.BuildNumber).

Related

how can i output current value of a variable in terraform during azure pipeline run?

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.

Getting Database names from SQLAzure using powershell in azure devops

I have a need to list down all the database names of a particular server on Azure.
$databases = Get-AzSqlDatabase -ServerName servername -ResourceGroupName resourcegroupname
foreach($dbs in $databases)
{
$dbs.DatabaseName
}
this script works well run from local.
My question is how to run this from an Azure DevOps release using the powershell task?
You have to use the Azure-Powershell task.
That way the pipeline can connect to the Proper Azure Subscirption, just like you connected to the Subscription manually from Powershell
Details about the Azure Powershell Task: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-powershell?view=azure-devops
But them I wonder what you want to do with the List of the databases.
Going out on a limb here, but you'd probably want the output back in the Pipeline. In which case please check out
https://github.com/microsoft/azure-pipelines-agent/blob/master/docs/preview/outputvariable.md
Write-Host "##vso[task.setvariable variable=var01;isOutput=true]123"

Custom variables in TFS Release Management

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.

ServiceEndpoint and ResourceManagerEndpoint values do not match existing environment. Please use Environment parameter

I have a PowerShell script running in Octopus Deploy as part of my deployment process. An extract of the script is below:
Import-AzurePublishSettingsFile "myAzurePublishSetting.PublishSettings"
Select-AzureSubscription 'mySubscription'
Set-AzureSubscription -SubscriptionName 'mySubscription' -Environment 'myEnvironment' -CurrentStorageAccountName 'myStorageAccount'
I'm now getting the below error from the Set-AzureSubscription cmdlet:
ServiceEndpoint and ResourceManagerEndpoint values do not match existing environment. Please use Environment parameter.
at Microsoft.WindowsAzure.Commands.Profile.SetAzureSubscriptionCommand.ExecuteCmdlet()
at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()
Octopus Deploy is hosted in an Azure virtual machine. This script worked fine until a few days ago so maybe Azure has changed something since nothing else has changed.
It's even more puzzling since I can run this script successfully on the virtual machine in both a PowerShell window and using Calamari.exe which is apparently what Octopus uses under the hood to call the script.
Any ideas?
This can happen if you have made changes to Azure subscriptions, for example disabling a subscription. Powershell still has a cache of the previous subscriptions. Use Get-AzureAccount to get the Id of the account and then Remove-AzureAccount. Finally, add the account again using Add-AzureAccount.

Change environment variable value between tasks in Build vNext

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