Pass pipeline variable to release variable Azure DevOps - azure

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

Related

Access pipeline A´s variables from pipeline B... Azure Devops

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)

Azure DevOps - Setting An Environment Variable on the Target Machine in a Deploy Group

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)

Azure DevOps pipeline trigger variables

We have this below scenario -
Pipeline 1 triggers pipeline 2 and also send variables $(path1), $(path2), $(path3)... to pipeline 2.
I'm trying to find a way to do a for-each loop for the variables which are being sent from pipeline1.
We are using YAML for the setup.
Thanks in advance.
You can use each function
parameters:
steps:
- ${{ each p in parameters.pipeline1vars }}:
- script: echo ${{ p }}
Currently, we can use the expression "resources.pipeline.<Alias>.<var_name>" to only pass some predefined variables of the resource pipeline to the triggered pipeline (see here).
We have no way to directly pass the custom variables from the resource pipeline to the triggered pipeline.
However, you can reference to the article below to try passing the variables through pipeline artifact.
How to pass variables with pipeline trigger in Azure Pipeline
You can save all the variables you want to pass into the artifact file. And in the triggered pipeline, you can try to use a loop to read each variable saved in the file.

Set Azure Pipeline variable value in SQL Server Deployment task for backup

I am using SQL Server Database Deployment task in azure release pipeline to take back up of the database I need to set backup filename/path in a pipeline variable to use it in another task under a different deployment group in the same stage to restore the DB in case of any release error.
But for SQL Server Deployment task, i couldn't find anything as such. "Settable at release time" flag is turned ON for the variable.
Basically the requirement is to back up the Database and restore in case of any error in the DACPAC release. Please suggest a way to set the variable value in SQL Server Deployment task or any alternate way to achieve my requirement. I need the value of SQL variable #FullPath in a Pipeline Variable dynamically set during the release time.
i couldn't find anything as such. "Settable at release time" flag is turned ON for the variable.
We could set the variable in the release variable tab or variable group such as FullPath = xxx, then use it in the task SQL Server Deployment via $(FullPath). Then we could set the feature Settable at release time in the variable page, please check the pic below.
I need the value of SQL variable #FullPath in a Pipeline Variable dynamically set during the release time.
We could also try to set the variable via the power script Write-Host "##vso[task.setvariable variable={variable name}]{variable value}", then we can call the variable in the another task. You could refer to this doc and ticket.

How do i identify my AzureDevOps id for a pipeline in ADO Pipelines?

So I am attempting to create a downstream project trying to use an artifact stored in azure pipeline artifact to build. I am using the task
DownloadPipelineArtifact#0
https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/download-pipeline-artifact?view=azure-devops
It talks about the need for a pipelineId, not really sure where to find out the id for my other pipeline. Is there any easy way, its supposed to be a ~4 digit number according the documentation.
Thanks
Go to the target pipeline you want -> Edit.
Check the URL. There you have the pipeline id.
.../_apps/hub/ms.vss-build-web.ci-designer-hub?pipelineId=1234&branch=main
I'm sorry I could not find a proper way to refer this without hardcoding.
There is an existing open issues on the pipeline ID.
The doc which you mentioned doesn't provide much information about pipelineID.
As per microsoft
pipelineId appears to be BuildId, and not the build definition id. It
needs the actual instance id of where the artifact is associated. I
was able to make this work by referencing a release variable tied to
the artifact alias. My alias is named "artifacts" and using
$(RELEASE_ARTIFACTS_ARTIFACTS_BUILDID) did the trick. So the format
would be $(RELEASE_ARTIFACTS_<alias>_BUILDID)
If you were trying to consume in a build and not a release pipeline
you would need to somehow get the value of $(Build.BuildId)
I hope as this matures there are plans to make pipeline artifacts
published from a build automatically in release, just like they are
when using the old Build Artifacts. Currently for me that is not
happening so I am forced to manually add this step to my release
pipeline and associate it with the build pipeline.
I was facing the same problem in my azure devops pipelines, I don't know if it applies the same way for you, but here is my solution to do it :
There is the function az pipeline show that gives you the id of a pipeline with its name:
Pipeline_to_find="$1"
pipelineInfo=$(az pipelines show --name "$Pipeline_to_find")
id=$(echo "$pipelineInfo" | python -c "import sys, json; print(json.load(sys.stdin)['id'])")
#export this var to be used in any other task of your pipeline
echo "##vso[task.setvariable variable=id;]$id"
You can get pipeline ID from a pipeline directly from portal.
Go Azure Pipeline
Now select the pipeline you want the ID from and choose “Edit”
Once in EDIT PIPELINE mode, click the dotted menu and select “TRIGGERS”
Now, click on “variables” tab
Here you will see a variable — system.definitionId which is aka PipelineId
There are two IDs you may need to know in Azure Pipelines.
Build Pipeline ID / Definition ID: This is the ID of the Pipeline not a particular run of the pipeline. You can get it via System.DefinitionId
Build Record ID / Build ID: This is the ID for a particular run/record of your pipeline run. You can access it in your pipeline as Build.BuildId
In your case, you will need to use the Build.BuildId since you are trying to get the artifact from a particular run of a pipeline.
Reference: Predefined Azure Pipeline Variables
The following command uses Azure CLI (with DevOps extension) and jq to get the pipeline id in Bash shell:
az pipelines show --name <PIPELINE_NAME> | jq -r .id
If you want to use this inside an Azure Pipeline, you need to use an Azure CLI task and probably install jq on the run agent.
See ultimatom's answer for how to set the id as a variable in the pipeline.

Resources