英文:
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'))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论