简体   繁体   中英

Azure Devops YAML Pipelines - While loop?

Is there such a thing in Azure Release Pipelines (YAML) as a while loop? My use case is that I am using runOnce strategy to deploy artifacts to a clean environment, deploy a clients data and before I move onto the next client I need to run a query to ensure all the processing is finished and health checks are done. All checks can be done via SQL scripts into an Azure SQL Database and eventually I need to compare the results and task timings against an expected set.

ie Does processing the client data across branches yield the expected results and timings.

Might be a square peg round hole so happy to use a different approach if there is an easier way.

 - deployment : Install
  pool:
    vmImage: ubuntu-latest
  environment:
    name: 'Test_Env'
    resourceType: VirtualMachine
  strategy:       
   runOnce:
     deploy:
       steps:
       # Remove and re-create blank database on the Elastic Pool.
         - task: SqlAzureDacpacDeployment@1
           displayName: Drop DB
           inputs:
             azureSubscription: 'Azure'
             AuthenticationType: 'server'
             ServerName: '$(DB_SERVER)'
             DatabaseName: 'master'
             SqlUsername: '$(DB_USERNAME)'
             SqlPassword: '$(DB_PASSWORD)'
             deployType: 'InlineSqlTask'
             SqlInline: |
                IF EXISTS (SELECT name FROM master.sys.databases WHERE name = N'$(DB_DATABASE)') DROP DATABASE [$(DB_DATABASE)]        
             IpDetectionMethod: 'AutoDetect'
         - task: SqlAzureDacpacDeployment@1
           displayName: Create DB
           inputs:
             azureSubscription: 'Azure'
             AuthenticationType: 'server'
             ServerName: '$(DB_SERVER)'
             DatabaseName: 'master'
             SqlUsername: '$(DB_USERNAME)'
             SqlPassword: '$(DB_PASSWORD)'
             deployType: 'InlineSqlTask'
             SqlInline: |
               CREATE DATABASE $(DB_DATABASE) ( SERVICE_OBJECTIVE = ELASTIC_POOL (name = [SQL_ElasticPool] ));        
             IpDetectionMethod: 'AutoDetect'                             
         - task: CmdLine@2
           displayName: Install Product
           inputs:
             script: |
               start /wait msiexec.exe /i "$(System.ArtifactsDirectory)\installer.msi" client_data= $(client_data) DB_USERNAME=$(DB_USERNAME) DB_PASSWORD=$(DB_PASSWORD)                 
               workingDirectory: $(System.ArtifactsDirectory)
         - task: CmdLine@2
           displayName: Start Service      
           inputs:
              script: |
                sc start $(WIN_SERVICE)
          
        # This is where I would want a while-loop
         - task: SqlAzureDacpacDeployment@1
           displayName: Check if processing finished
           inputs:
             azureSubscription: 'Azure'
             AuthenticationType: 'server'
             ServerName: '$(DB_SERVER)'
             DatabaseName: '$(DB_DATABASE)'
             SqlUsername: '$(DB_USERNAME)'
             SqlPassword: '$(DB_PASSWORD)'
             deployType: 'InlineSqlTask'
             SqlInline: |
                select 1 from eventlog if complete = 0                 
             IpDetectionMethod: 'AutoDetect'
  • In YAML we use each keyword, and it is an equivalent of the for loop .
  • You cannot run a task until a condition is met , you can run multiple task either by writing them one by one or by using the each statement which is equivalent to a for loop.
  • But you can use a GATE which is a way to control a deployment. They are predominantly used for heath checkups of infrastructure, external approvals for deployment, etc.
  • The gates can either be at the start of the pipeline or at the end of the pipeline.
  • Gates are of many types like the ones which use invokes functions and azure monitors and some gates use rest api too. Also you can create your custom gates too.
  • Gates will continue to check for a user specified condition until it is met.
  • So, you can use a azure function gate run the required scripts and return the results in the gate for validation.

Refence:

Azure Pipeline Gate

Azure Devops YAML Pipelines - While loop?

You could create a template which will have a set of Check if processing finished task, and pass loop time as parameters across during your build, like:

 - template: CheckProcessingFinished.yaml
   parameters:
     param: ["1","2","3"]

CheckProcessingFinished.yaml:

parameters:
  param : []

steps:
  - ${{each Looptimes in parameters.param}}:
    - task: SqlAzureDacpacDeployment@1
      displayName: Check if processing finished
      inputs:
          azureSubscription: 'Azure'
          AuthenticationType: 'server'
          ServerName: '$(DB_SERVER)'
          DatabaseName: '$(DB_DATABASE)'
          SqlUsername: '$(DB_USERNAME)'
          SqlPassword: '$(DB_PASSWORD)'
          deployType: 'InlineSqlTask'
          SqlInline: |
          select 1 from eventlog if complete = 0                 
          IpDetectionMethod: 'AutoDetect'

    - task: PowerShell@2
      inputs:
        targetType: 'Sleep 30 seconds'
        script: |
          Start-Sleep 30

You could check the document Solving the looping problem in Azure DevOps Pipelines for some more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM