Azure DevOps pipeline trigger variables - azure

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.

Related

How can we define different variable group for different schedule trigger in Azure devops

Different variable group for different schedule trigger in Azure devops
Do you mean you would like to use variable in different variable groups in different schedule triggered pipelines? What about go to UI->library->choose the specific variable group then choose pipeline permission and add the corresponding schedule pipelines.
Then add the variable group name in the schedule pipelines yml and you could output the value of variables in the group, for example if there is a variable named 'SomeNumber' in the variable group.
variables:
- group: MyVarGroup
steps:
- task: PowerShell#2
inputs:
targetType: 'inline'
script: Write-Host "$(SomeNumber)"
But if you mean to add variable into the schedule trigger syntax, you can't use pipeline variables when specifying schedules in yml. You could see the official doc for more details.
If it is not what you want, please kindly specific your issue with the sample yaml, then I will try to do further investigation.

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)

Protect Tasks from modification in Azure Pipeline

How can I ensure that a particular scan is run at the end of an Azure Pipeline? The user should not be able to delete/modify this task. I cannot use decorators because they inject jobs across all pipelines in the Azure Organization, or I will need to filter using project names, which is not possible. Is there some other way to make this mandatory?
From your requirement, the pipeline decorators can directly meet your requirement.
Pipeline decorators also supports to filter using project name.
You can define the if expression in decorator YAML file to filter the projects.
Here is an example:
my-decorator.yml
steps:
- ${{ if in(variables['System.TeamProject'], '123', 'azure', 'ProjectC') }}:
- task: CmdLine#2
displayName: 'Run my script (injected from decorator)'
inputs:
script: 'echo "test"'
Result:
When the project name meets the filter, it will run the pipeline decorator task.
For example:
If no, it will not run the pipeline decorator task.
For example:
For more detailed info, you can refer to this doc: Use a decorator to inject steps into a pipeline

Pass pipeline variable to release variable Azure DevOps

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

How to call Environment Variables from Azure Devops Variable Groups in Terraform files?

Vairables are declared in Terraform files. Environment Variables are declared in Azure DevOps as TF_VAR_example. Still when the Pipeline runs it gets stuck in terraform plan, waiting for an input for that value.
you need to define them in the env section of your tasks:
- task: Bash#3
displayName: 'TF_plan'
env:
TF_VAR_example: $(TF_VAR_example)

Resources