I would like to use a common pipeline definition for our solutions. Using variables, I would like to specify solution specific settings. This works, except for a variable group.
I would like to use the pipeline definition in my variable group definition.
For example:
group: $(Build.DefinitionName).Dev
But that does not work. Another option would be to use a pipeline variable, but neither does work:
group: $(buildDefinitonName).Dev
group: {{ variables.buildDefinitonName }}.Dev
What does work is a parameter, but I do not want to specify it for each run.
group: ${{ parameters.buildDefinition }}.Dev
One option is to use your deployment in a template and scope the variable group to a job in that template. The parameter passed into the template would be the environment.
Here is how the template could look:
jobs:
- deployment: Deploy_JobName
variables:
- group: 'ProjectName${{ parameters.stage}}'
The parameter in the template would look like:
parameters:
- name: stage
type: string
This template would be called from a joblike:
jobs:
- template: template.yml
parameters:
stage: ${{ parameters.stage }}
Thanks for your response. Found out that
group: ${{ variables['Build.Definition'] }}.Dev
also works. So you can use predefined variables, but not pipeline variables this way.
Related
Is it possible to have different variable template files that would be used based on the branch being built? I was trying to do something like this:
jobs:
- job: BuildandPublish
variables:
- template: /env/$(Build.SourceBranchName).vars.yml
But that doesn't work, I'm guessing do to the order that those variables are replaced.
Is it possible to have different variable template files that would be used based on the branch being built?
From your YAML sample, you are using the format: $(Build.SourceBranchName). The variable value will be expanded at runtime.
But the template will read the variable at Compile time.
To solve this issue, you need to change the format : ${{ variables['Build.SourceBranchName'] }}
Here is an example:
jobs:
- job: BuildandPublish
variables:
- template: /env/${{ variables['Build.SourceBranchName'] }}.vars.yml
I have one variable group in ADO library which store different paths and some other variables.
In my main "master" pipeline I use it as below:
variables:
- group: myGroupName
- name: nameOfMyVariable(from variables group) or JustAnyName
- value: $[variables.nameOfMyVariable] or $[variables.JustAnyName]
then in job in the first Stage (for testing, there is only one stage and job for now) I'm trying to using template yaml:
jobs:
- template: my-template.yaml
parameters:
path: $(nameOfMyVariable) or $(JustAnyName)
then in my-template.yaml I have this code:
parameters:
- name: path
type: string
default: ''
jobs:
- job: BuildSomething
steps:
- task: CopyFiles#2
inputs:
Contents: |
${{ parameters.path }}
TargetFolder: '$Build.ArtifactStagingDirectory)'
....
Rest is not that important as it just can't find files to copy and when I try to print parameters.path with echo I get error :
syntax error: invalid arithmetic operator(error token is ".nameOfMyVariable").
I do not know how to fix it so I can access variables from variable group in some of my templates. Do I need to use ##vso[task.setvariables] or something else?
If you want use variable from variable group it is enough to just include this group
variables:
- group: myGroupName
And then use variable by name $(nameOfMyVariable)
In your example it seems you try unnecessary try to declare this variable again in first yaml example.
This example is additionally incorrect because you are addings dash directly before 'value' keyword and it may cause undefined behaviour.
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#specify-variables
Just like Kontekst says, when it comes to variable groups, you don't need to declare the variable names and values in your yaml. Once you declare the variable groups in your yaml, you could use the variables from the groups directly.
And if you are using parameters against templates, for your scenario, I suppose that you could declare the parameters in your main yaml, and input the parameter value into your template. (and you don't have to use the variable groups)
My main yaml as below.
trigger:
- none
pool:
vmImage: ubuntu-latest
parameters:
- name: pathmain
displayName: Source Path
type: string
default: README.md
values:
- azure-pipelines.yml
- README.md
jobs:
- job:
steps:
- script: echo ${{ parameters.pathmain }}
- template: test-template.yml
parameters:
path: ${{parameters.pathmain}}
My test-template as below.
parameters:
- name: path
displayName: Source Path
type: string
jobs:
- job: BuildSomething
steps:
- task: CopyFiles#2
inputs:
Contents: |
${{ parameters.path }}
TargetFolder: '$Build.ArtifactStagingDirectory'
I set up a variable group called secret-variables and gave it access to my pipeline.
In my pipeline I use a variable template and inside that variable template I define the variable group. I pass the variables from the variable template to pipeline templates using template expression syntax.
azure-pipelines.yml:
trigger:
- master
pool:
vmImage: windows-latest
variables:
- template: pipeline-variables.yml
stages:
- template: templates/myPipelineTemplate.yml
parameters:
mySecretVariable: ${{ variables.mySecretVariable }}
pipeline-variables.yml:
variables:
# secret-variables contain mySecretVariable
- group: secret-variables
- name: foo
value: bar
Yet the value of mySecretVariable in myPipelineTemplate.yml is empty. What have I missed?
It seems that variable groups do not support template expression syntax. Macro syntax needs to be used in stead, change the code in the stage to:
stages:
- template: templates/myPipelineTemplate.yml
parameters:
mySecretVariable: $(mySecretVariable)
This works because macro syntax is evaluated at runtime and template expression is evaluated at compile time.
I have defined a variable in the pipeline and set it to false/true. However, when the pipeline is set, I can see that the value for parameters.RunUnitTest is $(RunUnitTest), and this is not the value I set up in the pipeline. So what I am doing wrong here?
trigger: none
extends:
template: ThunderPipeline.yaml
parameters:
MergeBetweenBranches: true
FromBranch: 'master'
ToBranch: 'R_Current_Sprint'
RunUnitTest: '$(RunUnitTest)'
Variables can be defined in one YAML and included in another template. This could be useful if you want to store all of your variables in one file. If you are using a template to include variables in a pipeline, the included template can only be used to define variables. You can use steps and more complex logic when you are extending from a template. Use parameters instead of variables when you want to restrict type.
In this example, the variable favoriteVeggie is included in azure-pipelines.yml.
# File: vars.yml
variables:
favoriteVeggie: 'brussels sprouts'
# File: azure-pipelines.yml
variables:
- template: vars.yml # Template reference
steps:
- script: echo My favorite vegetable is ${{ variables.favoriteVeggie }}.
Or something like:
# File: templates/steps-with-params.yml
parameters:
- name: 'runExtendedTests' # defaults for any parameters that aren't specified
type: boolean
default: false
steps:
- script: npm test
- ${{ if eq(parameters.runExtendedTests, true) }}:
- script: npm test --extended
# File: azure-pipelines.yml
steps:
- script: npm install
- template: templates/steps-with-params.yml # Template reference
parameters:
runExtendedTests: 'true'
I am trying to update Build definition name based on the YAML runtime parameters. I am able to achieve this like below
name: ${{ parameters.source }} to ${{ parameters.target }} $(Date:yyyyMMdd).$(Rev:r)
But I want to update the build name by providing conditional expressions.
Please let me know if this can be achieved.
You can do something like this
${{ if eq(variables['Build.SourceBranchName'], 'master') }}:
stageName: prod
Follow this link for full list of expressions you can use.
You can reference the documentation that describes how to set the run (build) number.
If defining your own variable, My.Variable, reference it the run number as:
$(Build.DefinitionName)_$(Build.DefinitionVersion)_$(Build.RequestedFor)_$(Build.BuildId)_$(My.Variable)