问题描述
我正在建立一个多阶段的管道,并且我使用了许多模板,因为每种环境的部署作业,任务和阶段具有很多共享的逻辑.因此,在此阶段,我要设置一个 environmentName
变量,然后在每个作业中使用该变量:
-阶段:StageReleaseProddisplayName:阶段发布(产品)变量:environmentName:'prod'职位:-部署:DeployWebAppdisplayName:部署Wep应用程序水池:vmImage:$(vmImage)环境:$(environmentName)战略:runOnce:部署:脚步:-模板:config/pipelines/templates/_deploy.task.yml参数:environmentName:$(environmentName)
通常,这真的很好用.我可以在ADO UI中看到所有环境,并且可以在阶段之间添加批准检查.但是我也经常遇到这个问题:
有时变量不会被插值.感觉这种情况发生在50%的时间上,但是我也无法推断出为什么.现在,我的解决方法是在不使用参数的情况下对环境变量进行硬编码,但这严重限制了我如何对所有管道进行模板化.
问题在于如何填充作业名称.在多阶段ADO管道中,如果在作业或阶段名称中使用了变量,则在
使用变量进行调用的另一件事是,在多级管道中要跟踪它们有些棘手,它具有将变量存储在ADO变量组中的能力.根据解决方案的结构,这实际上可以为您简化事情.
如果您真的想将此变量设为定义变量模板.这可以通过创建类似于以下内容的单独的yaml文件来完成:
变量:environmentNameDEV:开发人员
然后在您的azure-pipelines-yml文件中引用此模板,例如:
变量:-模板:variables.yml
最后,在创建阶段名称时:
-阶段:$ {{variables.environmentNameDEV}}
缺点是每个环境都有一个单独的变量名.好处是,如果所有存储库都使用相同的变量模板,则可以确保所有命名标准之间的一致性.
I'm building a multi-stage pipeline, and I'm using a lot of templates as the deploy jobs, tasks, and stages for each environment has a lot of shared logic. So on the stage I'm setting a environmentName
variable, then using that in each of the jobs:
- stage: StageReleaseProd
displayName: Stage Release (Prod)
variables:
environmentName: 'prod'
jobs:
- deployment: DeployWebApp
displayName: Deploy Wep App
pool:
vmImage: $(vmImage)
environment: $(environmentName)
strategy:
runOnce:
deploy:
steps:
- template: config/pipelines/templates/_deploy.task.yml
parameters:
environmentName: $(environmentName)
Often times this works really well. I can see all the environments in the ADO UI and can add approval checks between stages. But I'm also frequently hitting this:
Sometimes the variable doesn't get interpolated. It feels like this happens 50% of the time, but I also can't deduce a pattern as to why. For now my workaround is to hardcode the environment variable without using parameters, but that severely limits how I can templatize all our pipelines.
The issue lies with when job name is being populated. In multi stage ADO pipelines if the variable is being used in a job or stage name it is being used at compile time vs run time. Try referencing your variable like ${( variables.envronmentName })
Here ar ethe different ways to reference a variable and the implications of doing so:
The other thing to call out with variables as they are a little tricky to keep track of in multi stage pipelines is there is the ability to store variables in an ADO variable group. Depending how the solution is structured this may actually simplify things for you.
Another option if you truly want to make this a variable is define a variable template. This would be done by creating a seperate yaml file similar to:
variables:
environmentNameDEV: dev
Then reference this template in your azure-pipelines-yml file like:
variables:
- template: variables.yml
And lastly when creating your stage name:
- stage: ${{ variables.environmentNameDEV }}
Downside is a separate variable name for each environment. Upside is if all repos use the same variable template can ensure consistency across everything with naming standards.
这篇关于Azure Devops多阶段管道环境名称设置不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!