Reputation: 33
First question on Stack Overflow, apologies for any etiquette faux pas.
We have an Azure Pipeline for application delivery where the engineer or developer can select the environment the app gets deployed to. I'll change some of the identifying info, but looks something like this:
parameters:
- name: environment
type: string
default: none (build app only)
values:
- none (build app only)
- test
- preprod
- prod
'none (build app only)' is for our CI stages. And our CD stage that looks something like this:
- stage: DeployToKubernetes
condition: ne('${{ parameters.environment }}', 'none (build app only)')
pool: $(buildpool)
jobs:
- template: <template>.yaml
parameters:
${{ each parameter in parameters }}:
${{ parameter.Key }}: ${{ parameter.Value }}
AppName: ${{ variables.solutionName }}
k8sServiceConnection: '${{ parameters.environment }}-k8s'
ServiceConnection: '${{ parameters.environment }}-sc'
This all works as expected when deploying to our environments but when selecting 'none (build app only)' as the environment, it errors.
I get errors as soon as I run the Pipeline such as:
The pipeline is not valid. Job <template>.job: Step input azureSubscription references service connection none (build app only)-sc which could not be found.
From what I can tell, this seems to be due to the fact that the ADO is validating the parameters passed to the stage before assessing the condition. My thought process was that if the condition for the stage isn't met, then ADO won't validate the underpinning jobs, but I think I'm missing something.
Can anyone please enlighten me?
I've tried reversing the condition statement and cloning the stage with the condition being if environment DOES eq an environment, and still get the issue.
EG:
- stage: DeployToKubernetes
condition: eq('${{ parameters.environment }}', 'preprod')
pool: $(buildpool)
jobs:
- template: <template>
parameters:
${{ each parameter in parameters }}:
${{ parameter.Key }}: ${{ parameter.Value }}
AppName: ${{ variables.solutionName }}
k8sServiceConnection: '${{ parameters.environment }}-k8s'
ServiceConnection: '${{ parameters.environment }}-sc'
Will still fail with the same message when selecting 'none' as the environment.
Also tried having the parameter value as 'none' instead of 'none (build app only)'. I've tried defining a variable that assesses the value of the parameter and using that variable as the condition (in case it's runtime / compile time issues). I've tried changing
condition: ne('${{ parameters.environment }}', 'none (build app only)')
to
condition: and(succeeded(), ne('${{ parameters.environment }}', 'none (build app only)'))
Appreciate any insight!
Upvotes: 3
Views: 768
Reputation: 9198
The problem is that the service connection used for the task is evaluated and approved at compile time (when the pipeline is queued), but the condition is only evaluated at run time (immediately before the task is run).
As an alternative, I'd suggest using an if expression to conditionally exclude the entire stage at compile time:
${{ if ne(parameters.environment, 'none (build app only)' }}:
- stage: DeployToKubernetes
condition: eq('${{ parameters.environment }}', 'preprod')
pool: $(buildpool)
jobs:
- template: <template>
parameters:
${{ each parameter in parameters }}:
${{ parameter.Key }}: ${{ parameter.Value }}
AppName: ${{ variables.solutionName }}
k8sServiceConnection: '${{ parameters.environment }}-k8s'
ServiceConnection: '${{ parameters.environment }}-sc'
Upvotes: 1