Creating variables based on conditions with other variables
Hello all,
I am quite new to azure devops and I am struggling with understanding why my approaches to do something in the pipelines doesn’t work.
Starting from a template, I have such a piece of code in it, to which I will pass variables:
steps:
– ${{ if gt(length(parameters.artifactoryUrl), 0) }}:
– bash: |
<doSomeWorkHere>
displayName: Do some work only if the URL is provided
Then in the pipeline everything starts from variables definition like so:
variables:
– name: isRelease
value: eq(variables[‘Build.SourceBranch’], ‘refs/heads/master’)
– name: isSnapshot
value: eq(variables[‘Build.SourceBranch’], ‘refs/heads/develop’)
# set artifactory url to stable if release, to stage if snapshot or else to empty (no publish to artifactory)
– name: artifactoryUrl
${{ if and( and( eq(variables[‘Build.SourceBranch’], ‘refs/heads/master’), ne(variables[‘Build.Reason’], ‘Schedule’)), not( and ( eq(variables[‘Build.SourceBranch’], ‘refs/heads/develop’), ne(variables[‘Build.Reason’], ‘Schedule’)))) }}:
value: ‘someURL1’
${{ if and( not( and ( eq(variables[‘Build.SourceBranch’], ‘refs/heads/master’), ne(variables[‘Build.Reason’], ‘Schedule’)) ), and ( eq(variables[‘Build.SourceBranch’], ‘refs/heads/develop’), ne(variables[‘Build.Reason’], ‘Schedule’)) ) }}:
value: ‘someURL2’
${{ if and( not( and ( eq(variables[‘Build.SourceBranch’], ‘refs/heads/master’), ne(variables[‘Build.Reason’], ‘Schedule’)) ), not ( and ( eq(variables[‘Build.SourceBranch’], ‘refs/heads/develop’), ne(variables[‘Build.Reason’], ‘Schedule’)) ) ) }}:
value: ”
The value of artifactoryUrl is passed into the template mentioned above. This actually works. But it looks quite unreadable and there are some duplications. So I made my attemp to simplify it. Instead of checking the same thing as in the previous variables definitions, I tried to reuse those new variables in another conditional expressions like so:
variables:
– name: isRelease
value: eq(variables[‘Build.SourceBranch’], ‘refs/heads/master’)
– name: isSnapshot
value: eq(variables[‘Build.SourceBranch’], ‘refs/heads/develop’)
# set artifactory url to stable if release, to stage if snapshot or else to empty (no publish to artifactory)
– name: artifactoryUrl
${{ if and(eq(variables[‘isRelease’], true), ne(variables[‘Build.Reason’], ‘Schedule’)) }}:
value: ‘someUrl1’
${{ if and(eq(variables[‘isSnapshot’], true), ne(variables[‘Build.Reason’], ‘Schedule’)) }}:
value: ‘someUrl2’
${{ if or(and(eq(variables[‘isRelease’], false), eq(variables[‘isSnapshot’], false)), eq(variables[‘Build.Reason’], ‘Schedule’)) }}:
value: ”
Why referring to the previously created variables doens’t work? I already tried to use in those conditions different types of variables (macro, compile-time, run-time) and nothing worked for me. What I am missing?
# Oh forgot to mention, we use older Azure Devops instance, so “else”, “elseif” is not recognized.
Hello all,I am quite new to azure devops and I am struggling with understanding why my approaches to do something in the pipelines doesn’t work.Starting from a template, I have such a piece of code in it, to which I will pass variables: steps:
– ${{ if gt(length(parameters.artifactoryUrl), 0) }}:
– bash: |
<doSomeWorkHere>
displayName: Do some work only if the URL is provided Then in the pipeline everything starts from variables definition like so: variables:
– name: isRelease
value: eq(variables[‘Build.SourceBranch’], ‘refs/heads/master’)
– name: isSnapshot
value: eq(variables[‘Build.SourceBranch’], ‘refs/heads/develop’)
# set artifactory url to stable if release, to stage if snapshot or else to empty (no publish to artifactory)
– name: artifactoryUrl
${{ if and( and( eq(variables[‘Build.SourceBranch’], ‘refs/heads/master’), ne(variables[‘Build.Reason’], ‘Schedule’)), not( and ( eq(variables[‘Build.SourceBranch’], ‘refs/heads/develop’), ne(variables[‘Build.Reason’], ‘Schedule’)))) }}:
value: ‘someURL1’
${{ if and( not( and ( eq(variables[‘Build.SourceBranch’], ‘refs/heads/master’), ne(variables[‘Build.Reason’], ‘Schedule’)) ), and ( eq(variables[‘Build.SourceBranch’], ‘refs/heads/develop’), ne(variables[‘Build.Reason’], ‘Schedule’)) ) }}:
value: ‘someURL2’
${{ if and( not( and ( eq(variables[‘Build.SourceBranch’], ‘refs/heads/master’), ne(variables[‘Build.Reason’], ‘Schedule’)) ), not ( and ( eq(variables[‘Build.SourceBranch’], ‘refs/heads/develop’), ne(variables[‘Build.Reason’], ‘Schedule’)) ) ) }}:
value: ” The value of artifactoryUrl is passed into the template mentioned above. This actually works. But it looks quite unreadable and there are some duplications. So I made my attemp to simplify it. Instead of checking the same thing as in the previous variables definitions, I tried to reuse those new variables in another conditional expressions like so: variables:
– name: isRelease
value: eq(variables[‘Build.SourceBranch’], ‘refs/heads/master’)
– name: isSnapshot
value: eq(variables[‘Build.SourceBranch’], ‘refs/heads/develop’)
# set artifactory url to stable if release, to stage if snapshot or else to empty (no publish to artifactory)
– name: artifactoryUrl
${{ if and(eq(variables[‘isRelease’], true), ne(variables[‘Build.Reason’], ‘Schedule’)) }}:
value: ‘someUrl1’
${{ if and(eq(variables[‘isSnapshot’], true), ne(variables[‘Build.Reason’], ‘Schedule’)) }}:
value: ‘someUrl2’
${{ if or(and(eq(variables[‘isRelease’], false), eq(variables[‘isSnapshot’], false)), eq(variables[‘Build.Reason’], ‘Schedule’)) }}:
value: ” Why referring to the previously created variables doens’t work? I already tried to use in those conditions different types of variables (macro, compile-time, run-time) and nothing worked for me. What I am missing? # Oh forgot to mention, we use older Azure Devops instance, so “else”, “elseif” is not recognized. Read More