Azure Devops YAML管道-如何重复执行任务 [英] Azure Devops YAML pipeline - how to repeat a task

查看:90
本文介绍了Azure Devops YAML管道-如何重复执行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的YAML管道中,我有一个部署工作:

In my YAML pipeline I have a deployment job:

- stage: deployment_to_development
  jobs:
  - deployment: deployment_to_development
    displayName: Deploy to Development
    environment: Development
    variables:
    - template: migrate-vars.yml
    strategy:
      runOnce:
        deploy:
          steps:
          - template: migrate-data.yml

部署模板是一个简单的DbUp任务:

The deployment template is a simple DbUp task:

steps:
  - task: UpdateDatabaseWithDbUp@2
    displayName: Migrate data
    inputs:
      ConnectionString: 'Data Source=$(DatabaseServer);Initial Catalog=$(DatabaseName);Integrated Security=SSPI'
      ScriptPath: '$(Pipeline.Workspace)\data-migrations'
      JournalSchemaName: dbo
      JournalTableName: _SchemaVersions
      LogScriptOutput: true
      IncludeSubfolders: true
      Order: FolderStructure
      TransactionStrategy: SingleTransaction

变量模板定义服务器和数据库名称:

The variables template defines the server and db name:

variables:
   DatabaseServer: 'server'
   DatabaseName: 'dbName'
   Instances: '_1,_2'

这对于单个数据库都可以正常工作.但是,我希望对Instances变量中定义的每个实例(即,名为dbName_1,dbName_2的数据库)重复执行该任务.这个看似简单的壮举被证明是困难的.

This all works fine for a single database. However, I wish to repeat the task for each instance defined in the Instances variable, i.e for databases named dbName_1, dbName_2. This seemingly simple feat is proving difficult.

我尝试将实例作为参数数组传递并使用

I have tried passing the instances as a parameter array and iterating them using

parameters:
  param: []

steps:
  - ${{each p in parameters.param}}:

,但未在任务中评估'p'变量. 还有许多徒劳的尝试.我一定在这里遗漏了一些非常明显的东西.什么事?

but the 'p' variable isn't evaluated in the task. There have been many more futile attempts. I must be missing something very obvious here. What is it?

推荐答案

我无法测试UpdateDatabaseWithDbUp@2,但是我有什么解释了如何实现目标的.首先定义templeate.yaml

I can't test UpdateDatabaseWithDbUp@2 but I have sth what explain how you can achieve your goal. First define templeate.yaml

parameters:
- name: 'instances'
  type: object
  default: {}
- name: 'server'
  type: string
  default: ''

steps:
- ${{ each instance in parameters.instances }}:
  - script: echo ${{ parameters.server }}:${{ instance }}

然后在您的版本中重复使用此模板:

then reuse this template in your build:

steps:
- template: template.yaml
  parameters:
    instances: 
    - test1
    - test2
    server: someServer

结果如下:

这篇关于Azure Devops YAML管道-如何重复执行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆