Azure管道使用if条件设置环境

huangapple go评论61阅读模式
英文:

Azure pipelines set environment using if condition

问题

I am working on a dacpac deployment pipeline and I need DBA to approve the stage if script finds certain things in the sql script.
So my idea is that I have a powershell script setting a output variable in one stage and then based one the output of that variable I either set the deployment environment to one value or the other.

My problem is that I am not getting my if statement to evaluate true even if the variable is what it is supposed to be.

How I test this and what is the problem.

Template file where I test the if statement and where I will change the environment:

deploy_dacpac.yml

parameters:
  - name: sqlWarn
    type: string

jobs:
  - deployment: 'deploy_dacpac'
    displayName: 'Deploy the dacpac'
    environment: 'Valters Code'
    strategy:
      runOnce:
        deploy:
          pool:
            name: 'SQL-TEST'
          steps:
            - powershell: Write-Output ${{ parameters.sqlWarn }}
            - ${{ if eq(parameters.sqlWarn, 'warning') }}:
                - powershell: Write-Output "nice"
            - ${{ else }}:
                - powershell: Write-Output "not nice"
            - template: '../steps/dacpac-deploy-steps.yml'

Partial part of where the parameter is set:

  - stage: TEST
    displayName: TEST (SANDBOX)
    ${{ if eq(parameters.deployDacpac, true) }}:
      dependsOn: PRE_TEST
    ${{ else }}:
      dependsOn: BUILD
    variables:
    - template: variables/test.yml
    - group: Container Registry
    - name: sqlWarn
      value: $[stageDependencies.PRE_TEST.check_for_anomalies.outputs['check_for_anomalies.sql-warn']]
    jobs:
      - ${{ if eq(parameters.deployDacpac, true) }}:
        - template: '../common/jobs/deploy_dacpac.yml'
          parameters:
            sqlWarn: $(sqlWarn)

For some reason it always outputs not nice result. Even though the parameter output is warning

I have tried to change ${{ else }} to ${{ if ne(parameters.sqlWarn, 'warning') }}

I think I need to add that my main goal is to set the environment based on the if condition

${{if eq(parameters.sqlWarn, 'warning')}} 
environment: 'environment1' 
${{if ne(parameters.sqlWarn, 'warning')}} 
environment: 'environment2'
英文:

I am working on a dacpac deployment pipeline and I need DBA to approve the stage if script finds certain things in the sql script.
So my idea is that I have a powershell script setting a output variable in one stage and then based one the output of that variable I either set the deployment environment to one value or the other.

My problem is that I am not getting my if statement to evaluate true even if the variable is what it is supposed to be.

How I test this and what is the problem.

Template file where I test the if statement and where I will change the environment:

deploy_dacpac.yml

parameters:
  - name: sqlWarn
    type: string

jobs:
  - deployment: 'deploy_dacpac'
    displayName: 'Deploy the dacpac'
    environment: 'Valters Code'
    strategy:
      runOnce:
        deploy:
          pool:
            name: 'SQL-TEST'
          steps:
            - powershell: Write-Output ${{ parameters.sqlWarn }}
            - ${{ if eq(parameters.sqlWarn, 'warning') }}:
                - powershell: Write-Output "nice"
            - ${{ else }}:
                - powershell: Write-Output "not nice"
            - template: '../steps/dacpac-deploy-steps.yml'

Partial part of where the parameter is set:

  - stage: TEST
    displayName: TEST (SANDBOX)
    ${{ if eq(parameters.deployDacpac, true) }}:
      dependsOn: PRE_TEST
    ${{ else }}:
      dependsOn: BUILD
    variables:
    - template: variables/test.yml
    - group: Container Registry
    - name: sqlWarn
      value: $[stageDependencies.PRE_TEST.check_for_anomalies.outputs['check_for_anomalies.sql-warn']]
    jobs:
      - ${{ if eq(parameters.deployDacpac, true) }}:
        - template: ../common/jobs/deploy_dacpac.yml
          parameters:
            sqlWarn: $(sqlWarn)

For some reason it always outputs not nice result. Even though the parameter output is warning

I have tried to change ${{ else }} to ${{ if ne(parameters.sqlWarn, 'warning') }}

I think I need to add that my main goal is to set environment based on the if condition

${{if eq(parameters.sqlWarn, 'warning')}} 
environment: 'environment1' 
${{if ne(parameters.sqlWarn, 'warning')}} 
environment: 'environment2'

答案1

得分: 0

你正在使用有条件运行步骤的语法,该语法使用编译时表达式(注意${{}}的语法)。这意味着在流水线排队之后、在执行任何作业之前将立即评估该值。在这个时间点上,sqlWarn参数尚未设置。这就是你看到这种行为的原因。

由于sqlWarn的值是在作业中生成的,你需要在运行时进行评估。为此,你可以使用condition参数:

steps:
  - powershell: Write-Output "nice"
    condition: and(succeeded(), eq(parameters.sqlWarn, 'warning'))
  - powershell: Write-Output "not nice"
    condition: and(succeeded(), ne(parameters.sqlWarn, 'warning'))
英文:

You are using the syntax to conditionally run a step, which uses a compile time expression (note the ${{}} syntax). This means that the evaluation of the value will happen right after the pipeline has been queued, before any job is being executed. At this point of time, the sqlWarn parameter has not been set yet. That's why you are seeing this behavior.

As the value for sqlWarn is generated in a job, you need to do the evaluation at runtime. For this you can use the condition parameter:

steps:
  - powershell: Write-Output "nice"
    condition: and(succeeded(), eq(parameters.sqlWarn, 'warning'))
  - powershell: Write-Output "not nice"
    condition: and(succeeded(), ne(parameters.sqlWarn, 'warning'))

huangapple
  • 本文由 发表于 2023年6月5日 18:08:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定