在 Azure DevOps Pipeline 中使用模板阶段

问题描述 投票:0回答:1

我使用不同存储库中的阶段作为 DevOps 管道中的模板阶段。但我无法在后续阶段中将这些阶段用作dependsOn。我不允许在模板阶段内进行更改(build_functionapp_testcase2、datafactory_testcase3)

这是我的管道,在这个管道中,如果上述任何一个阶段失败,我希望运行阶段清理,并且它还应该从“ImageDeployment”阶段获取 $NEW_VERSION 变量。请帮我解决这个问题。:

  - stage: ImageDeployment
    jobs:
      - job: NewImageDeployment
        timeoutInMinutes: 180
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          - task: AzureKeyVault@2
            displayName: 'Fetch Secrets'
            inputs:
              connectedServiceName: '$(GLOBAL_SERVICE_CONNECTION)'
              keyVaultName: $(GLOBAL_KV_NAME)
              secretsFilter: "Packer-Secret"
              runAsPreJob: false

          - task: AzureCLI@2
            displayName: 'Check image version'
            retryCountOnTaskFailure: 0
            inputs:
              azureSubscription: '$(GLOBAL_SERVICE_CONNECTION)'
              scriptType: 'bash'
              scriptLocation: 'inlineScript'
              inlineScript: |
                while IFS=$'\t' read -a arr_image_versions; do
                  version=${arr_image_versions[0]}
                  date=${arr_image_versions[1]}
                  if [[ $date > $old_date ]]; then
                    last_version=$version
                  fi
                  old_date=$date
                done <<< "$(az sig image-version list --gallery-image-definition ado-agent-basic-ubuntu2004 --gallery-name globalceagsig --resource-group GLOBAL-CEAG-ComputeRG --query "[].[name, publishingProfile.publishedDate]" -o tsv)"
                echo "Last image version: $last_version"
                echo "##vso[task.setvariable variable=last_version]$last_version"
            
          - task: Bash@3
            name: CheckImageVersion
            displayName: 'Increment image version'
            inputs:
              targetType: 'inline'
              script: |
                IFS='.' read -ra VERSION_PARTS <<< "$(last_version)"
                PATCH_VERSION=${VERSION_PARTS[2]}
                PATCH_VERSION=$((PATCH_VERSION + 1))
                NEW_VERSION="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.$PATCH_VERSION"
                echo "New image version: $NEW_VERSION"
                echo "##vso[task.setvariable variable=NEW_VERSION;isOutput=true]$NEW_VERSION"

  - template: tests/build_functionapp_testcase2.yaml@self

  - template: tests/datafactory_testcase3.yaml@self

  - stage: Cleanup
    dependsOn: ImageDeployment
    condition: failed()
    jobs:
      - job: DeleteImage
        pool:
          vmImage: 'ubuntu-latest'
        variables:
          NEW_VERSION: $[ stageDependencies.ImageDeployment.NewImageDeployment.outputs['CheckImageVersion.NEW_VERSION'] ]
        steps:
          - script: |
              echo "NEW_VERSION in Cleanup stage: $(NEW_VERSION)" ```
templates variables azure-devops azure-pipelines
1个回答
0
投票

TL;DR 理想情况下,应将参数添加到阶段模板中;如果这不是一种选择,请记住,当您在管道中定义多个阶段时,默认情况下,它们会一个接一个地运行

创建可配置/可扩展模板

考虑到很难预测和理解将使用阶段或作业模板的所有场景,为了创建可配置和可扩展的阶段/作业模板应该向这些模板添加一些参数,以便:

  • 设定阶段/工作条件
  • 设置阶段/作业依赖关系
  • 设置作业超时
  • ...

例如:

# templates/my-stage.yaml

parameters:  
  # other parameters here

  - name: dependsOn
    type: object
    displayName: 'Dependencies'
    default: []

  - name: condition
    type: string
    displayName: 'Condition'
    default: ''

stages:
  - stage: my_stage
    displayName: My stage
    dependsOn: ${{ parameters.dependsOn }}
    ${{ if ne(parameters.condition, '') }}:
      condition: ${{ parameters.condition }}
    jobs:
      # ...
© www.soinside.com 2019 - 2024. All rights reserved.