Is it possible to Update the Build Definition name using YAML in Azure Pipelines - azure-devops

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)

Related

Azure Devops yml pipeline if else condition with variables

I am trying to use if else conditions in Azure Devops yml pipeline with variable groups. I am trying to implement it as per latest Azure Devops yaml pipeline build.
Following is the sample code for the if else condition in my scenario. test is a variable inside my-global variable group.
variables:
- group: my-global
- name: fileName
${{ if eq(variables['test'], 'true') }}:
value: 'product.js'
${{ elseif eq(variables['test'], false) }}:
value: 'productCost.js'
jobs:
- job:
steps:
- bash:
echo test variable value $(fileName)
When the above code is executed, in echo statement we don't see any value for filename, i.e. it empty, meaning none of the above if else condition was executed, however when I test the if else condition with the following condition.
- name: fileName
${{ if eq('true', 'true') }}:
value: 'product.js'
Filename did echo the correct value, i.e. product.js. So my conclusion is that I am not able to refer the variables from the variable group correctly. So any suggestion will be helpful and appreciated.
Thanks!
Unfortunately there is no ternary operator in Azure DevOps Pipelines. And it seems unlikely considering the state of https://github.com/microsoft/azure-pipelines-yaml/issues/256 and https://github.com/microsoft/azure-pipelines-yaml/issues/278. So for the time being the only choices are :
conditional insertion : it works with parameters, and should work with variables according to the documentation (but it is difficult to use properly),
or the hacks you can find in this Stack Overflow question.
Another work-around has been posted by Simon Alling on GitHub (https://github.com/microsoft/azure-pipelines-yaml/issues/256#issuecomment-1077684972) :
format(
replace(replace(condition, True, '{0}'), False, '{1}'),
valueIfTrue,
valueIfFalse
)
It is similar to the solution provided by Tejas Nagchandi, but I find it a little bit better because the syntax looks closer to what it would be if there was a ternary operator.
I was able to achieve the goal using some dirty work-around, but I do agree that using parameters would be much better way unless ternary operators are available for Azure DevOps YAML pipeline.
The issue is that ${{ if condition }}: is compile time expression, thus the variables under variable group are not available.
I was able to use runtime expressions $[<expression>]
Reference: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops
My pipeline:
trigger:
- none
variables:
- group: Temp-group-for-testing
- name: fileName
value: $[replace(replace('True',eq(variables['test'], 'True'), 'value1'),'True','value2')]
stages:
- stage: test
jobs:
- job: testvar
continueOnError: false
steps:
- bash: echo $(fileName)
displayName: "echo variable"
Results are available on github
After detailed investigation I realized that if else doesnt work with variables in Az Devop yaml pipelines, it only works with parameters. However the solution posted by #Tejas Nagchandi is a workaround and might be able to accomplish the same logic of if else setting variable value with replace commands. Hats off to TN.

How do I pass the job name into a github action's input?

jobs:
my-name:
name: "My Name"
...
steps:
- name: Slack Notification
uses: my-action
with:
slack-msg: ${{ jobs.${{ env.GITHUB_JOB }}.name }}
I want that slack-msg to evaluate to "My Name". I'm using my-action in multiple jobs, and I always want to pass in the job name, but I don't know how to do that. When I tried the above, the job literally didn't run and I don't know how to troubleshoot why: the github workflow log for my-name literally doesn't exist.
How do I pass job-name into an input parameter?
As nested expression are not supported you can use a trick like below to obtaint the matrix job name.
jobs:
test:
env:
# to expose matrix job name to steps, which is not possible with expansions
JOB_NAME: ${{ matrix.name || format('{0} ({1})', matrix.tox-target, matrix.os) }}
name: ${{ matrix.name || format('{0} ({1})', matrix.tox-target, matrix.os) }}
Note that you cannot really access the matrix name, but you can ensure you save the same name into an environment variable and use that.

Is there a way to use custom variables in Azure Pipelines conditions

I am trying to do something like this
variables:
${{ if eq(variables['abc'], 'dev') }}:
someOtherVariable: '123'
with a variable defined via UI here:
It doesn't work. someOtherVariable is not defined after this.
Is there a way to use this variable in conditions? What should be the syntax?
thanks
The syntax is correct but doesn't seem to work for conditions that rely on variables within the block where variables are defined for custom variables.
It is one of the (many) quirks of the Azure Pipelines YAML processing pipeline. Some conditions, variables, templates, syntax is only available at specific stages of the YAML processing and it depends on whether you are in a pipeline, template, or decorator.
Simplest solution is to use a script step to set the variable and optionally make that step conditional:
${{ if eq(variables['condition'], 'true') }}:
script: echo '##vso[task.setvariable variable=someOtherVariable]123'
or rely on one of my tasks to do that on your behalf:
- task: SetVariable#1
inputs:
name: 'someOtherVariable'
value: '123'
condition: eq(variables['condition'], 'true')
or:
${{ if eq(variables['condition'], 'true') }}:
- task: SetVariable#1
inputs:
name: 'someOtherVariable'
value: '123'

Using a pipeline variable for a variable group in YAML?

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.

Azure DevOps IIS Deploy Pipelines - where do variable values source from in templates?

I oversee some teams, and one team has an Azure DevOps pipeline leveraging the YAML pipeline setup. Unfortunately, due to Covid-19, we have had to release a lot of staff, including the entire DevOps team. That has left my team with no DevOps knowledge and an established YAML pipeline that we're struggling to get acquainted with.
So far, we've make progress and have learned a lot. However, while trying to troubleshoot an IIS web site deployment task, we have had some trouble figuring out where some of the parameters passed to each stage are sourced from.
For example, a snippet from a template:
- task: IISWebAppManagementOnMachineGroup#0
displayName: 'Create Or Update IIS Website'
inputs:
EnableIIS: true
WebsiteName: ${{ parameters.WebsiteName }}
WebsitePhysicalPath: ${{ parameters.WebsitePhysicalPath }}
AddBinding: ${{ parameters.AddWebsiteBinding }}
**Bindings: ${{ parameters.WebsiteBindings }}**
CreateOrUpdateAppPoolForWebsite: ${{ parameters.CreateOrUpdateAppPoolForWebsite }}
AppPoolNameForWebsite: ${{ parameters.WebsiteName }}
DotNetVersionForWebsite: ${{ parameters.DotNetVersionForWebsite }}
PipeLineModeForWebsite: ${{ parameters.PipeLineModeForWebsite }}
AppPoolIdentityForWebsite: ${{ parameters.AppPoolIdentityForWebsite }}
AppPoolUsernameForWebsite: ${{ parameters.AppPoolUsernameForWebsite }}
"WebsiteBindings" is one that we're interested in ... and I can work back to where the template is being used, and see this:
WebsiteBindings: '$(DevOps:IISWebsiteBindings)'
Problem is - I want to see what is being passed in here, and I cannot seem to figure out where the above value comes from. I am sure the answer is simple, but none of us have any Azzure DevOps experience, and we're kind of stuck on this simple problem. Is there anything in the above syntax that can clue me in as to where the values are located?
Other areas in the pipeline are referencing Azure AppConfig Key/Value storage for config transforms, and there are some pipeline variables set, but none line up with any of the values I am looking for.
Can anyone provide any insight here? Thank you in advance.
Problem is - I want to see what is being passed in here, and I cannot
seem to figure out where the above value comes from.
You can add a powershell task and output these variables through Write-Host to see what is passed here . For example : Write-Host "$(parameters.WebsiteBindings)"
According to the syntax of ${{ parameters.WebsiteBindings }}, I speculate that this variable comes from the template parameters.
In a pipeline, template expression variables (${{ variables.var }}) get processed at compile time, before runtime starts. You can use template expression syntax to expand both template parameters and variables . Template expressions are designed for reusing parts of YAML as templates.