is there any way to use Set variables in scripts azure classic pipeline?
Looking this option in azure classic pipeline
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/set-variables-scripts?view=azure-devops&tabs=bash
just add bash or powershell script:
Related
After some internet search, I wasnt able to find a proper way or suggestion on how to access variables from different pipelines. Lets say, from Pipeline A access variables of Pipeline B.
What I did find, is the idea to use Key Vault, which I am not able to use right now. I was wondering if there is a workaround, lets say, with powershell.
All of this is happening in an Azure Devops environment, where I am trying to access/read variables from different pipelines.
Any ideas?
Kind regards
Leo.
You can make use of variable groups to call variables in multiple pipelines within a project.
You just need to reference that variable in the YAML script or release pipeline with the variable group and use it in any pipeline, Refer below :-
I went to my project > Pipelines > Library > Variable Group > And added a variable > You can add multiple variables here storing your secrets or values.
Using the variable group in a yaml pipeline :-
trigger:
- main
pool:
vmImage: ubuntu-latest
variables:
- group: SharedVariables
steps:
- script: |
echo $(databaseserverpassword)
Now, when you run the pipeline, It will ask you to permit the use of variable group for the pipeline.
This will enable access to all the variables in the SharedVariables group.
Output :-
We got our databaseservername value masked.
You can also enable this variable group for all the pipeline in the project by default.
You can use the same variable group in your Classic pipeline or release pipeline in release or specific stages like below :-
Reference :-
Variable groups for Azure Pipelines - Azure Pipelines | Microsoft Learn
For PowerShell - Azure DevOps: how to manage CI/CD variable groups using PowerShell – Radu Narita (ranari.com)
Created CICD pipeline with trigger release, and Pipeline variable set in to Azure Devops Rest API. Getting variable in pipeline and set Output true using PowerShell.
Write-Host $(webAppName)
echo "##vso[task.setvariable variable=webAppName;isoutput=true]$webAppName"
And set the release pipeline variable using
WebAppName= ${{variables.webAppName}} on the variable tab. It's not working, don't have idea how can we retrive the pipeline variable in release.
The is no official way to pass variables from Build to Release.
Using Variable Groups in Azure Pipeline Library is a good option: https://learn.microsoft.com/en-us/azure/devops/pipelines/library/variable-groups?view=azure-devops&tabs=yaml
Another work around, you could store the variable values in a file and attach that as a Build Artifact. That way you can read the file in the release pipeline by downloading Build Artifacts and set the variable again. You could check my answer here: is there any way to pass variables from CI to CD pipeline
I am deploying an Asp.Net Core application via Azure DevOps' Pipelines option and trying to set an environment variable on each of the target machines. Each deployment environment has its own settings file (for example "appsettings.Development.json") that the Asp.Net app chooses from when it starts up by reading the ASPNETCORE_ENVIRONMENT environment variable. I am using a Deploy Group to deploy the artifact to our on-site servers and that is working perfectly until it comes to setting the correct appsettings file to use.
I tried unsuccessfully to use a PowerShell task that has this inline script:
$Env:ASPNETCORE_ENVIRONMENT="$(EnvironmentName)"
All the pipeline tasks are executed successfully but the environment variable doesn't actually get applied on the target machine.
How can I set an environment variable on the target machines in a Deploy Group?
The above inline script of yours $Env:ASPNETCORE_ENVIRONMENT="$(EnvironmentName)" only set the variable in the current terminal window opened by the powershell task. The terminal window will be shut down after the powershell task is complete. We usually use below solutions to set environment variables in the pipeline.
If you want to set an environment variable on the target machines in the release pipeline. You can directly set a variable ASPNETCORE_ENVIRONMENT in the release pipeline Variables tab. See below:
Go to your release Edit page-->Variables tab-->Add new Variable-->Set the stage Scope
There is another way to set the environment variables dynamically using logging commands(Write-Host "##vso[task.setvariable..]) in the pipeline. See below inline script in powershell task:
echo "##vso[task.setvariable variable=ASPNETCORE_ENVIRONMENT]$(EnvironmentName)"
Please check the official document for more information.
Setting an environment variable that way isn't persistent, as you've observed. You'll need to the .NET environment variable methods to accomplish what you want.
[System.Environment]::SetEnvironmentVariable('ASPNETCORE_ENVIRONMENT', '$(EnvironmentName)' [System.EnvironmentVariableTarget]::Machine)
In Azure Devops Variable Groups you do something like:
FileRepo = 'FolderA'
LogFile = '$(FileRepo)/Log.txt'
Is this possible in Azure App Configuration?
*UPDATE:
When using the App Configration as an Azure Devops Task extenstion, $() references will work during pipelines.
No, this is not possible. Settings are flat and they do not support referencing to other settings values. You can try to do this programmatically, by replacing your tokens with real values, but there is nothing out of the box.
May you please suggest how to lowercase the environment name in Azure DevOps pipelines.
When environment name is "Test" I want account name to be myprefixtest.
I don't think there is an option to do to lower when you use the task (like in your Azure CLI task), but you can add a small PowerShell script that does it before the Azure CLI task:
$envName = "$(Release.EnvironmentName)"
$lower = $envName.ToLower()
Write-Host "##vso[task.setvariable variable=Release.EnvironmentName;]$lower"
Have you seen lower? This might help with what you are trying to do.