Deploy dacpac through VSTS only if it's been m

2019-09-17 07:09发布

In the deploy dacpac step in VSTS, you can set the database to only run based on custom conditions. The conditions examples are based on VSTS build information, and I can't find any documentation on using conditions from a connected Azure subscription or dacpac metadata. In the conditional page, they have a version variable which seems like it might be useful, but I can't find other information about it.

Basically, when the dacpac step is triggered, I want to check metadata against existing data, conditionally run the build step, and update metadata. Is this possible through a VSTS build step?

1条回答
淡お忘
2楼-- · 2019-09-17 07:41

Yes, it is possible. You can add an user defined variable (such as the variable result with default value 0) in the VSTS build definition. And with the value 1 to run the dacpac step, with value 0 to skip the step.

Detail steps as below:

  • Add a PowerShell task with two operations before the dacpac step:

    1. Check if there has new changes for the existing data.

      If the metadata only stored in Azure, you can refer this way to connect with Azure in powershell. If the metadata also stored in the repository (such as a git repo) you build with, you can also check the update in the repository.

    2. Set the result variable value based on if there the metadata is updated or not.

      If the data is updated, then change the result variable with value 1:

      Write-Host ("##vso[task.setvariable variable=result]1")
      

      Else, do not change the value (keep the value with 0)

    Since the data are managed in git VCS, you can check if the data is update or not in git repo. If the data is changed, then change the variable result as 1. detail powershell script as below:

    $files=$(git diff HEAD HEAD~1 --name-only)
    echo "changed files as below: $files"
    if ($files -contains 'filename')
      Write-Host ("##vso[task.setvariable variable=result]1")
    
  • Set conditions for the dacpac step:

    In the task, select Custom conditions for Run this task. If you want to run this task when succeeding and the variable result variable is 1, you can the express:

    and(succeeded(), eq(variables['result'], '1'))
    

Now if the result with the value 0, the dacpac step will be skipped, is the result with value 1, the dacpack will be executed.

查看更多
登录 后发表回答