gpt4 book ai didi

azure - 将dependsOn 设置为上一阶段,而不对Azure Pipelines 中的值进行硬编码

转载 作者:行者123 更新时间:2023-12-02 07:56:27 26 4
gpt4 key购买 nike

使用 Azure Pipelines,如何将 dependsOn 设置为前一阶段而不对值进行硬编码?

我可以使用某种预定义变量吗?我只看到了当前阶段,没有看到之前的阶段。

我的管道正在执行 Terraform 部署。第一个(构建)阶段为每个环境并行运行 terraform plan,然后使用该计划文件运行 terraform apply 的(部署)阶段将使用该计划依次在每个环境中。

我在计划(构建)和应用(部署)阶段使用两个不同的模板。我目前正在使用参数来指定 dependsOn 列表值,我需要在其中明确指定所有计划(构建)阶段,但是对于应用(部署)阶段,我希望参数的默认值类似 $(Build.PreviousStage) 的内容,使其更加优雅。

# azure-pipelines.yml

# LINES REMOVED #

stages:
# Terraform Plan (DevTest)
- template: templates/plan.yml
parameters:
az_service_connection: DevTest-Terraform
environment: DevTest

# Terraform Plan (Stage)
- template: templates/plan.yml
parameters:
az_service_connection: Stage-Terraform
environment: Stage

# Terraform Plan (Prod)
- template: templates/plan.yml
parameters:
az_service_connection: Prod-Terraform
environment: Prod

# Terraform Deploy (DevTest)
- template: templates/deploy.yml
parameters:
az_service_connection: DevTest-Terraform
depends_on:
- DevTest_Plan
- Stage_Plan
- Prod_Plan
environment: DevTest

# Terraform Deploy (Stage)
- template: templates/deploy.yml
parameters:
az_service_connection: Stage-Terraform
depends_on:
- DevTest_Deploy
environment: Stage

# Terraform Deploy (Prod)
- template: templates/deploy.yml
parameters:
az_service_connection: Prod-Terraform
depends_on:
- Stage_Deploy
environment: Prod
# ./templates/deploy.yml

parameters:
- name: az_service_connection
type: string
- name: depends_on
type: object
- name: environment
type: string

stages:
# Terraform Deploy
- stage: ${{ parameters.environment }}_Deploy
displayName: Terraform Deploy (${{ parameters.environment }})
condition: and(succeeded(), in(variables['Build.Reason'], 'IndividualCI', 'BatchedCI', 'Manual'))
dependsOn:
${{ parameters.depends_on }}

# LINES REMOVED #

最佳答案

通过 if 条件检查 depends_on 参数是否为空,同时还将参数默认值设置为“”,这使我能够解决此问题,从而允许我设置 depends_on 参数用于第一个 DEV 部署阶段,而不为其他阶段设置。

正如 @Krzysztof Madej 在他的回答/评论中所述,不存在这样的变量来设置 dependsOn 的前一阶段,但如果未设置,将自动依赖于前一阶段。

# azure-pipelines.yml

# LINES REMOVED #

stages:
# Terraform Plan (DevTest)
- template: templates/plan.yml
parameters:
az_service_connection: DevTest-Terraform
environment: DevTest

# Terraform Plan (Stage)
- template: templates/plan.yml
parameters:
az_service_connection: Stage-Terraform
environment: Stage

# Terraform Plan (Prod)
- template: templates/plan.yml
parameters:
az_service_connection: Prod-Terraform
environment: Prod

# Terraform Deploy (DevTest)
- template: templates/deploy.yml
parameters:
az_service_connection: DevTest-Terraform
depends_on:
- DevTest_Plan
- Stage_Plan
- Prod_Plan
environment: DevTest

# Terraform Deploy (Stage)
- template: templates/deploy.yml
parameters:
az_service_connection: Stage-Terraform
environment: Stage

# Terraform Deploy (Prod)
- template: templates/deploy.yml
parameters:
az_service_connection: Prod-Terraform
environment: Prod

# templates/deploy.yml

parameters:
- name: az_service_connection
type: string
- name: depends_on
type: object
default:
- name: environment
type: string

stages:
# Terraform Deploy
- stage: ${{ parameters.environment }}_Deploy
displayName: Terraform Deploy (${{ parameters.environment }})
condition: and(succeeded(), in(variables['Build.Reason'], 'IndividualCI', 'BatchedCI', 'Manual'))
${{ if ne(length(parameters.depends_on), 0) }}:
dependsOn:
${{ parameters.depends_on }}

关于azure - 将dependsOn 设置为上一阶段,而不对Azure Pipelines 中的值进行硬编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64701610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com